标签:ret lap name 区间 oid names code 模板 splay
求区间第k大 整体二分模板提
// luogu-judger-enable-o2 #include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define ll long long #define see(x) (cerr<<(#x)<<‘=‘<<(x)<<endl) #define inf 0x3f3f3f3f #define CLR(A,v) memset(A,v,sizeof A) ///////////////////////////////////// const int N=1e6+10; int n,m,t[N],k,ans[N],cnt,a[N]; void add(int x,int v){for(;x<=N;x+=x&-x)t[x]+=v;} int qsum(int x){int ans=0;for(;x;x-=x&-x)ans+=t[x];return ans;} struct node{int l,r,k,op,id;}s[N],temp1[N],temp2[N]; void cdq(int l,int r,int L,int R) { if(L>R)return ; if(l==r) { rep(i,L,R)if(s[i].op==2)ans[s[i].id]=l; return ; } int mid=(l+r)>>1,cnt1=0,cnt2=0; rep(i,L,R) { if(s[i].op==1) { if(s[i].l<=mid)add(s[i].id,1),temp1[++cnt1]=s[i]; else temp2[++cnt2]=s[i]; } else { int x=qsum(s[i].r)-qsum(s[i].l-1); if(s[i].k<=x)temp1[++cnt1]=s[i]; else s[i].k-=x,temp2[++cnt2]=s[i]; } } rep(i,1,cnt1)if(temp1[i].op==1)add(temp1[i].id,-1); rep(i,1,cnt1)s[L-1+i]=temp1[i]; rep(i,1,cnt2)s[L+cnt1-1+i]=temp2[i]; cdq(l,mid,L,L+cnt1-1); cdq(mid+1,r,L+cnt1,R); } int main() { cin>>n>>m; rep(i,1,n) cnt++,scanf("%d",&s[cnt].l),s[cnt].r=1,s[cnt].k=0,s[cnt].id=i,s[cnt].op=1; rep(i,1,m) { int l,r,k;scanf("%d%d%d",&l,&r,&k); s[++cnt]=(node){l,r,k,2,i}; } cdq(-inf,inf,1,cnt); rep(i,1,n) printf("%d\n",ans[i]); return 0; }
标签:ret lap name 区间 oid names code 模板 splay
原文地址:https://www.cnblogs.com/bxd123/p/11466946.html