思路:

RT

可以看VFK的题解

我写了半天拍了半天...

不过是$nlog^2n$的

要写垃圾回收的

线段树 如果某个节点的sum是0  也可以free掉

//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=;
int root[],n,q,xx,yy,zz,Root,ans;
struct Segment_Tree{
int rubbish[],top,tree[],lson[],rson[];
int get_rubbish(){if(rubbish[top])return rubbish[top--];return top--;}
void insert(int l,int r,int &pos,int num,int wei){
if(!pos)pos=get_rubbish();
if(l==r){tree[pos]+=wei;return;}
int mid=(l+r)>>;
if(mid>=num)insert(l,mid,lson[pos],num,wei);
else insert(mid+,r,rson[pos],num,wei);
tree[pos]=tree[lson[pos]]+tree[rson[pos]];
if(!tree[lson[pos]]&&lson[pos])rubbish[++top]=lson[pos],lson[pos]=;
if(!tree[rson[pos]]&&rson[pos])rubbish[++top]=rson[pos],rson[pos]=;
}
void merge(int x,int y,int &k){
if(!k)k=get_rubbish();
tree[k]=tree[x]+tree[y];
if(lson[x]||lson[y])merge(lson[x],lson[y],lson[k]);
if(rson[x]||rson[y])merge(rson[x],rson[y],rson[k]);
}
void dfs(int l,int r,int x){
int mid=(l+r)>>;
if(lson[x])dfs(l,mid,lson[x]),rubbish[++top]=lson[x];
if(rson[x])dfs(mid+,r,rson[x]),rubbish[++top]=rson[x];
lson[x]=rson[x]=tree[x]=;
}
}segtree;
int top1,top2,stk1[],stk2[];
struct SC_TREE{
int top,rubbish[],lson[],rson[],wei[],size[];
int get_rubbish(){return rubbish[top--];}
void build(int &k,int l,int r){
if(l>r)return;
if(!k)k=get_rubbish();
int mid=(l+r)>>;wei[k]=stk2[mid];
if(l==r){segtree.insert(,N,root[k],stk2[l],),size[k]=;return;}
build(lson[k],l,mid-),build(rson[k],mid+,r);
segtree.merge(root[lson[k]],root[rson[k]],root[k]);
segtree.insert(,N,root[k],stk2[mid],);
size[k]=size[lson[k]]+size[rson[k]]+;
}
void init(){for(int i=;i<=;i++)rubbish[i]=-i+;top=;segtree.top=,build(Root,,n);}
int find_wei(int x,int sz){
if(size[lson[x]]+==sz)return wei[x];
else if(size[lson[x]]>=sz)return find_wei(lson[x],sz);
else return find_wei(rson[x],sz-size[lson[x]]-);
}
void change(int x,int sz,int w,int last_w){
segtree.insert(,N,root[x],last_w,-);
segtree.insert(,N,root[x],w,);
if(size[lson[x]]+==sz){wei[x]=w;return;}
else if(size[lson[x]]>=sz)change(lson[x],sz,w,last_w);
else change(rson[x],sz-size[lson[x]]-,w,last_w);
}
void query(int l,int r,int x){
int L=size[lson[x]],R=size[x];
if(l==&&R==r){stk1[++top1]=root[x];return;}
if(l<=L+&&r>=L+)stk2[++top2]=wei[x];
if(r<=L)query(l,r,lson[x]);
else if(l>L+)query(l-L-,r-L-,rson[x]);
else{
if(l<=L)query(l,L,lson[x]);
if(r>L+)query(,r-L-,rson[x]);
}
}
int Query(int l,int r,int kth){
top1=top2=;query(l,r,Root);
int L=,R=N,ans=,M,temp;
while(L<R){
M=(L+R)>>;temp=;
for(int i=;i<=top1;i++)temp+=segtree.tree[segtree.lson[stk1[i]]];
for(int i=;i<=top2;i++)if(stk2[i]<=M&&stk2[i]>=L)temp++;
if(temp>kth){for(int i=;i<=top1;i++)stk1[i]=segtree.lson[stk1[i]];R=M;}
else {for(int i=;i<=top1;i++)stk1[i]=segtree.rson[stk1[i]];L=M+;kth-=temp;}
}return L;
}
void dfs(int x){
segtree.dfs(,N,root[x]);root[x]=;
if(lson[x])dfs(lson[x]),rubbish[++top]=lson[x];
stk2[++top2]=wei[x];
if(rson[x])dfs(rson[x]),rubbish[++top]=rson[x];
lson[x]=rson[x]=wei[x]=size[x]=;
}
int rebuild(int sz,int &x){top2=,dfs(x),build(x,,sz);}
bool insert(int &x,int sz,int w){
if(!x){x=get_rubbish();segtree.insert(,N,root[x],w,);size[x]=;wei[x]=w;return ;}
segtree.insert(,N,root[x],w,),size[x]++;
if(size[lson[x]]>=sz){if(insert(lson[x],sz,w))rebuild(size[lson[x]],lson[x]);}
else if(insert(rson[x],sz-size[lson[x]]-,w))rebuild(size[rson[x]],rson[x]);
if(max(size[lson[x]],size[rson[x]])>0.666*size[x])return ;return ;
}
}SC_Tree;
int read(){
int x=;char p=getchar();
while(p<''||p>'')p=getchar();
while(p>=''&&p<='')x=x*+p-'',p=getchar();
return x;
}
char readchar(){char p=getchar();while(p!='Q'&&p!='M'&&p!='I')p=getchar();return p;}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&stk2[i]);
SC_Tree.init();
scanf("%d",&q);
while(q--){
char op=readchar();xx=read()^ans,yy=read()^ans;
if(op=='Q')zz=read()^ans,printf("%d\n",ans=SC_Tree.Query(xx,yy,zz-));
else if(op=='M')SC_Tree.change(Root,xx,yy,SC_Tree.find_wei(Root,xx));
else SC_Tree.insert(Root,xx-,yy);
}
}

BZOJ 3065 替罪羊树+动态开节点线段树的更多相关文章

  1. 洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)

    题意 题目链接 Sol 树链剖分板子 + 动态开节点线段树板子 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...

  2. 洛谷P3120 [USACO15FEB]牛跳房子(动态开节点线段树)

    题意 题目链接 Sol \(f[i][j]\)表示前\(i\)行\(j\)列的贡献,转移的时候枚举从哪里转移而来,复杂度\(O(n^4)\) 然后考虑每一行的贡献,动态开节点线段树维护一下每种颜色的答 ...

  3. BZOJ4636: 蒟蒻的数列(动态开节点线段树)

    题意 题目链接 Sol 直接上动态开节点线段树 因为只有一次询问,所以中途不需要下传标记 #include<bits/stdc++.h> #define LL long long usin ...

  4. 洛谷P3960 列队(动态开节点线段树)

    题意 题目链接 Sol 看不懂splay..,看不懂树状数组... 只会暴力动态开节点线段树 观察之后不难发现,我们对于行和列需要支持的操作都是相同的:找到第\(k\)大的元素并删除,在末尾插入一个元 ...

  5. Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树

    思路: (我也不知道这是不是正解) ST表预处理出来原数列的两点之间的min 再搞一个动态开节点线段树 节点记录ans 和标记 lazy=-1 当前节点的ans可用  lazy=0 没被覆盖过 els ...

  6. 洛谷P4632 [APIO2018] New Home 新家(动态开节点线段树 二分答案 扫描线 set)

    题意 题目链接 Sol 这题没有想象中的那么难,但也绝对不简单. 首先把所有的询问离线,按照出现的顺序.维护时间轴来处理每个询问 对于每个询问\((x_i, y_i)\),可以二分答案\(mid\). ...

  7. [bzoj 3531][SDOI2014]旅行(树链剖分+动态开点线段树)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3531 分析: 对于每个颜色(颜色<=10^5)都建立一颗线段树 什么!那么不是M ...

  8. BZOJ 3531 [Sdoi2014]旅行 树链剖分+动态开点线段树

    题意 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. 为了方便,我们用 ...

  9. BZOJ 3531: [Sdoi2014]旅行 (树剖+动态开点线段树)

    对于每种信仰维护一棵动态开点线段树就行了- #include <cstdio> #include <cctype> #include <cstring> #incl ...

随机推荐

  1. DataTable如何去除重复的行

    两种方法1 数据库直接去除重复select distinct * from 表名去除了重复行distinct 2 对 DataTable直接进行操作DataTable dt=db.GetDt(&quo ...

  2. VHDL之concurrent之block

    1 Simple BLOCK The simple block represents only a way of partitioning the code. It allows concurrent ...

  3. 经典实用SQL Server语句大全总结(一)

    简要介绍基础语句:1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创 ...

  4. python 生成测试报告并发送邮件

    前言: 使用unittest编写自动化测试脚本,执行脚本后可以很方便看到测试用例的执行情况. 但如果想向领导汇报工作,就需要提供更直观的测试报告. 思路: 使用unittest编写测试用例,HTMLT ...

  5. Day 18 hashlib,logging模块

    hashlib 模块 作用:hash是一种算法,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法,该算法接受传入的内容,经过运算得到一串hash值 特点: 1.只要 ...

  6. Mysql [Err] 1118 - Row size too large

    首先声明,对MySQL不懂,很多都不知道原因 设计了一个表,里面很多text字段,然后填进去的东西太多(用的是Python的MySQLdb),报错: _mysql_exceptions.Operati ...

  7. Pytorch基础(5)——批数据训练

    一.知识点: 相关包:torch.utils.data import torch import torch.utils.data as Data 包装数据类:TensorDataset [包装数据和目 ...

  8. [luogu2474 SCOI2008]天平(floyd差分约束)

    传送门 Solution 由于重量只有三种情况,那么想到用差分约束. 由于范围比较小,想到可以floyed求差分约束,暴力求天平另一边 Code #include <cstdio> #in ...

  9. 【4】Django 创建第一个模块应用

    大成若缺,其用不弊.大盈若冲,其用不穷.大直若屈.大巧若拙.大辩若讷.静胜躁,寒胜热.清静为天下正 --老子<道德经> 本章内容 创建应用(app) 开发第一个视图(View) URL访问 ...

  10. git常见问题总结

    1.每次上传文件的时候,有很多iml文件容易不小心上传上去,然后报错,所以可以把这些文件取消上传 如图所示,每次提交时,都不会显示标红文件 具体操作步骤如下: