Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) const int N=1e5+10,M=4e6+10,inf=1e9+10; ll sum[N<<2],mod; void pushup(int pos) { sum[pos]=(sum[pos<<1|1]*sum[pos<<1])%mod; } void buildtree(int l,int r,int pos) { if(l==r) { sum[pos]=1; return; } int mid=(l+r)>>1; buildtree(l,mid,pos<<1); buildtree(mid+1,r,pos<<1|1); pushup(pos); } void update(int point,ll change,int l,int r,int pos) { if(l==r&&l==point) { sum[pos]=change; return; } int mid=(l+r)>>1; if(point<=mid) update(point,change,l,mid,pos<<1); else update(point,change,mid+1,r,pos<<1|1); pushup(pos); } int main() { int T,cas=1; scanf("%d",&T); while(T--) { int n,m; scanf("%d%lld",&n,&mod); buildtree(1,n,1); printf("Case #%d:\n",cas++); for(int i=1;i<=n;i++) { int flag; ll l; scanf("%d%lld",&flag,&l); if(flag==1) update(i,l,1,n,1); else update(l,1,1,n,1); printf("%lld\n",sum[1]); } } return 0; }
原文:http://www.cnblogs.com/jhz033/p/5835571.html