BZOJ 4923: [Lydsy1706月赛]K小值查询 Splay + 思维
Description
Input
Output
感觉怎么做都没什么思路.
学长说正解是分类讨论.
还真是分类讨论...
可以将所有数分为两种:小于等于 $k$ 的,大于 $k$ 的.
前一部分的数值是不变的,大于 $k$ 的数值都要变.
而我们还可以把大于 $k$ 的再分成大于 $2k$ 的和 $k<=a_{i}<=2k.$
而将 $a_{i}\in[k,2k]$ 的数减掉 $k$ 就相当于至少 / 2.
因为没有加法操作,所以每一个数至多会减少 $log(10^9)$ 次.
所以可以每一次用 $splay$ 将 $a_{i}\in[k,2k]$ 之间的数字提取出来,直接暴力减掉 $k$ 并重新插进平衡树.
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxn=100003;
- namespace IO {
- void setIO(string s) {
- string in=s+".in";
- string out=s+".out";
- freopen(in.c_str(),"r",stdin);
- freopen(out.c_str(),"w",stdout);
- }
- char *p1,*p2,buf[100000];
- #define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
- int rd() {
- int x=0;
- char c=nc();
- while(c<48) c=nc();
- while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc();
- return x;
- }
- };
- int root;
- int arr[maxn];
- struct Splay {
- #define lson ch[x][0]
- #define rson ch[x][1]
- stack<int>S;
- int cnt,tp;
- int ch[maxn][2],f[maxn],val[maxn],siz[maxn],tag[maxn],key[maxn];
- void init() {
- for(int i=1;i<maxn;++i) S.push(i);
- }
- int newnode() {
- int x=S.top();S.pop();
- return x;
- }
- void mark(int x,int d) {
- val[x]-=d, tag[x]+=d;
- }
- int get(int x) {
- return ch[f[x]][1]==x;
- }
- void pushup(int x) {
- siz[x]=siz[lson]+siz[rson]+1;
- }
- void pushdown(int x) {
- if(tag[x]) mark(lson,tag[x]),mark(rson,tag[x]),tag[x]=0;
- }
- void rotate(int x) {
- int old=f[x],fold=f[old],which=get(x);
- ch[old][which]=ch[x][which^1],f[ch[old][which]]=old;
- ch[x][which^1]=old,f[old]=x,f[x]=fold;
- if(fold) ch[fold][ch[fold][1]==old]=x;
- pushup(old),pushup(x);
- }
- void splay(int x,int &tar) {
- int u=f[tar],fa;
- for(;(fa=f[x])^u;rotate(x))
- if(f[fa]^u)
- rotate(get(fa)==get(x)?fa:x);
- tar=x;
- }
- int pre(int v) {
- int x=root,re=0;
- while(x) {
- pushdown(x);
- if(val[x]<=v) re=x,x=rson;
- else x=lson;
- }
- return re;
- }
- int nxt(int v) {
- int x=root,re=0;
- while(x) {
- pushdown(x);
- if(val[x]>=v) re=x,x=lson;
- else x=rson;
- }
- return re;
- }
- void build(int &x,int ff,int l,int r) {
- x=newnode();
- int mid=(l+r)>>1;
- f[x]=ff,val[x]=arr[mid],siz[x]=1;
- if(l<mid) build(lson,x,l,mid-1);
- if(r>mid) build(rson,x,mid+1,r);
- pushup(x);
- }
- int insert(int &x,int ff,int k) {
- if(!x) {
- x=newnode();
- f[x]=ff,val[x]=k,siz[x]=1;
- pushup(x);
- return x;
- }
- pushdown(x);
- int a=insert(ch[x][k>val[x]],x,k);
- pushup(x);
- return a;
- }
- void dfs(int x) {
- if(!x) return;
- pushdown(x);
- key[++tp]=val[x];
- if(lson) dfs(lson);
- if(rson) dfs(rson);
- S.push(x);
- ch[x][0]=ch[x][1]=f[x]=val[x]=siz[x]=tag[x]=0;
- }
- int kth(int k) {
- int x=root;
- while(1) {
- pushdown(x);
- if(k>siz[lson]) {
- k-=(siz[lson]+1);
- if(k==0) return val[x];
- else x=rson;
- }
- else x=lson;
- }
- }
- void solve(int k) {
- int pr=pre(k),nx=nxt(k<<1);
- if(!pr) mark(root,k);
- else if(!nx) {
- splay(pr,root);
- int a=ch[root][1];
- ch[root][1]=f[ch[root][1]]=0;
- pushup(root);
- tp=0;
- dfs(a);
- for(int i=1;i<=tp;++i) {
- int cc=insert(root,0,key[i]-k);
- if(i%6==0) splay(cc,root);
- }
- }
- else {
- splay(pr,root),splay(nx,ch[root][1]);
- int a=ch[ch[root][1]][0];
- ch[ch[root][1]][0]=0, pushup(ch[root][1]);
- tp=0, dfs(a);
- mark(ch[root][1], k);
- for(int i=1;i<=tp;++i) {
- int cc=insert(root,0,key[i]-k);
- if(i%6==0) splay(cc,root);
- }
- }
- }
- #undef lson
- #undef rson
- }tr;
- int main() {
- using namespace IO;
- // setIO("input");
- tr.init();
- int n,m;
- n=rd(),m=rd();
- for(int i=1;i<=n;++i) arr[i]=rd();
- sort(arr+1,arr+1+n);
- tr.build(root,0,1,n);
- for(int cas=1;cas<=m;++cas) {
- int op,k;
- op=rd(),k=rd();
- if(op==1) printf("%d\n",tr.kth(k));
- else tr.solve(k);
- }
- return 0;
- }
BZOJ 4923: [Lydsy1706月赛]K小值查询 Splay + 思维的更多相关文章
- [BZOJ 4923][Lydsy1706月赛]K小值查询
传送门 势能分析平衡树,splay或treap都可以 放个指针版的就跑 #include <bits/stdc++.h> using namespace std; #define rep( ...
- BZOJ4923:[Lydsy1706月赛]K小值查询(Splay)
Description 维护一个长度为n的正整数序列a_1,a_2,...,a_n,支持以下两种操作: 1 k,将序列a从小到大排序,输出a_k的值. 2 k,将所有严格大于k的数a_i减去k. In ...
- 4923: [Lydsy1706月赛]K小值查询 平衡树 非旋转Treap
国际惯例的题面:这种维护排序序列,严格大于的进行操作的题都很套路......我们按照[0,k],(k,2k],(2k,inf)分类讨论一下就好.显然第一个区间的不会变化,第二个区间的会被平移进第一个区 ...
- [BZ4923][Lydsy1706月赛]K小值查询
K小值查询 题面 维护一个长度为n的正整数序列a_1,a_2,...,a_n,支持以下两种操作: 1 k,将序列a从小到大排序,输出a_k的值. 2 k,将所有严格大于k的数a_i减去k. Input ...
- BZOJ4923 [Lydsy1706月赛]K小值查询
题意 维护一个长度为n的正整数序列a_1,a_2,...,a_n,支持以下两种操作: 1 k,将序列a从小到大排序,输出a_k的值. 2 k,将所有严格大于k的数a_i减去k. \(n \leq 10 ...
- [bzoj4923]K小值查询
来自FallDream的博客,未经允许,请勿转载,谢谢. 维护一个长度为n的正整数序列a_1,a_2,...,a_n,支持以下两种操作: 1 k,将序列a从小到大排序,输出a_k的值. 2 k,将所有 ...
- BZOJ4923 K小值查询(splay)
容易想到建一棵平衡树,修改时打上标记即可.但是修改会导致平衡树结构被破坏.注意到实际上只有[k+1,2k)这一部分数在平衡树中的位置会被改变,所以对这一部分暴力修改,因为每次都会使其至少减小一半,复杂 ...
- 【BZOJ】3065: 带插入区间K小值
http://www.lydsy.com/JudgeOnline/problem.php?id=3065 题意:带插入.修改的区间k小值在线查询.(原序列n<=35000, 询问<=175 ...
- bzoj 3065: 带插入区间K小值 替罪羊树 && AC300
3065: 带插入区间K小值 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 1062 Solved: 253[Submit][Status] Des ...
随机推荐
- Elasticsearch如何关掉服务
1.使用head插件找到想关掉的节点进行关停 2.使用命令kill杀掉服务器的ES进程即可1.查找ES进程ps -ef | grep elastic 2.杀掉ES进程kill -9 2382(进程号) ...
- mysql安装及相关配置
安装下载 第一种 安装mysql安装包 //www.jb51.net/softs/451120.html 保存root密码 打开系统偏好设置,start mysql server #配置mysql e ...
- LeetCode.876-链表的中间节点(Middle of the Linked List)
这是悦乐书的第337次更新,第361篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第206题(顺位题号是876).给定具有头节点的非空单链表,返回链表的中间节点.如果有两 ...
- lsb-realse
[root@localhost ~]# lsb_release -a -bash: lsb_release: command not found 解决方法:yum install redhat-lsb ...
- kafka学习(四)
集群成员关系 kafka使用Zookeeper 来维护集群成员的信息.每个broker都有一个唯一标识符,这个标识符可以在配置里指定,也可以自动生成.在broker启动的时候,它通过创建临时节点把自己 ...
- 关于cors 跨域的一些问题
system.webServer节点写配置 <httpProtocol> <customHeaders> <add name="Access-Control-A ...
- python+selenium模拟鼠标操作
from selenium.webdriver.common.action_chains import ActionChains #导入鼠标相关的包 ------------------------- ...
- 华南理工大学 “三七互娱杯” G HRY and tree
https://ac.nowcoder.com/acm/contest/874/G 题目大意:对于一个连通图,现在定义两个点的贡献为连接两点的路径上最大的权值 求任意两个点贡献的和 这个题看懂花了我很 ...
- 函数 FUNCTION
函数 FUNCTION 定义: 带名字的代码块,用于执行具体任务. 注意: 应给函数指定描述性名称,只是用小写字母和下划线. 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 (). 任何 ...
- Qfile
打开方式: void AddStudents::write_to_file(QString src){ QFile file("stu.txt"); if (!file.open( ...