BZOJ2120&2453数颜色——线段树套平衡树(treap)+set/带修改莫队
题目描述
墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会像你发布如下指令: 1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。 2、 R P Col 把第P支画笔替换为颜色Col。为了满足墨墨的要求,你知道你需要干什么了吗?
输入
第1行两个整数N,M,分别代表初始画笔的数量以及墨墨会做的事情的个数。第2行N个整数,分别代表初始画笔排中第i支画笔的颜色。第3行到第2+M行,每行分别代表墨墨会做的一件事情,格式见题干部分。
输出
对于每一个Query的询问,你需要在对应的行中给出一个数字,代表第L支画笔到第R支画笔中共有几种不同颜色的画笔。
样例输入
1 2 3 4 5 5
Q 1 4
Q 2 6
R 1 2
Q 1 4
Q 2 6
样例输出
4
3
4
提示
对于100%的数据,N≤10000,M≤10000,修改操作不多于1000次,所有的输入数据中出现的所有整数均大于等于1且不超过10^6。
- #include<set>
- #include<map>
- #include<queue>
- #include<stack>
- #include<cmath>
- #include<cstdio>
- #include<bitset>
- #include<vector>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #define ll long long
- using namespace std;
- struct lty
- {
- int l,r,num,tim;
- }q[10010];
- struct miku
- {
- int x,y;
- }a[10010];
- int res;
- int v[1000010];
- int s[100010];
- int ans[100010];
- int num,cnt;
- int n,m;
- int block;
- char ch[20];
- bool cmp(lty a,lty b)
- {
- return (a.l/block==b.l/block)?(a.r==b.r?a.tim<b.tim:a.r<b.r):a.l<b.l;
- }
- void del(int x)
- {
- if(v[x]==1)
- {
- res--;
- }
- v[x]--;
- }
- void ins(int x)
- {
- if(!v[x])
- {
- res++;
- }
- v[x]++;
- }
- void change(int l,int r,int id)
- {
- if(a[id].x>=l&&a[id].x<=r)
- {
- if(--v[s[a[id].x]]==0)
- {
- res--;
- }
- if(++v[a[id].y]==1)
- {
- res++;
- }
- }
- swap(a[id].y,s[a[id].x]);
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- block=pow(n,2/3);
- for(int i=1;i<=n;i++)
- {
- scanf("%d",&s[i]);
- }
- for(int i=1;i<=m;i++)
- {
- scanf("%s",ch);
- if(ch[0]=='Q')
- {
- cnt++;
- scanf("%d%d",&q[cnt].l,&q[cnt].r);
- q[cnt].num=cnt,q[cnt].tim=num;
- }
- else
- {
- num++;
- scanf("%d%d",&a[num].x,&a[num].y);
- }
- }
- sort(q+1,q+1+cnt,cmp);
- int L=1,R=0,now=0;
- for(int i=1;i<=cnt;i++)
- {
- while(L>q[i].l)
- {
- L--;
- ins(s[L]);
- }
- while(R<q[i].r)
- {
- R++;
- ins(s[R]);
- }
- while(L<q[i].l)
- {
- del(s[L]);
- L++;
- }
- while(R>q[i].r)
- {
- del(s[R]);
- R--;
- }
- while(now<q[i].tim)
- {
- now++;
- change(q[i].l,q[i].r,now);
- }
- while(now>q[i].tim)
- {
- change(q[i].l,q[i].r,now);
- now--;
- }
- ans[q[i].num]=res;
- }
- for(int i=1;i<=cnt;i++)
- {
- printf("%d\n",ans[i]);
- }
- }
- #include<set>
- #include<map>
- #include<queue>
- #include<stack>
- #include<cmath>
- #include<cstdio>
- #include<vector>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #define ll long long
- using namespace std;
- int x,y;
- int n,m;
- int tot;
- int opt;
- char ch[3];
- int a[50010];
- int v[2000010];
- int w[2000010];
- int t[2000010];
- int l[500010];
- int r[500010];
- int ls[2000010];
- int rs[2000010];
- int pre[50010];
- int suf[50010];
- int size[2000010];
- int root[500010];
- map<int,int>b;
- set<int>s[1000010];
- set<int>::iterator it;
- int inbuild(int k)
- {
- tot++;
- t[tot]=rand();
- v[tot]=k;
- w[tot]=1;
- size[tot]=1;
- ls[tot]=rs[tot]=0;
- return tot;
- }
- void updata(int rt)
- {
- size[rt]=size[ls[rt]]+size[rs[rt]]+w[rt];
- }
- void lturn(int &rt)
- {
- int t=rs[rt];
- rs[rt]=ls[t];
- ls[t]=rt;
- updata(rt);
- updata(t);
- rt=t;
- }
- void rturn(int &rt)
- {
- int t=ls[rt];
- ls[rt]=rs[t];
- rs[t]=rt;
- updata(rt);
- updata(t);
- rt=t;
- }
- void insert(int &rt,int k)
- {
- if(!rt)
- {
- rt=inbuild(k);
- return ;
- }
- if(v[rt]==k)
- {
- w[rt]++;
- }
- else
- {
- if(k<v[rt])
- {
- insert(ls[rt],k);
- if(t[ls[rt]]<t[rt])
- {
- rturn(rt);
- }
- }
- else
- {
- insert(rs[rt],k);
- if(t[rs[rt]]<t[rt])
- {
- lturn(rt);
- }
- }
- }
- updata(rt);
- }
- void del(int &rt,int k)
- {
- if(v[rt]<k)
- {
- del(rs[rt],k);
- }
- else if(v[rt]>k)
- {
- del(ls[rt],k);
- }
- else
- {
- if(w[rt]>1)
- {
- w[rt]--;
- }
- else
- {
- if(!ls[rt]||!rs[rt])
- {
- rt=ls[rt]+rs[rt];
- }
- else
- {
- if(t[ls[rt]]<t[rs[rt]])
- {
- rturn(rt);
- del(rs[rt],k);
- }
- else
- {
- lturn(rt);
- del(ls[rt],k);
- }
- }
- }
- }
- if(rt)
- {
- updata(rt);
- }
- }
- int inrank(int rt,int k)
- {
- if(!rt)
- {
- return 0;
- }
- if(v[rt]==k)
- {
- return size[ls[rt]];
- }
- else if(v[rt]<k)
- {
- return size[ls[rt]]+w[rt]+inrank(rs[rt],k);
- }
- else
- {
- return inrank(ls[rt],k);
- }
- }
- void outbuild(int rt,int L,int R)
- {
- l[rt]=L;
- r[rt]=R;
- for(int i=L;i<=R;i++)
- {
- insert(root[rt],pre[i]);
- }
- if(L!=R)
- {
- int mid=(L+R)>>1;
- outbuild(rt<<1,L,mid);
- outbuild(rt<<1|1,mid+1,R);
- }
- }
- void change(int rt,int x,int y)
- {
- del(root[rt],pre[x]);
- insert(root[rt],y);
- if(l[rt]!=r[rt])
- {
- int mid=(l[rt]+r[rt])>>1;
- if(x<=mid)
- {
- change(rt<<1,x,y);
- }
- else
- {
- change(rt<<1|1,x,y);
- }
- }
- }
- int outrank(int rt,int L,int R,int k)
- {
- if(L<=l[rt]&&r[rt]<=R)
- {
- return inrank(root[rt],k);
- }
- int mid=(l[rt]+r[rt])>>1;
- if(R<=mid)
- {
- return outrank(rt<<1,L,R,k);
- }
- else if(L>mid)
- {
- return outrank(rt<<1|1,L,R,k);
- }
- return outrank(rt<<1,L,R,k)+outrank(rt<<1|1,L,R,k);
- }
- int main()
- {
- srand(12378);
- scanf("%d%d",&n,&m);
- for(int i=1;i<=n;i++)
- {
- scanf("%d",&a[i]);
- pre[i]=b[a[i]];
- suf[b[a[i]]]=i;
- b[a[i]]=i;
- s[a[i]].insert(i);
- }
- for(int i=1;i<=n;i++)
- {
- if(!suf[i])
- {
- suf[i]=n+1;
- }
- }
- outbuild(1,1,n);
- for(int i=1;i<=m;i++)
- {
- scanf("%s",ch);
- if(ch[0]=='R')
- {
- scanf("%d%d",&x,&y);
- if(a[x]==y)
- {
- continue;
- }
- if(pre[x])
- {
- suf[pre[x]]=suf[x];
- }
- if(suf[x]!=n+1)
- {
- change(1,suf[x],pre[x]);
- pre[suf[x]]=pre[x];
- }
- s[a[x]].erase(x);
- a[x]=y;
- s[a[x]].insert(x);
- it=s[a[x]].lower_bound(x);
- if(it!=s[a[x]].begin())
- {
- it--;
- suf[(*it)]=x;
- change(1,x,(*it));
- pre[x]=(*it);
- it++;
- }
- else
- {
- change(1,x,0);
- pre[x]=0;
- }
- if(++it!=s[a[x]].end())
- {
- suf[x]=(*it);
- change(1,(*it),x);
- pre[(*it)]=x;
- }
- else
- {
- suf[x]=n+1;
- }
- }
- else
- {
- scanf("%d%d",&x,&y);
- printf("%d\n",outrank(1,x,y,x));
- }
- }
- }
BZOJ2120&2453数颜色——线段树套平衡树(treap)+set/带修改莫队的更多相关文章
- 树套树Day1线段树套平衡树bzoj3196
您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的前驱(前驱定义为小于x,且最大的数)5.查 ...
- 【BZOJ-3196】二逼平衡树 线段树 + Splay (线段树套平衡树)
3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2271 Solved: 935[Submit][Stat ...
- BZOJ3196二逼平衡树——线段树套平衡树(treap)
此为平衡树系列最后一道:二逼平衡树您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询 ...
- bzoj 3196/ Tyvj 1730 二逼平衡树 (线段树套平衡树)
3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description ...
- bzoj 2120 线段树套平衡树
先吐下槽,改了快一个小时,最后发现是SBT的delete写错了,顿时就有想死的心..... 首先对于这道题,我们应该先做一下他的小问题,bzoj1878,虽然和这道题几乎一点关系没有, 但是能给我们一 ...
- P3380 【模板】二逼平衡树(树套树) 线段树套平衡树
\(\color{#0066ff}{ 题目描述 }\) 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上 ...
- BZOJ3196 二逼平衡树 【线段树套平衡树】
题目 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查询k在区间内的前驱(前驱 ...
- 浅谈树套树(线段树套平衡树)&学习笔记
0XFF 前言 *如果本文有不好的地方,请在下方评论区提出,Qiuly感激不尽! 0X1F 这个东西有啥用? 树套树------线段树套平衡树,可以用于解决待修改区间\(K\)大的问题,当然也可以用 ...
- CF 19D - Points 线段树套平衡树
题目在这: 给出三种操作: 1.增加点(x,y) 2.删除点(x,y) 3.询问在点(x,y)右上方的点,如果有相同,输出最左边的,如果还有相同,输出最低的那个点 分析: 线段树套平衡树. 我们先离散 ...
随机推荐
- MySQL(六)常用语法和数据类型
阅读MySQL语法时,需要注意的规则: ①符号用来指出几个选择中的一个,比如:null | not null表示或者给出null或者给出not null: ②包含在方括号中的关键字或子句(如[like ...
- linux中断源码分析 - 软中断(四)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 在上一篇文章中,我们看到中断实际分为了两个部分,俗称就是一部分是硬中断,一部分是软中断.软中断是专门用于处理中断 ...
- eclpse安装jetty插件
公司不用tomcat,使用的是jetty,那么学习一下如何在eclipse中安装jetty插件.
- php实现一个单链表
单链表,节点只有一个指针域的链表.节点包括数据域和指针域. 因此用面向对象的思维,节点类的属性就有两个:一个data(表示存储的数据),一个指针next(链表中指向下一个节点). 链表一个很重要的特性 ...
- NOI.ac #31 MST DP、哈希
题目传送门:http://noi.ac/problem/31 一道思路好题考虑模拟$Kruskal$的加边方式,然后能够发现非最小生成树边只能在一个已经由边权更小的边连成的连通块中,而树边一定会让两个 ...
- 搭建SpringBoot+dubbo+zookeeper+maven框架(二)
上一篇文章是关于搭建SpringBoot+dubbo+zookeeper+maven框架的,但是里面的功能还不够完善,今天就日志管理方面做一些改善. 下了demo的网友可能会发现项目在启动时会有警告: ...
- VS2015 搭建 Asp.net core 开发环境
1.首先你得装个vs2015 并且保证已经升级至 update3及以上(此处附上一个vs2015带up3的下载链接: ed2k://|file|cn_visual_studio_enterprise_ ...
- linux监控文件夹内的文件数量
开发的时候遇到一个问题,服务器一旦重启,项目生成的文件就丢失了,感觉很莫名其妙..一开始猜测是文件流没有关闭,检查了代码,感觉没毛病.于是先看看是关机丢失了文件还是开机被删除了.下面的脚本每秒执行一次 ...
- VS2010、VS2012、VS2013、VS2015、VS2017各版本产品激活秘钥
Visual Studio 2017(VS2017) 企业版 Enterprise 注册码:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Visual Studio 2017(VS201 ...
- 保留最新N份备份目录脚本
如下所示,在/opt/backup下是备份目录,只需要保留最新的三份备份,在此之前的备份目录都要删除. [root@syslog-ng ~]# cd /opt/backup/ [root@syslog ...