#include <cstdio>
#include "segmenttree.h"
typedef long long ll;
int n, m, p;
std::vector<int> a;
struct Tag
{
int add, mul;
Tag(int _add = 0, int _mul = 1) { add = _add; mul = _mul; }
void operator+=(const Tag& y)
{
add = ((ll) add * y.mul + y.add) % p;
mul = (ll) mul * y.mul % p;
}
};
int merge(int x, int y)
{
return (x + y) % p;
}
void update(segmentTreeNode<int, Tag>& node, Tag tag)
{
node.val = ((ll) node.val * tag.mul + (ll) tag.add * (node.right - node.left)) % p;
node.tag += tag;
}
int main()
{
scanf("%d%d%d", &n, &m, &p);
a.resize(n);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
segmentTree<int, Tag, merge, update> t(n, a, 0);
while (m--)
{
int opt, l, r;
scanf("%d%d%d", &opt, &l, &r);
if (opt == 1)
{
int x;
scanf("%d", &x);
t.modify(l, r + 1, Tag(0, x));
}
else if (opt == 2)
{
int x;
scanf("%d", &x);
t.modify(l, r + 1, Tag(x, 1));
}
else printf("%d\n", t.query(l, r + 1));
}
return 0;
}
http://www.cplusplus.com/doc/tutorial/classes/
Protip:
#define
and global-levelusing
shouldn't appear in headers. It restricts users of your code more than necessary. Defines can be completely replaced by inline functions or constexpr variables.You're also doing things like duplicating
initValues
into the class, even though you only use it once.The functions you pass as arguments can be template arguments.
Thanks for reminding me. I'm not good at programming except CP, so there may be lots of mistakes.
However, I used
#undef
at the end, and I think it may not restrict the users, am I right?Okay, I didn't notice the undef, which is better — it's not perfect since it would undef identically named macros in earlier includes if they're used later in the code. Define/ifdef are good for things like choosing pieces of code based on compatibility (e.g. system-specific constants) or specific purpose if one source can be used for several programs.
OK, I got it. And the issues mentioned by you are fixed now.
Just noticed that "duplicating
initValues
" means "duplicatinginit
function". I used to let it be a public function, and then realized it's useless and make it private.cool