BZOJ3932 CQOI2015 任务查询系统-可持久化线段树-可持久化平衡树
2016-04-10 12:12
465 查看
BZOJ3932 CQOI2015 任务查询系统
可持久化线段树
每个时间点建立一棵权值线段树,保存数的个数与数的和。我们发现相邻的时间点所对应的线段树用很多重复部分
于是我们把每个修改(Si,Ei,Pi)变成在Si处加入Pi,在1+Ei处减去Pi,时间点为Si+1~Ei的线段树直接由前一个时间点复制而来。回答询问时直接在第Xi棵线段树上查找即可。
#include<bits/stdc++.h> using namespace std; #define LL long long const int maxn = 100005; const int INF = 0x3f3f3f3f; struct opt{ int pos,key,fg; }a[maxn<<1]; struct num{ int pos,key; }b[maxn]; int n,m,maxp=0; bool cmp(opt x,opt y){ return x.pos<y.pos; } bool cmpb(num x,num y){ return x.key<y.key; } struct node{ int siz;LL sum; node *L,*R; }mempool[maxn*38];int tot=0; node* new_node(const int c,const int d){ node *t=&mempool[tot++]; t->siz=c;t->sum=d; t->L=t->R=NULL; return t; } inline int siz(node* p){return p==NULL?0:p->siz;} inline LL sum(node* p){return p==NULL?0:p->sum;} node* add(node* p,int L,int R,int d,int k){ int inc=k>0?1:-1; node* t=new_node( siz(p)+ inc , sum(p)+k); int mid=(L+R)>>1; if(L==R)return t; if(d<=mid){ t->R=p->R; if(p->L==NULL)p->L=new_node(0,0); t->L=add(p->L,L,mid,d,k); t->L->siz=siz(p->L)+ inc ; t->L->sum=sum(p->L)+k; }else{ t->L=p->L; if(p->R==NULL)p->R=new_node(0,0); t->R=add(p->R,mid+1,R,d,k); t->R->siz=siz(p->R)+ inc ; t->R->sum=sum(p->R)+k; } return t; } LL query(node *r,int L,int R,int k){ if(r==NULL)return 0; if(k>=siz(r))return sum(r); int sz=siz(r); LL sm=sum(r); if(L==R)return 1ll*sm/sz*min(k,sz); int mid=(L+R)>>1; sz=siz(r->L); LL ret=0; ret+=query(r->L,L,mid,k); if(k>sz)ret+=query(r->R,mid+1,R,k-sz); return ret; } node* root[maxn]; int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ int s,e,p,j=i+n; scanf("%d%d%d",&s,&e,&p);e++; a[i].pos=s; a[i].key=p; a[i].fg=p; a[j].pos=e; a[j].key=p; a[j].fg=-p; b[i].key=p;b[i].pos=i; } sort(b+1,b+1+n,cmpb); int T=0; int tmp=b[1].pos; a[tmp].key=a[tmp+n].key=++T; for(int i=2;i<=n;i++){ tmp=b[i].pos; a[tmp].key=a[tmp+n].key=b[i].key==b[i-1].key?T:++T; } sort(a+1,a+1+n+n,cmp); root[0]=new_node(0,0); for(int i=1,t=1;i<=n<<1;i++){ for(;t<a[i].pos;t++)root[t]=root[t-1]; int d=a[i].key; root[t]=add(root[t-1],1,n,d,a[i].fg); i++; while(a[i].pos==a[i-1].pos){ d=a[i].key; root[t]=add(root[t],1,n,d,a[i].fg); i++; } t++;i--; } LL pre=1; for(int i=1;i<=m;i++){ int X,A,B,C; scanf("%d%d%d%d",&X,&A,&B,&C); int K=1+(A%C*pre%C+B%C)%C; LL ans=query(root[X],1,n,K); printf("%lld\n",ans); pre=ans; } }
可持久化平衡树
原理同可持久化线段树,省去了离散化。然而我的可持久化Treap感觉不大对啊,内存和时间都辣么大。#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn = 100005; #define LL long long struct node{ int key,pri,siz; LL sum; node *L,*R; }; #define siz(x) ( x == NULL ? 0 : x->siz ) #define sum(x) ( x == NULL ? 0 : x->sum ) node *new_node(int key){ node *x = new node; x->key = key; x->sum = key; x->pri = rand(); x->L = x->R = NULL; x->siz = 1; return x; } node *copy(node *x){ node *y = new node; y->key = x->key; y->siz = x->siz; y->pri = x->pri; y->sum = x->sum; y->L = x->L; y->R = x->R; return y; } void pushup(node *x){ x->siz = siz(x->L) + siz(x->R) + 1; x->sum = sum(x->L) + sum(x->R) + x->key; } node *merge(node *x,node *y){ if ( x == NULL ) return y; if ( y == NULL ) return x; if( x->pri < y->pri ){ node *t = copy(x); t->R = merge(t->R,y); pushup(t); return t; }else{ node *t =copy(y); t->L = merge(x,t->L); pushup(t); return t; } } typedef pair<node*,node*>pii; pii split(node *x,int k){ if ( x == NULL ) return pii(NULL,NULL); if ( k <= 0 ) return pii(NULL,x); node *t = copy(x); if( siz(x->L) >= k ){ pii tmp = split(t->L,k); t->L = tmp.second; pushup(t); return pii(tmp.first,t); } else { pii tmp = split(t->R,k-siz(t->L)-1); t->R = tmp.first; pushup(t); return pii(t,tmp.second); } } int find(node *x,int key){ int ans = 0; while( x != NULL ){ if( x->key <= key ) { ans += siz(x->L) + 1; x = x->R; } else x = x->L; } return ans; } void order(node *x){ if( x == NULL ) return; order(x->L); cout<<x->key<<" "; order(x->R); } void ins(node *x,node *&y,int key){ int k = find(x,key); pii tmp = split(x,k); node *t = new_node(key); y = merge(tmp.first,t); y = merge(y,tmp.second); } void del(node *x,node *&y,int key){ int k = find(x,key); pii tmp1 = split(x,k); pii tmp2 = split(tmp1.first,k-1); y = merge(tmp2.first,tmp1.second); } LL query(node *x,int k){ LL ans = 0; while( x != NULL ) { if( siz(x->L) == k ) { ans += sum(x->L); return ans; } else if( siz(x->L)+1 == k) { ans += sum(x->L)+x->key; return ans;} else if( siz(x->L) > k ) { x = x->L; } else {ans += sum(x->L)+x->key; k -=(siz(x->L)+1); x=x->R;} } return ans; } node *root[maxn]; struct opt{ int key,pos,fg; }a[maxn<<1]; bool cmp(opt x,opt y){ return x.pos<y.pos; } int n,m; int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ int s,e,p,j=i+n; scanf("%d%d%d",&s,&e,&p);e++; a[i].key=p;a[i].fg=1;a[i].pos=s; a[j].key=p;a[j].fg=-1;a[j].pos=e; } sort(a+1,a+1+n+n,cmp); root[0]=NULL; for(int i=1,t=1;i<=n+n;i++){ for(;t<a[i].pos;t++)root[t]=root[t-1]; int d=a[i].key; if(a[i].fg>0)ins(root[t-1],root[t],a[i].key); else del(root[t-1],root[t],a[i].key); i++; while(a[i].pos==a[i-1].pos){ if(a[i].fg>0)ins(root[t],root[t],a[i].key); else del(root[t],root[t],a[i].key); i++; } t++;i--; } LL pre=1; for(int i=1;i<=m;i++){ int X,A,B,C; scanf("%d%d%d%d",&X,&A,&B,&C); int K=1+(A%C*pre%C+B%C)%C; LL ans = query(root[X],K); printf("%lld\n",ans); pre = ans; } }
相关文章推荐
- 开心的金明
- xps13 关机充电 右边的usb口
- android layout_gravity gravity 失效的问题
- int、long、String类型数据与字节数组转换
- C:变量
- 金明的预算方案(有依赖的背包问题)
- 金明的预算方案
- [Angularjs]自定义指令
- JAVA Math 类
- spring websocket
- 砝码称重
- AXIS2的简单实例
- 关于子自己近期现状的一点想法及安排
- 遥感数据免费获取网址
- 砝码称重
- delphi 修改代码补全的快捷键(由Ctrl+Space 改为 Ctrl + alt + Space)
- jquery选择器效率优化问题
- Pass-Muraille
- LeetCode 268 -Missing Number ( JAVA )
- Deeplearning ,AI,Machine Learning 的关系