标签:out void closed mic tor return lag query main
给出一个序列,每次查询区间第k小
整体二分入门题?
1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 //by NeighThorn 6 #define inf 0x3f3f3f3f 7 using namespace std; 8 9 const int maxn=100000+5,maxm=5000+5; 10 11 int n,m,a[maxn],ans[maxm],tr[maxn]; 12 13 struct M{ 14 int x,y,k,id,flag; 15 M(int a=0,int b=0,int c=0,int d=0,int e=0){ 16 x=a,y=b,k=c,id=d,flag=e; 17 } 18 }q[maxm+maxn],q1[maxm+maxn],q2[maxm+maxn]; 19 20 inline void add(int x,int y){ 21 for(;x<=n;x+=x&(-x)) 22 tr[x]+=y; 23 } 24 25 inline int query(int x){ 26 int res=0; 27 for(;x;x-=x&(-x)) 28 res+=tr[x]; 29 return res; 30 } 31 32 inline void solve(int L,int R,int l,int r){ 33 if(L>R) 34 return; 35 if(l==r){ 36 for(int i=L;i<=R;i++) 37 if(q[i].flag==2) 38 ans[q[i].id]=l; 39 return; 40 } 41 int mid=(l+r)>>1,l1=0,l2=0; 42 for(int i=L;i<=R;i++){ 43 if(q[i].flag==1){ 44 if(q[i].x<=mid) 45 add(q[i].id,1),q1[l1++]=q[i]; 46 else 47 q2[l2++]=q[i]; 48 } 49 else{ 50 int lala=query(q[i].y)-query(q[i].x-1); 51 if(lala>=q[i].k) 52 q1[l1++]=q[i]; 53 else 54 q[i].k-=lala,q2[l2++]=q[i]; 55 } 56 } 57 for(int i=0;i<l1;i++) 58 if(q1[i].flag==1) 59 add(q1[i].id,-1); 60 memcpy(q+L,q1,sizeof(q[0])*l1); 61 memcpy(q+L+l1,q2,sizeof(q[0])*l2); 62 solve(L,L+l1-1,l,mid);solve(L+l1,R,mid+1,r); 63 } 64 65 signed main(void){ 66 while(scanf("%d%d",&n,&m)!=EOF){ 67 memset(tr,0,sizeof(tr)); 68 for(int i=1;i<=n;i++){ 69 scanf("%d",&a[i]); 70 q[i]=(M){a[i],1,0,i,1}; 71 } 72 for(int i=1;i<=m;i++) 73 scanf("%d%d%d",&q[i+n].x,&q[i+n].y,&q[i+n].k),q[i+n].id=i,q[i+n].flag=2; 74 solve(1,n+m,-inf,inf); 75 for(int i=1;i<=m;i++) 76 printf("%d\n",ans[i]); 77 } 78 return 0; 79 }
和上一题一样,就是把修改操作拆成删除和插入操作...
1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 //by NeighThorn 6 #define inf 0x3f3f3f3f 7 using namespace std; 8 9 const int maxn=10000+5; 10 11 int n,m,cnt,tot,tr[maxn],pre[maxn],ans[maxn]; 12 13 char ch[3]; 14 15 struct M{ 16 int x,y,k,id,flag; 17 }q[maxn*4],q1[maxn*4],q2[maxn*4]; 18 19 inline int read(void){ 20 char ch=getchar();int f=1,x=0; 21 while(!(ch>=‘0‘&&ch<=‘9‘)){ 22 if(ch==‘-‘) 23 f=-1; 24 ch=getchar(); 25 } 26 while(ch>=‘0‘&&ch<=‘9‘) 27 x=x*10+ch-‘0‘,ch=getchar(); 28 return x; 29 } 30 31 inline void insert(int x,int y){ 32 for(;x<=n;x+=x&(-x)) 33 tr[x]+=y; 34 } 35 36 inline int query(int x){ 37 int res=0; 38 for(;x;x-=x&(-x)) 39 res+=tr[x]; 40 return res; 41 } 42 43 inline void solve(int L,int R,int l,int r){ 44 if(L>R) 45 return; 46 if(l==r){ 47 for(int i=L;i<=R;i++) 48 if(q[i].flag==2) 49 ans[q[i].id]=l; 50 return; 51 } 52 int mid=(l+r)>>1,l1=0,l2=0; 53 for(int i=L;i<=R;i++){ 54 if(q[i].flag==1){ 55 if(q[i].x<=mid) 56 insert(q[i].id,q[i].y),q1[l1++]=q[i]; 57 else 58 q2[l2++]=q[i]; 59 } 60 else{ 61 int lala=query(q[i].y)-query(q[i].x-1); 62 if(lala>=q[i].k) 63 q1[l1++]=q[i]; 64 else 65 q[i].k-=lala,q2[l2++]=q[i]; 66 } 67 } 68 for(int i=0;i<l1;i++) 69 if(q1[i].flag==1) 70 insert(q1[i].id,-q1[i].y); 71 memcpy(q+L,q1,sizeof(q[0])*l1); 72 memcpy(q+L+l1,q2,sizeof(q[0])*l2); 73 solve(L,L+l1-1,l,mid);solve(L+l1,R,mid+1,r); 74 } 75 76 signed main(void){ 77 n=read(),m=read();tot=n; 78 for(int i=1;i<=n;i++) 79 pre[i]=read(),q[i].x=pre[i],q[i].y=1,q[i].k=0,q[i].id=i,q[i].flag=1; 80 for(int i=1,x,y;i<=m;i++){ 81 scanf("%s",ch);tot++; 82 if(ch[0]==‘Q‘) 83 q[tot].x=read(),q[tot].y=read(),q[tot].k=read(),q[tot].id=++cnt,q[tot].flag=2; 84 else 85 x=read(),y=read(),q[tot].x=pre[x],q[tot].y=-1,q[tot].k=0,q[tot].id=x,q[tot].flag=1, 86 pre[x]=y,q[++tot].x=y,q[tot].y=1,q[tot].k=0,q[tot].id=x,q[tot].flag=1; 87 } 88 solve(1,tot,0,inf); 89 for(int i=1;i<=cnt;i++) 90 printf("%d\n",ans[i]); 91 return 0; 92 }//Cap ou pas cap. Cap.
标签:out void closed mic tor return lag query main
原文地址:http://www.cnblogs.com/neighthorn/p/6259917.html