思路:

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. NLP:单词嵌入Word Embeddings

    深度学习.自然语言处理和表征方法 原文链接:http://blog.jobbole.com/77709/ 一个感知器网络(perceptron network).感知器 (perceptron)是非常 ...

  2. 【sqli-labs】 less2 GET - Error based - Intiger based (基于错误的GET整型注入)

    与less1相同,直接走流程 提交参数,直接order by http://localhost/sqli/Less-2/?id=1 order by 1%23 http://localhost/sql ...

  3. Generics of a Higher Kind

    http://adriaanm.github.io/files/higher.pdf https://www.atlassian.com/blog/archives/scala-types-of-a- ...

  4. java模拟Cookies登陆

    在使用java访问URL时,如果该URL需要身份验证,那么就不能够直接访问,因为没有登陆.那么,如何解决这个问题呢? 方法是使用java模拟登陆,登陆后记录下cookie信息,在下次发起请求时时将co ...

  5. 整理Webview加载缓慢的解决方案

    1.https://www.cnblogs.com/xinye/p/3144139.html 2.https://www.jianshu.com/p/95d4d73be3d1

  6. 【剑指Offer】46、圆圈中最后剩下的数

      题目描述:   每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后 ...

  7. [置顶] 来自 Google 的高可用架构理念与实践

    转自:   https://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=402738153&idx=1&sn=af5e76aad ...

  8. Mysql入门详解

    目录 数据库之Mysql 一 .简单了解数据库 二.Mysql的使用 三.多表查询 数据库之Mysql 本篇文章为观看某教学视频后所作个人总结 一 .简单了解数据库 1.1常见关系型数据库 mysql ...

  9. 高举 Vue-SSR

    将同一个组件渲染为服务器端的 HTML 字符串,将它们直接发送到浏览器,最后将静态标记"混合"为客户端上完全交互的应用程序. SSR的目的 To solve 首屏渲染问题 SEO问 ...

  10. 通过请求接口的办法获得本设备IP以及IP地址

    获取本设备IP接口(搜狐) http://pv.sohu.com/cityjson?ie=utf-8 result Content: {    "cip": "58.21 ...