[BZOJ2120][BZOJ2453]数颜色
[BZOJ2120]数颜色
试题描述
输入
输出
输入示例
Q
Q
R
Q
Q
输出示例
数据规模及约定
对于100%的数据,N≤10000,M≤10000,修改操作不多于1000次,所有的输入数据中出现的所有整数均大于等于1且不超过10^6。
题解
对于每一个位置 i 我们维护 pre[i] 表示上一个最近的同色的位置。然后用主席树维护 pre[i];需要支持修改,所以得用树状数组套主席树。
对于修改相当于链表的插入和删除,我们对于每一种颜色用一个 set 维护位置集合,修改时需要查前驱后继。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <set>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 10010
#define maxcol 1000010
#define maxnode 7840010 int ToT, sumv[maxnode], lc[maxnode], rc[maxnode];
void update(int& y, int x, int l, int r, int p, int v) {
sumv[y = ++ToT] = sumv[x] + v;
if(l == r) return ;
int mid = l + r >> 1; lc[y] = lc[x]; rc[y] = rc[x];
if(p <= mid) update(lc[y], lc[x], l, mid, p, v);
else update(rc[y], rc[x], mid + 1, r, p, v);
return ;
}
int query(int o, int l, int r, int qr) {
if(r <= qr) return sumv[o];
int mid = l + r >> 1, ans = query(lc[o], l, mid, qr);
if(qr > mid) ans += query(rc[o], mid + 1, r, qr);
return ans;
} int rt[maxn], Rt[maxn];
int n, A[maxn], pre[maxn], lst[maxcol];
void change(int p, int val) {
for(int x = p; x <= n; x += x & -x)
update(Rt[x], Rt[x], 0, n, pre[p], -1),
update(Rt[x], Rt[x], 0, n, val, 1);
pre[p] = val;
return ;
}
int Art[maxn], cntA, Drt[maxn], cntD;
void getrt(int A[], int& cnt, int x) {
cnt = 0;
for(; x; x -= x & -x) A[++cnt] = Rt[x];
return ;
}
int ask(int ql, int qr) {
getrt(Art, cntA, qr); Art[++cntA] = rt[qr];
getrt(Drt, cntD, ql - 1); Drt[++cntD] = rt[ql-1];
int sum = 0;
for(int i = 1; i <= cntA; i++) sum += query(Art[i], 0, n, ql - 1);
for(int i = 1; i <= cntD; i++) sum -= query(Drt[i], 0, n, ql - 1);
return sum;
} set <int> colpos[maxcol]; int main() {
n = read(); int q = read();
for(int i = 0; i < maxcol; i++) colpos[i].insert(0);
for(int i = 1; i <= n; i++) {
A[i] = read();
pre[i] = lst[A[i]];
lst[A[i]] = i;
colpos[A[i]].insert(i);
} for(int i = 1; i <= n; i++) update(rt[i], rt[i-1], 0, n, pre[i], 1);
char cmd[5];
while(q--) {
scanf("%s", cmd);
if(cmd[0] == 'Q') {
int l = read(), r = read();
printf("%d\n", ask(l, r));
}
if(cmd[0] == 'R') {
int p = read(), col = read();
if(A[p] == col) continue; // it -> next ttit -> now tit -> pre
set <int> :: iterator it = colpos[A[p]].lower_bound(p), tit = it, ttit;
it++; tit--;
if(it != colpos[A[p]].end()) change(*it, *tit);
colpos[A[p]].erase(p); colpos[col].insert(p);
it = colpos[col].lower_bound(p); ttit = tit = it;
it++; tit--;
if(it != colpos[col].end()) change(*it, *ttit);
change(*ttit, *tit); A[p] = col;
}
} return 0;
}
[BZOJ2120][BZOJ2453]数颜色的更多相关文章
- 【bzoj2120】数颜色 带修莫队
数颜色 题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画 ...
- 【BZOJ2453】维护队列/【BZOJ2120】数颜色 分块
[BZOJ2453]维护队列 Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色 ...
- 【bzoj2453】维护队列/【bzoj2120】数颜色 分块+二分
题目描述 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会依据个人喜好, ...
- 【bzoj2120】 数颜色
http://www.lydsy.com/JudgeOnline/problem.php?id=2120 (题目链接) 题意 给出一个n个数,m个询问,每次询问一个区间或修改一个数,求区间内不同的数有 ...
- BZOJ2120:数颜色——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2120 https://www.luogu.org/problemnew/show/P1903#su ...
- BZOJ2120:数颜色
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...
- BZOJ2120:数颜色(数状数组套主席树)(带修改的莫对)
墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...
- BZOJ2120&2453数颜色——线段树套平衡树(treap)+set/带修改莫队
题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2 ...
- 【BZOJ2120】数颜色
看代码学习好,好学好懂好ac 原题: 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中 ...
随机推荐
- html5表单新增的元素与属性
1.表单内元素的form属性 在html4中,表单内的从属元素必须书写在表单内部, 而在html5中,可以把他们书写在页面上任何地方, 然后为该元素指定一个form属性,属性值为该表单的id,这样就可 ...
- 回顾PMP考试
2014年9月20日,于我来说绝对可以说是一个重要的日子.经过考场里4个多小时(4个小时正式的时间+前面的签到以及后面的survey等)的鏖战,出去之后才发现北京外国语大学的楼宇是如此的漂亮,阳光也是 ...
- Linux 使用常见问题
1. 如何查看软件安装到什么位置 [Ubuntu] 今天安装了Lxc-docker,想看一下文件都安装到哪里了,首先找到这个包的ersion zhouh1@uhome:~$ dpkg -s lxc-d ...
- IE8 window.open 不支持此接口 的问题解决
在使用vs2010调试代码时,突然出现 window.open 不支持此接口的提示,开始认为是不是vs的问题,后来上网查询说是系统问题.我不想重装系统,之后发现是IE的问题,使用其他浏览器浏览系统不会 ...
- liunx 常用的命令
常用命令 ======================输入模式=================== Ctrl+d 向前缩进 Ctrl+t 向后缩进 =====================光标模式 ...
- TensorFlow中屏蔽warning的方法
问题 使用sudo pip3 install tensorflow安装完CPU版tensorflow后,运行简单的测试程序,出现如下警告: I tensorflow/core/platform/cpu ...
- es6数组新特性
1.Array.from 从类数组和可遍历对象中创建Array的实例 类数组对象包括:函数中的arguments.由document.getElementsByTagName()返回的nodeList ...
- 洛谷P1035 级数求和
#include <iostream> using namespace std; int main(){ long k,i; cin >> k; double s=0.0; ; ...
- 转: 使用 /sys 文件系统访问 Linux 内核
转一个挺不错的文章 使用 /sys 文件系统访问 Linux 内核 https://www.ibm.com/developerworks/cn/linux/l-cn-sysfs/ 如果你正在开发的设备 ...
- webpack 之 loader
loader简介 loader在webpack.config.js中进行配置,配置之后,会自动检测打包过程中引入的文件(import或require),通过test成功匹配被引入的文件名后,会对文件内 ...