板子题,只是记得负数加fix最方便
#include <cstdio> const int A=19,N=100010; namespace FIFO { char ch,B[1<<20],*S=B,*T=B; #define getc() (S==T&&(T=(S=B)+fread(B,1,1<<20,stdin),S==T)?0:*S++) #define isd(c) (c>=‘0‘&&c<=‘9‘) int aa,bb;int F(){ while(ch=getc(),!isd(ch)&&ch!=‘-‘);ch==‘-‘?aa=bb=0:(aa=ch-‘0‘,bb=1); while(ch=getc(),isd(ch))aa=aa*10+ch-‘0‘;return bb?aa:-aa; } } #define gi FIFO::F() struct Trie{ Trie *ch[2];int size; }*root[N],*null,node[(1<<22)+10]; int n,m,sz=1; int main(){ freopen("kth.in","r",stdin);freopen("kth.out","w",stdout); null=node,null->ch[0]=null->ch[1]=null,root[0]=null; n=gi,m=gi;for(register int i=1,x;i<=n;i++){ root[i]=node+sz,sz++,x=gi;register Trie *p=root[i],*last=root[i-1]; for(register int i=A;i>=0;i--) p->ch[(x>>i)&1]=node+sz,sz++,p->ch[((x>>i)&1)^1]=last->ch[((x>>i)&1)^1], p=p->ch[(x>>i)&1],last=last->ch[(x>>i)&1],p->size=last->size+1; } register int x,y,k;while(m--){ x=gi,y=gi,k=gi;register Trie *a=root[x-1],*b=root[y];register int ret=0; for(register int i=A;i>=0;i--) if(b->ch[0]->size-a->ch[0]->size>=k)a=a->ch[0],b=b->ch[0]; else ret|=(1<<i),k-=b->ch[0]->size-a->ch[0]->size,a=a->ch[1],b=b->ch[1]; printf("%d\n",ret); } }
【COGS 930】 [河南省队2012] 找第k小的数
#include <cstdio> const int A=30,N=100010,fox=1000000000; namespace FIFO { char ch,B[1<<20],*S=B,*T=B; #define getc() (S==T&&(T=(S=B)+fread(B,1,1<<20,stdin),S==T)?0:*S++) #define isd(c) (c>=‘0‘&&c<=‘9‘) int aa,bb;int F(){ while(ch=getc(),!isd(ch)&&ch!=‘-‘);ch==‘-‘?aa=bb=0:(aa=ch-‘0‘,bb=1); while(ch=getc(),isd(ch))aa=aa*10+ch-‘0‘;return bb?aa:-aa; } } #define gi FIFO::F() struct Trie{ Trie *ch[2];int size; }*root[N],*null,node[(1<<22)+10]; int n,m,sz=1; int main(){ freopen("kthnumber.in","r",stdin);freopen("kthnumber.out","w",stdout); null=node,null->ch[0]=null->ch[1]=null,root[0]=null; n=gi,m=gi;for(register int i=1,x;i<=n;i++){ root[i]=node+sz,sz++,x=gi+fox;register Trie *p=root[i],*last=root[i-1]; for(register int i=A;i>=0;i--) p->ch[(x>>i)&1]=node+sz,sz++,p->ch[((x>>i)&1)^1]=last->ch[((x>>i)&1)^1], p=p->ch[(x>>i)&1],last=last->ch[(x>>i)&1],p->size=last->size+1; } register int x,y,k;while(m--){ x=gi,y=gi,k=gi;register Trie *a=root[x-1],*b=root[y];register int ret=0; for(register int i=A;i>=0;i--) if(b->ch[0]->size-a->ch[0]->size>=k)a=a->ch[0],b=b->ch[0]; else ret|=(1<<i),k-=b->ch[0]->size-a->ch[0]->size,a=a->ch[1],b=b->ch[1]; printf("%d\n",ret-fox); } }
【COGS 1534】 [NEERC 2004]K小数
时间: 2024-10-13 02:40:31