求调!!样例都过不掉

P3373 【模板】线段树 2

更新一下现在的 ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; namespace IO { #pragma GCC target("avx") #pragma GCC optimize("Og") #pragma GCC optimize("Ofast") template<class T> inline void read(T& x) { x = 0; int p = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') p = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } x = x * p; } template<class T> inline void write(T x) { if(x < 0) { x = -x; } if(x > 9) write(x / 10); putchar(x % 10 + '0'); } }; using namespace IO; const int N = 100010; int a[N]; int n,q,p; namespace SegmentTree { struct Seg { int l,r; ll lazy1,lazy2; ll pre; }; Seg Tree[N * 4]; int lchild(int root) { return root * 2; } int rchild(int root) { return root * 2 + 1; } void build(int root,int l,int r) { Tree[root].lazy2 = 0; Tree[root].lazy1 = 1; Tree[root].l = l; Tree[root].r = r; if(l == r) { Tree[root].pre = a[l]; return; } int mid = (l + r) >> 1; build(lchild(root),l,mid); build(rchild(root),mid + 1,r); Tree[root].pre = Tree[lchild(root)].pre + Tree[rchild(root)].pre; } void push_down(int root) { Tree[lchild(root)].pre = (Tree[lchild(root)].pre * Tree[root].lazy1 + Tree[root].lazy2 * (Tree[lchild(root)].r - Tree[lchild(root)].l + 1)) % p; Tree[rchild(root)].pre = (Tree[rchild(root)].pre * Tree[root].lazy1 + Tree[root].lazy2 * (Tree[rchild(root)].r - Tree[rchild(root)].l + 1)) % p; Tree[lchild(root)].lazy1 = (Tree[lchild(root)].lazy1 * Tree[root].lazy1) % p; Tree[rchild(root)].lazy1 = (Tree[rchild(root)].lazy1 * Tree[root].lazy1) % p; Tree[lchild(root)].lazy2 = (Tree[root].lazy1 * Tree[lchild(root)].lazy2 + Tree[root].lazy2) % p; Tree[rchild(root)].lazy2 = (Tree[root].lazy1 * Tree[rchild(root)].lazy2 + Tree[root].lazy2) % p; Tree[root].lazy2 = 0; Tree[root].lazy1 = 1; } void update1(int root,int l,int r,ll k) { if(l > Tree[root].r || r < Tree[root].l) return; if(Tree[root].l >= l && Tree[root].r <= r) { Tree[root].pre = (Tree[root].pre * k) % p; Tree[root].lazy1 = (Tree[root].lazy1 * k) % p; Tree[root].lazy2 = (Tree[root].lazy2 * k) % p; return; } push_down(root); int mid = (Tree[root].l + Tree[root].r) >> 1; update1(lchild(root),l,r,k); update1(rchild(root),l,r,k); Tree[root].pre = (Tree[lchild(root)].pre + Tree[rchild(root)].pre) % k; } void update2(int root,int l,int r,ll k) { if(l > Tree[root].r || r < Tree[root].l) return; if(Tree[root].l >= l && Tree[root].r <= r) { Tree[root].pre = (Tree[root].pre + k * (Tree[root].r - Tree[root].l + 1)) % p; Tree[root].lazy2 = (Tree[root].lazy2 + k) % p; return; } push_down(root); int mid = (Tree[root].l + Tree[root].r) >> 1; update2(lchild(root),l,r,k); update2(rchild(root),l,r,k); Tree[root].pre = (Tree[lchild(root)].pre + Tree[rchild(root)].pre) % p; } ll query(int root,int l,int r) { if(l > Tree[root].r || r < Tree[root].l) return 0; if(Tree[root].l >= l && Tree[root].r <= r) return Tree[root].pre; push_down(root); int mid = (Tree[root].l + Tree[root].r) >> 1; long long cnt = 0; cnt += query(lchild(root),l,r); cnt += query(rchild(root),l,r); return cnt; } }; using namespace SegmentTree; int main() { read(n);read(q);read(p); for(int i = 1;i <= n;i++) { read(a[i]); } build(1,1,n); for(int i = 1;i <= q;i++) { int opt; read(opt); if(opt == 1) { int l,r; ll k; k %= p; update1(1,l,r,k); }else if(opt == 2) { int l,r; ll k; read(l);read(r);read(k); k %= p; update2(1,l,r,k); }else { int l,r; read(l);read(r); write(query(1,l,r));puts(""); } } return 0; } ```
by TRZ_2007 @ 2020-01-25 18:02:24


@[TRZ_2007](/user/86971) 重构吧 (
by DepletedPrism @ 2020-01-25 18:09:41


@[TRZ_2007](/user/86971) 我也忘了改了哪些锅了,反正下面这个是改好的 ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; namespace IO { #pragma GCC target("avx") #pragma GCC optimize("Og") #pragma GCC optimize("Ofast") template<class T> inline void read(T& x) { x = 0; int p = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') p = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } x = x * p; } template<class T> inline void write(T x) { if(x < 0) { x = -x; } if(x > 9) write(x / 10); putchar(x % 10 + '0'); } }; using namespace IO; const int N = 100010; int a[N]; int n,q,p; namespace SegmentTree { struct Seg { int l,r; ll lazy1,lazy2; ll pre; }; Seg Tree[N * 4]; int lchild(int root) { return root * 2; } int rchild(int root) { return root * 2 + 1; } void build(int root,int l,int r) { Tree[root].lazy2 = 0; Tree[root].lazy1 = 1; Tree[root].l = l; Tree[root].r = r; if(l == r) { Tree[root].pre = a[l]; return; } int mid = (l + r) >> 1; build(lchild(root),l,mid); build(rchild(root),mid + 1,r); Tree[root].pre = Tree[lchild(root)].pre + Tree[rchild(root)].pre; } void push_down(int root) { Tree[lchild(root)].pre = (Tree[lchild(root)].pre * Tree[root].lazy1 + Tree[root].lazy2 * (Tree[lchild(root)].r - Tree[lchild(root)].l + 1)) % p; Tree[rchild(root)].pre = (Tree[rchild(root)].pre * Tree[root].lazy1 + Tree[root].lazy2 * (Tree[rchild(root)].r - Tree[rchild(root)].l + 1)) % p; Tree[lchild(root)].lazy1 = (Tree[lchild(root)].lazy1 * Tree[root].lazy1) % p; Tree[rchild(root)].lazy1 = (Tree[rchild(root)].lazy1 * Tree[root].lazy1) % p; Tree[lchild(root)].lazy2 = (Tree[root].lazy1 * Tree[lchild(root)].lazy2 + Tree[root].lazy2) % p; Tree[rchild(root)].lazy2 = (Tree[root].lazy1 * Tree[rchild(root)].lazy2 + Tree[root].lazy2) % p; Tree[root].lazy2 = 0; Tree[root].lazy1 = 1; } void update1(int root,int l,int r,ll k) { if(l > Tree[root].r || r < Tree[root].l) return; if(Tree[root].l >= l && Tree[root].r <= r) { Tree[root].pre = (Tree[root].pre * k) % p; Tree[root].lazy1 = (Tree[root].lazy1 * k) % p; Tree[root].lazy2 = (Tree[root].lazy2 * k) % p; return; } push_down(root); int mid = (Tree[root].l + Tree[root].r) >> 1; update1(lchild(root),l,r,k); update1(rchild(root),l,r,k); Tree[root].pre = (Tree[lchild(root)].pre + Tree[rchild(root)].pre) % p; } void update2(int root,int l,int r,ll k) { if(l > Tree[root].r || r < Tree[root].l) return; if(Tree[root].l >= l && Tree[root].r <= r) { Tree[root].pre = (Tree[root].pre + k * (Tree[root].r - Tree[root].l + 1)) % p; Tree[root].lazy2 = (Tree[root].lazy2 + k) % p; return; } push_down(root); int mid = (Tree[root].l + Tree[root].r) >> 1; update2(lchild(root),l,r,k); update2(rchild(root),l,r,k); Tree[root].pre = (Tree[lchild(root)].pre + Tree[rchild(root)].pre) % p; } ll query(int root,int l,int r) { if(l > Tree[root].r || r < Tree[root].l) return 0; if(Tree[root].l >= l && Tree[root].r <= r) return Tree[root].pre; push_down(root); int mid = (Tree[root].l + Tree[root].r) >> 1; long long cnt = 0; cnt += query(lchild(root),l,r); cnt += query(rchild(root),l,r); return cnt % p; } }; using namespace SegmentTree; int main() { read(n);read(q);read(p); for(int i = 1;i <= n;i++) { read(a[i]); } build(1,1,n); for(int i = 1;i <= q;i++) { int opt; read(opt); if(opt == 1) { int l,r; ll k; read(l);read(r);read(k); k %= p; update1(1,l,r,k); }else if(opt == 2) { int l,r; ll k; read(l);read(r);read(k); k %= p; update2(1,l,r,k); }else { int l,r; read(l);read(r); write(query(1,l,r));puts(""); } } return 0; } ```
by chenxinyang2006 @ 2020-01-25 23:30:58


@[chenxinyang2006](/user/49776) 谢谢,调过了
by TRZ_2007 @ 2020-01-26 20:32:07


|