标签:you problem watch out stream 线段 query wait slow
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2637 Accepted Submission(s): 732
#include<iostream> #include<algorithm> #define ls t<<1 #define rs (t<<1)|1 using namespace std; struct node { int l,r; int ll,ml,rl; int llx,mlx,rlx; }tree[100086*4]; void build(int t,int l,int r) { tree[t].l=l; tree[t].r=r; tree[t].ll=tree[t].rl=tree[t].ml=r-l+1; tree[t].llx=tree[t].rlx=tree[t].mlx=r-l+1; if(l==r){return;} int mid=(l+r)>>1; build(ls,l,mid); build(rs,mid+1,r); } void push_down(int t) { if(tree[t].l==tree[t].r)return; if(tree[t].ml==0){ tree[ls].ml=tree[ls].ll=tree[ls].rl=0; tree[rs].ml=tree[rs].ll=tree[rs].rl=0; } if(tree[t].ml==tree[t].r-tree[t].l+1) { tree[ls].ml=tree[ls].ll=tree[ls].rl=tree[ls].r-tree[ls].l+1; tree[rs].ml=tree[rs].ll=tree[rs].rl=tree[rs].r-tree[rs].l+1; } if(tree[t].mlx==0){ tree[ls].mlx=tree[ls].llx=tree[ls].rlx=0; tree[rs].mlx=tree[rs].llx=tree[rs].rlx=0; } if(tree[t].mlx==tree[t].r-tree[t].l+1) { tree[ls].mlx=tree[ls].llx=tree[ls].rlx=tree[ls].r-tree[ls].l+1; tree[rs].mlx=tree[rs].llx=tree[rs].rlx=tree[rs].r-tree[rs].l+1; } } int query1(int t,int x) { push_down(t); if(tree[t].ml<x){return -1;} if(tree[t].ll>=x){return tree[t].l;} if(tree[ls].ml>=x){return query1(ls,x);} if(tree[ls].rl+tree[rs].ll>=x){return tree[ls].r-tree[ls].rl+1;} if(tree[rs].ml>=x){return query1(rs,x);} if(tree[rs].rl>=x){return tree[rs].r-tree[rs].rl+1;} } int query2(int t,int x) { push_down(t); if(tree[t].mlx<x){return -1;} if(tree[t].llx>=x){return tree[t].l;} if(tree[ls].mlx>=x){return query2(ls,x);} if(tree[ls].rlx+tree[rs].llx>=x){return tree[ls].r-tree[ls].rlx+1;} if(tree[rs].mlx>=x){return query2(rs,x);} if(tree[rs].rlx>=x){return tree[rs].r-tree[rs].rlx+1;} } void update1(int t,int l,int r,int num)//来自屌丝的更新。不影响女神树 { if(tree[t].l==l&&tree[t].r==r){ tree[t].ll=tree[t].rl=tree[t].ml=(tree[t].r-tree[t].l+1)*num; return; } push_down(t); int mid=(tree[t].l+tree[t].r)>>1; if(r<=mid){update1(ls,l,r,num);} else if(l>mid){update1(rs,l,r,num);} else{ update1(ls,l,mid,num); update1(rs,mid+1,r,num); } tree[t].ll=tree[ls].ll; tree[t].rl=tree[rs].rl; tree[t].ml=max(tree[ls].ml,tree[rs].ml); tree[t].ml=max(tree[t].ml,tree[ls].rl+tree[rs].ll); if(tree[ls].ll==tree[ls].r-tree[ls].l+1){ tree[t].ll=tree[ls].ll+tree[rs].ll; } if(tree[rs].rl==tree[rs].r-tree[rs].l+1){ tree[t].rl=tree[rs].rl+tree[ls].rl; } } void update2(int t,int l,int r,int num)//来自女神的更新 { if(tree[t].l==l&&tree[t].r==r){ tree[t].ll=tree[t].rl=tree[t].ml=(tree[t].r-tree[t].l+1)*num; tree[t].llx=tree[t].rlx=tree[t].mlx=(tree[t].r-tree[t].l+1)*num; return; } push_down(t); int mid=(tree[t].l+tree[t].r)>>1; if(r<=mid){update2(ls,l,r,num);} else if(l>mid){update2(rs,l,r,num);} else{ update2(ls,l,mid,num); update2(rs,mid+1,r,num); } tree[t].ll=tree[ls].ll; tree[t].rl=tree[rs].rl; tree[t].ml=max(tree[ls].ml,tree[rs].ml); tree[t].ml=max(tree[t].ml,tree[ls].rl+tree[rs].ll); if(tree[ls].ll==tree[ls].r-tree[ls].l+1){ tree[t].ll=tree[ls].ll+tree[rs].ll; } if(tree[rs].rl==tree[rs].r-tree[rs].l+1){ tree[t].rl=tree[ls].rl+tree[rs].rl; } tree[t].llx=tree[ls].llx; tree[t].rlx=tree[rs].rlx; tree[t].mlx=max(tree[ls].mlx,tree[rs].mlx); tree[t].mlx=max(tree[t].mlx,tree[ls].rlx+tree[rs].llx); if(tree[ls].llx==tree[ls].r-tree[ls].l+1){ tree[t].llx=tree[ls].llx+tree[rs].llx; } if(tree[rs].rlx==tree[rs].r-tree[rs].l+1){ tree[t].rlx=tree[rs].rlx+tree[ls].rlx; } } void view_tree() { for(int i=1;i<=10;i++){ printf("\n%d: %d %d\n",i,tree[i].l,tree[i].r); printf("%d %d %d\n",tree[i].ll,tree[i].rl,tree[i].ml); printf("%d %d %d\n",tree[i].llx,tree[i].rlx,tree[i].mlx); } } int main() { int T; scanf("%d",&T); int sst=1; while(T--){ printf("Case %d:\n",sst++); int n,m,ans,tm; scanf("%d%d",&n,&m); char s[10]; build(1,1,n); for(int i=1;i<=m;i++){ scanf("%s",s); if(s[0]==‘N‘){ scanf("%d",&tm); ans=query1(1,tm); if(ans==-1){ ans=query2(1,tm); if(ans==-1){ printf("wait for me\n"); } else{ printf("%d,don‘t put my gezi\n",ans); update2(1,ans,ans+tm-1,0); } } else{ printf("%d,don‘t put my gezi\n",ans); update2(1,ans,ans+tm-1,0); } } if(s[0]==‘D‘){ scanf("%d",&tm); ans=query1(1,tm); if(ans==-1){printf("fly with yourself\n");} else {printf("%d,let‘s fly\n",ans); update1(1,ans,ans+tm-1,0); } } if(s[0]==‘S‘){ int x,y; scanf("%d%d",&x,&y); update2(1,x,y,1); printf("I am the hope of chinese chengxuyuan!!\n"); } } } }
As the last ship sailed towards the distant horizon,I sat there watching on a rock.My mind slowly drifting away,froming into my Dreamtale.
标签:you problem watch out stream 线段 query wait slow
原文地址:https://www.cnblogs.com/ZGQblogs/p/9374649.html