我不是妹子

P3373 【模板】线段树 2

@[海洋](/space/show?uid=106519) 谢谢
by shitbro @ 2019-01-13 10:43:10


@[海洋](/space/show?uid=106519) 然而还是输出0
by shitbro @ 2019-01-13 10:43:35


@[larry2004](/space/show?uid=90972) emm……我再看看
by 海洋 @ 2019-01-13 10:44:34


```cpp #include<string> #include<cstdio> #include<iostream> using namespace std; struct tree{ int l,r; long long add,mul,pre; }t[1000005]; int a[100005]; int n,m,p; void build(int p,int l,int r) { t[p].l=l; t[p].r=r; t[p].mul=1; t[p].add=0; if(l==r) { t[p].pre=a[r]; return ; } int mid=(l+r)/2; build(p*2,l,mid); build(p*2+1,mid+1,r); t[p].pre=(t[p*2].pre+t[p*2+1].pre)%p; } void lan(int p) { t[p*2].add=(t[p].add+t[p*2].add*t[p].mul)%p; t[p*2+1].add=(t[p].add+t[p*2+1].add*t[p].mul)%p; t[p*2].mul=(t[p*2].mul*t[p].mul)%p; t[p*2+1].mul=(t[p*2+1].mul*t[p].mul)%p; t[p*2].pre=(t[p*2].pre*t[p].mul+t[p].add*(t[p*2].r-t[p*2].l+1))%p; t[p*2+1].pre=(t[p*2+1].pre*t[p].mul+t[p].add*(t[p*2+1].r-t[p*2].l+1))%p; t[p].add=0; t[p].mul=1; } void changec(int p,int x,int y,int z) { if(x<=t[p].l&&y>=t[p].r) { t[p].pre=(t[p].pre*z)%p; t[p].mul=(t[p].mul*z)%p; t[p].add=(t[p].add*z)%p; return; } lan(p); int mid=(t[p].r+t[p].l)/2; if(x<=mid) changec(p*2,x,mid,z); if(y>mid) changec(p*2+1,mid+1,y,z); t[p].pre=(t[p*2].pre+t[p*2+1].pre)%p; } void changej(int p,int x,int y,int z) { if(x<=t[p].l&&y>=t[p].r) { t[p].pre=(t[p].pre+z*(t[p].r-t[p].l+1))%p; t[p].add=(t[p].add+z)%p; return; } lan(p); int mid=(t[p].r+t[p].l)/2; if(x<=mid) changej(p*2,x,y,z); if(y>mid) changej(p*2+1,x,y,z); t[p].pre=(t[p*2].pre+t[p*2+1].pre)%p; } long long ask(int p,int x,int y) { if(x<=t[p].l&&y>=t[p].r) return t[p].pre; lan(p); int mid=(t[p].r+t[p].l)/2; long long ans=0; if(x<=mid) ans=(ans+ask(p*2,x,mid))%p; if(y>mid) ans=(ans+ask(p*2+1,mid+1,y))%p; return ans; } int main() { /*freopen(" ","r",stdin); freopen(" ","w",stdout);*/ cin>>n>>m>>p; for(int i=1;i<=n;i++) cin>>a[i]; build(1,1,n); for(int i=1;i<=m;i++) { int b,x,y,z; cin>>b; if(b==1) { cin>>x>>y>>z; changec(1,x,y,z); } else if(b==2) { cin>>x>>y>>z; changej(1,x,y,z); } else { cin>>x>>y; cout<<ask(1,x,y)%p<<endl; } } /*fclose(stdin); fclose(stdout);*/ return 0; } ```
by shitbro @ 2019-01-13 10:45:00


@[larry2004](/space/show?uid=90972) 我看错您代码的意思了,得改回来……qwq
by 海洋 @ 2019-01-13 10:47:19


@[larry2004](/space/show?uid=90972) 是变量定义混乱!!! ```cpp void build(int p,int l,int r) { t[p].l=l; t[p].r=r; t[p].mul=1; t[p].add=0; if(l==r) { t[p].pre=a[r]; return ; } int mid=(l+r)/2; build(p*2,l,mid); build(p*2+1,mid+1,r); t[p].pre=(t[p*2].pre+t[p*2+1].pre)%p; } ``` 你这里p到底是mod的数字还是权值?
by 海洋 @ 2019-01-13 10:52:09


@[larry2004](/space/show?uid=90972)
by 海洋 @ 2019-01-13 10:52:25


@[海洋](/space/show?uid=106519) 然后呢
by shitbro @ 2019-01-13 10:52:32


@[larry2004](/space/show?uid=90972) 你重新定义哈
by 海洋 @ 2019-01-13 10:53:32


@[larry2004](/space/show?uid=90972) ```cpp #include<string> #include<cstdio> #include<iostream> using namespace std; struct tree{ int l,r; long long add,mul,pre; }t[1000005]; int a[100005]; int n,m,p; void build(int p,int l,int r) { t[p].l=l; t[p].r=r; t[p].mul=1; t[p].add=0; if(l==r) { t[p].pre=a[r]; return ; } int mid=(l+r)/2; build(p*2,l,mid); build(p*2+1,mid+1,r); t[p].pre=(t[p*2].pre+t[p*2+1].pre)%p; } void lan(int p) { t[p*2].add=(t[p].add+t[p*2].add*t[p].mul)%p; t[p*2+1].add=(t[p].add+t[p*2+1].add*t[p].mul)%p; t[p*2].mul=(t[p*2].mul*t[p].mul)%p; t[p*2+1].mul=(t[p*2+1].mul*t[p].mul)%p; t[p*2].pre=(t[p*2].pre*t[p].mul+t[p].add*(t[p*2].r-t[p*2].l+1))%p; t[p*2+1].pre=(t[p*2+1].pre*t[p].mul+t[p].add*(t[p*2+1].r-t[p*2].l+1))%p; t[p].add=0; t[p].mul=1; } void changec(int p,int x,int y,int z) { if(x<=t[p].l&&y>=t[p].r) { t[p].pre=(t[p].pre*z)%p; t[p].mul=(t[p].mul*z)%p; t[p].add=(t[p].add*z)%p; return; } lan(p); int mid=(t[p].r+t[p].l)/2; if(x<=mid) changec(p*2,x,y,z); if(y>mid) changec(p*2+1,x,y,z); t[p].pre=t[p*2].pre+t[p*2+1].pre; } void changej(int p,int x,int y,int z) { if(x<=t[p].l&&y>=t[p].r) { t[p].pre=(t[p].pre+z*(t[p].r-t[p].l+1))%p; t[p].add=(t[p].add+z)%p; return; } lan(p); int mid=(t[p].r+t[p].l)/2; if(x<=mid) changej(p*2,x,y,z); if(y>mid) changej(p*2+1,x,y,z); t[p].pre=t[p*2].pre+t[p*2+1].pre; } long long ask(int p,int x,int y) { if(x<=t[p].l&&y>=t[p].r) return t[p].pre; lan(p); int mid=(t[p].r+t[p].l)/2; long long ans=0; if(x<=mid) ans=(ans+ask(p*2,x,y))%p; if(y>mid) ans=(ans+ask(p*2+1,x,y))%p; return ans; } int main() { /*freopen(" ","r",stdin); freopen(" ","w",stdout);*/ cin>>n>>m>>p; for(int i=1;i<=n;i++) cin>>a[i]; build(1,1,n); for(int i=1;i<=m;i++) { int b,x,y,z; cin>>b; if(b==1) { cin>>x>>y>>z; changec(1,x,y,z); } else if(b==2) { cin>>x>>y>>z; changej(1,x,y,z); } else { cin>>x>>y; cout<<ask(1,x,y)%p<<endl; } } /*fclose(stdin); fclose(stdout);*/ return 0; } ``` 在你最初始代码的基础上
by 海洋 @ 2019-01-13 10:54:20


上一页 | 下一页