BZOJ 3065 替罪羊树+动态开节点线段树
思路:
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 替罪羊树+动态开节点线段树的更多相关文章
- 洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)
题意 题目链接 Sol 树链剖分板子 + 动态开节点线段树板子 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...
- 洛谷P3120 [USACO15FEB]牛跳房子(动态开节点线段树)
题意 题目链接 Sol \(f[i][j]\)表示前\(i\)行\(j\)列的贡献,转移的时候枚举从哪里转移而来,复杂度\(O(n^4)\) 然后考虑每一行的贡献,动态开节点线段树维护一下每种颜色的答 ...
- BZOJ4636: 蒟蒻的数列(动态开节点线段树)
题意 题目链接 Sol 直接上动态开节点线段树 因为只有一次询问,所以中途不需要下传标记 #include<bits/stdc++.h> #define LL long long usin ...
- 洛谷P3960 列队(动态开节点线段树)
题意 题目链接 Sol 看不懂splay..,看不懂树状数组... 只会暴力动态开节点线段树 观察之后不难发现,我们对于行和列需要支持的操作都是相同的:找到第\(k\)大的元素并删除,在末尾插入一个元 ...
- Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树
思路: (我也不知道这是不是正解) ST表预处理出来原数列的两点之间的min 再搞一个动态开节点线段树 节点记录ans 和标记 lazy=-1 当前节点的ans可用 lazy=0 没被覆盖过 els ...
- 洛谷P4632 [APIO2018] New Home 新家(动态开节点线段树 二分答案 扫描线 set)
题意 题目链接 Sol 这题没有想象中的那么难,但也绝对不简单. 首先把所有的询问离线,按照出现的顺序.维护时间轴来处理每个询问 对于每个询问\((x_i, y_i)\),可以二分答案\(mid\). ...
- [bzoj 3531][SDOI2014]旅行(树链剖分+动态开点线段树)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3531 分析: 对于每个颜色(颜色<=10^5)都建立一颗线段树 什么!那么不是M ...
- BZOJ 3531 [Sdoi2014]旅行 树链剖分+动态开点线段树
题意 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. 为了方便,我们用 ...
- BZOJ 3531: [Sdoi2014]旅行 (树剖+动态开点线段树)
对于每种信仰维护一棵动态开点线段树就行了- #include <cstdio> #include <cctype> #include <cstring> #incl ...
随机推荐
- webapi部署到IIS 404错误
环境:win2008r2+IIS 解决方案: 添加一个映射 可执行文件地址(根据系统决定64位可32位): C:\Windows\Microsoft.NET\Framework64\v4.0.3031 ...
- HTML5 video常用属性
一.视频video常用标签方法 <!-- controls 控制条,播放暂停等 controlslist 控制不允许全屏 不允许下载等 poster 封面 autoplay 自动播放 muted ...
- Js构造对象-添加方法的三种方式
Js构造函数添加方法有多种方案,来看一个混合方式构造函数的例子:申明person构造函数,有两个属性,name,qq.在原型上添加方法showname.这是最常用的方法. <script> ...
- Webpack 快速上手(下)
杏仁前端开发工程师,代码洁癖症早期,关注前端技术. 由于文章篇幅较长,为了更好的阅读体验,本文分为上.中.下三篇: 上篇介绍了什么是 webpack,为什么需要 webpack,webpack 的文件 ...
- C#第八节课
for的穷举迭代.while.do while using System;using System.Collections.Generic;using System.Linq;using Syste ...
- python的多版本安装以及常见错误(长期更新)
(此文长期更新)Python安装常见错误汇总 注:本教程以python3.6为基准 既然是总结安装过程中遇到的错误,就顺便记录一下我的安装过程好了. 先来列举一下安装python3.6过程中可能需要的 ...
- android 手机网络接入点名称及WAP、NET模式的区别
移动 电信 联通 APN cmwap cmnet ctwap ctnet 3gwap uniwap 3gnet uninet设置 APN(Access Point Name),即“接入点名称”,用来标 ...
- 通过请求接口的办法获得本设备IP以及IP地址
获取本设备IP接口(搜狐) http://pv.sohu.com/cityjson?ie=utf-8 result Content: { "cip": "58.21 ...
- BZOJ 4244 邮戳拉力赛 (DP)
手动博客搬家: 本文发表于20181211 18:01:21, 原地址https://blog.csdn.net/suncongbo/article/details/84957907 为了防止我的博客 ...
- Java基础学习总结(68)——有关Java线程方面的面试题
不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java 语言一个重要的特点就是内置了对并发的支持,让 Java 大受企业和程序员的欢迎.大多数待遇丰厚的 Java 开发职位都要求开发者 ...