【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的时候, ...
随机推荐
- 从大整数乘法的实现到 Karatsuba 快速算法
Karatsuba 快速乘积算法是具有独特合并过程(combine/merge)的分治算法(Karatsuba 是俄罗斯人).此算法主要是对两个整数进行相乘,并不适用于低位数(如 int 的 32 位 ...
- Python Tricks(二十)—— 阶乘的极简实现
使用 reduce # 比如计算 9 的阶乘 >> reduce(lambda x, y: x*y, range(1, 9+1)) 362880 当然这里的 reduce 直接返回具体的数 ...
- Lucy_Hedgehog techniques
在project euler 的第\(10\)题的 \(forum\) 中 Lucy Hedgehog 提到的这种方法. 求 \(n\) 以内素数个数以及求 \(n\) 以内素数和的算法. 定义\(S ...
- 每日技术总结:setx,
1.setx命令设置环境变量 设置用户环境变量: setx NAME "XXX" 设置系统环境变量: setx NAME "XXX" /m
- Nginx TCP代理
nginx 在1.9.0 版本发布以前如果要想做到基于TCP的代理及负载均衡需要通过打名为nginx_tcp_proxy_module的第三方patch来实现,该模块的代码托管在github上 网址: ...
- 汉字转换成对应ASCII
private static byte[] hanziToHexByte(string hanzistring) { byte[] bytes = Encoding ...
- [NodeJS] Use Secrets When Deploying Applications with Now
Applications require a lot of sensitive information. Database passwords, API keys and secrets used f ...
- 重构——DataTable转泛型
泛型简单介绍 泛型能够最大限度的重用代码.保护类型的安全.提高性能. 泛型最常见的用途是创建集合类 泛型数据类型中使用的信息可在执行时通过反射 ...
- Linux下开启关闭防火墙
一.Linux下开启/关闭防火墙命令 1) 永久性生效,重启后不会复原 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重 ...
- 10.5 android输入系统_Reader线程_使用EventHub读取事件和核心类及配置文件_实验_分析
4. Reader线程_使用EventHub读取事件 使用inotify监测/dev/input下文件的创建和删除 使用epoll监测有无数据上报 细节: a.fd1 = inotify_init(& ...