复杂度是n*log(s)*log(n)
#include
#include
#include
#include
#include
using namespace std;
#define maxn 300007
int tree[maxn];
void add(int p,int n){
for(;p0;p-=p&(-p))
ans += tree[p];
return ans;
}
struct Node{
int l,r,k,ty,ans;
};
Node p[maxn];
int id1[maxn],id2[maxn];
void CDQ(int L,int R,int low,int high){
if(R < L) return ;
if(low == high ){
for(;L<=R;L++){
p[id1[L]].ans = low;
}
return ;
}
int mid = (low+high)/2,l=L,r=R,k,u;
for(int i = L;i <= R; i++){
u = id1[i];
if(p[u].ty == 2){
k = query(p[u].r) - query(p[u].l-1);
if(k >= p[u].k) id2[l++] = u;
else {
p[u].k -= k;
id2[r--] = u;
}
}
else if(p[u].k <= mid){
add(p[u].l,p[u].ty);
id2[l++] = u;
}
else id2[r--] = u;
}
for(int i = L; i <= R; i++){
u = id1[i];
if(p[u].ty != 2 && p[u].k <= mid) add(p[u].l,-p[u].ty);
}
for(k=L;k
#include
#include
#include
#include
#include
#include
#define MAXN 100002
using namespace std;
using namespace __gnu_pbds;
struct query{int t,l,r,k;}q[MAXN];
//保存询问
tree,rb_tree_tag,tree_order_statistics_node_update>bit[MAXN*2];
tree,rb_tree_tag,tree_order_statistics_node_update>::iterator it;
//exSTL红黑树,G++ Only,低版本中null_type为null_mapped_type
int num[MAXN],n,ql,qr,k,oper,lsh[MAXN*2],H,ct;
//lsh为离散化数组 ,H为贪心构造答案的最高位
int lowbit(int x){return x&(-x);}
void add(int p,int num){
for(int i=p;i<=ct;i+=lowbit(i))
bit[i].insert(num);
}
//树状数组更新
void del(int p,int num){
for(int i=p;i<=ct;i+=lowbit(i))
bit[i].erase(num);
}
//树状数组删除
int pos(int num){return lower_bound(lsh,lsh+ct,num)-lsh+1;}
// 返回离散化下标
int query(int l,int r,int k){
int num=0;
for(int i=H;i;i>>=1){ //高位往地位贪心
int tmp=num+i;
if(tmp>ct)continue;
int kth=bit[tmp].order_of_key(r+1)-bit[tmp].order_of_key(l);
//平衡树查询
if(kth>=k)continue;
num=tmp,k-=kth;
}
return num;
}
//贪心构造答案
int main(){
int t;
while(scanf("%d",&n)!=EOF){
ct=0;
for(int i=1;i<=n;i++){
scanf("%d",&num[i]);
lsh[ct++]=num[i];
}
scanf("%d",&t);
for(int i=0;i
3.线段树套treap
//#pragma warning (disable:4786)
//#pragma comment(linker,"/STACK:102400000,102400000") //手动扩栈
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include