【bzoj2453】维护队列 (分块 + 二分)
传送门(权限题)
题目分析
题意为:求区间内有多少种不同的数,带修改。 首先对原序列分块,用last[i]表示与i相同的上一个在哪里,然后将分块后的数组每个块内的按照last进行排序,这样查询时就可以暴力枚举散块,看last[i]是否<l,是则ans++,并二分枚举每个整块,查找出last < l 的数的个数即新出现的数。对于修改,修改后暴力重新构建被影响点所在的块。
code
3452 ms
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
using namespace std; const int N = 1e4 + ;
int n, m, col[N], now[], last[N], La[N], S, blo; inline int read(){
int i = , f = ; char ch = getchar();
for(; (ch < '' || ch > '') && ch != '-'; ch = getchar());
if(ch == '-') f = -, ch = getchar();
for(; ch >= '' && ch <= ''; ch = getchar())
i = (i << ) + (i << ) + (ch - '');
return i * f;
} inline void wr(int x){
if(x < ) putchar('-'),x = -x;
if(x > ) wr(x / );
putchar(x % + '');
} inline void change(int x, int c){
if(col[x] == c) return;
for(int i = ; i <= n; i++) now[col[i]] = ;
col[x] = c;
for(int i = ; i <= n; i++){
int tmp = last[i];
last[i] = now[col[i]];
if(last[i] != tmp){
int B = i / S + (i % S ? : );
int l = (B - ) * S + , r = min(n, B * S);
for(int j = l; j <= r; j++) La[j] = last[j];
sort(La + l, La + r + );
}
now[col[i]] = i;
}
} inline int query(int x, int y){
int ans = ;
if(y - x + <= * S){
for(int i = x; i <= y; i++)
if(last[i] < x) ans++;
return ans;
}
int Bx = x / S + (x % S ? : ), By = y / S + (y % S ? : );
int L = Bx + , R = By - ;
if(x == (Bx - ) * S + ) L--;
if(y == min(n, By * S)) R++;
for(int i = x; i <= (L - ) * S; i++)
if(last[i] < x) ans++;
for(int i = min(n, R * S) + ; i <= y; i++)
if(last[i] < x) ans++;
for(int i = L; i <= R; i++){
int l = (i - ) * S + , r = min(n, i * S);
int tmp = lower_bound(La + l, La + r + , x) - (La + l);
ans += tmp;
}
return ans;
} int main(){
n = read(), m = read(), S = , blo = n / S + (n % S ? : );
for(int i = ; i <= n; i++){
col[i] = read();
last[i] = La[i] = now[col[i]];
now[col[i]] = i;
}
for(int i = ; i <= blo; i++){
int l = (i - ) * S + , r = min(n, i * S);
sort(La + l, La + r + );
}
for(int i = ; i <= m; i++){
char opt[]; scanf("%s", opt + );
if(opt[] == 'Q'){
int l = read(), r = read();
if(l > r) swap(l, r);
wr(query(l, r)), putchar('\n');
}
else if(opt[] == 'R'){
int x = read(), c = read();
change(x, c);
}
}
return ;
}
【bzoj2453】维护队列 (分块 + 二分)的更多相关文章
- [BZOJ2453]维护队列|分块
Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会 ...
- 【BZOJ2473/2120】维护队列 分块+二分
Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会 ...
- BZOJ2453: 维护队列
2453: 维护队列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 183 Solved: 89[Submit][Status] Descripti ...
- [bzoj2453]维护队列_带修改莫队
维护队列 bzoj-2453 题目大意:给定一个n个数序列,支持查询区间数的种类数,单点修改.不强制在线. 注释:$1\le n,m\le 10^5$. 想法: 带修改莫队裸题. 如果没有修改操作的话 ...
- [BZOJ2120] 数颜色 && [bzoj2453] 维护队列(莫队 || 分块)
传送门 只有第一个,第二个权限题. 分块,然而wa,没看出来错在哪里,有时间再看. #include <cmath> #include <cstdio> #include &l ...
- 【分块】bzoj2453 维护队列
http://www.cnblogs.com/autsky-jadek/p/4020296.html 同bzoj2120. #include<cstdio> #include<cma ...
- BZOJ2453维护队列&&BZOJ2120数颜色
2016-05-28 11:20:22 共同的思路: 维护某种颜色上一次在哪里出现pre,可以知道当pre<询问的l时更新答案 块内按照pre排序 修改的时候重新O(n)扫一遍,如果和之前的不一 ...
- bzoj2120: 数颜色 &&bzoj2453: 维护队列
题目大意: 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会依据个人喜好 ...
- BZOJ 2453 维护队列 | 分块
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2453 题解: 考虑维护每个位置的颜色上一次出现在哪里,计为pre[i],在询问l到r的时候, ...
随机推荐
- Android中Application类的详解:
Android中Application类的详解: 我们在平时的开发中,有时候可能会须要一些全局数据.来让应用中的全部Activity和View都能訪问到.大家在遇到这样的情况时,可能首先会想到自定义一 ...
- [React & Debug] Quick way to debug Stateless component
For example we have the following code: const TodoList = (props) => ( <div className="Tod ...
- [Angular] Progress HTTP Events with 'HttpRequest'
New use case that is supported by the HTTP client is Progress events. To receive these events, we cr ...
- StringBuilder和String的区别
使用 StringBuilder 语言 C# String 对象是不可改变的.每次使用 System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为 ...
- Android滑动到顶部悬停
无图说卵,先上图 jianshu-top.gif 查阅资料后,发现网上大部分都是用这种方法实现的: 多写一个和需要悬浮的部分一模一样的layout,先把浮动区域的可见性设置为gone.当浮动区域滑动到 ...
- SourceInsight打开的工程中中文字体显示乱码的问题
1.在ubuntu下进入文件所在目录执行指令“file *”来查看文件的编码方式,sourceinsight有些版本只支持GB2312和ascil码,所以需要编码转换: 2.在ubuntu下可以通过i ...
- LoaderManager使用具体解释(一)---没有Loader之前的世界
来源: http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html 感谢作者Alex ...
- 【t099】最接近神的人
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古 ...
- angular表单的使用实例
原文 https://www.jianshu.com/p/da1fd5396798 大纲 1.模板驱动表单的创建 2.响应式表单的创建 3.模板驱动型表单的自定义指令 4.响应式表单的自定义指令 5. ...
- 解决maven项目找不到maven依赖的解决办法
不同的IDE对应的.classpath中的maven声明也不一样,这样就会导致项目找不到maven依赖. 即Java Build Path--->Libraries中找不到Maven Depen ...