【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的时候, ...
随机推荐
- HDU1969 Pie(二分搜索)
题目大意是要办生日Party,有n个馅饼,有f个朋友.接下来是n个馅饼的半径.然后是分馅饼了, 注意咯自己也要,大家都要一样大,形状没什么要求,但都要是一整块的那种,也就是说不能从两个饼中 各割一小块 ...
- Android RxJava基本流程和lift源码分析
基本结构 我们先来看一段最基本的代码,分析这段代码在RxJava中是如何实现的. Observable.OnSubscribe<String> onSubscriber1 = new Ob ...
- Android 使用JSON格式与服务器交互 中文乱码问题解决
当前是在开发Android 程序时,客户端与服务器端采用JSON传送数据,发现中文乱码问题.不过这个问题的解决办法应该对所有java语言开发的项目都使用. 解决方法是: 1.客户端发送数据之间加上: ...
- 硬件——STM32,ADC篇
未完,待续...... 也就是stm32f10X系列的adc采集出来的结果是12位的 stm32f10X系列有两个16位adc 关于程序的编写方法:一般 “某某.c文件”:都是用来设置“某某”的一些 ...
- (转)高强度密码管理软件KeePass使用详解
转自:http://www.ruancan.com/ 算下来,你接触电脑有多久了?从第一次上网,到今天,你一共申请了多少个网站或者软件的帐号?相信这是一个几乎无人能够回答的问题. 无数人面临着这两个问 ...
- DB2学习总结(1)——DB2数据库基础入门
DB2的特性 完全Web使能的:可以利用HTTP来发送询问给服务器. 高度可缩放和可靠:高负荷时可利用多处理器和大内存,可以跨服务器地分布数据库和数据负荷:能够以最小的数据丢失快速地恢复,提供多种备份 ...
- python中如何在一张图上画两条折线
摘自:https://segmentfault.com/q/1010000002760775
- 字符的输入和输出即:getchar和putchar
#include <stdio.h> int main(int argc, const char * argv[]) { putchar(getchar());//这种方式就是输入一个字符 ...
- 前端项目课程3 jquery1.8.3到1.11.1有了哪些新改变
web项目课程3 jquery1.8.3到1.11.1有了哪些新改变 一.总结 一句话总结:领会官方升级的意思. 1.live(); 2.die(); 3.bind(); 4.u ...
- php实现字符串替换
php实现字符串替换 一.总结 二.php实现字符串替换 代码一: //字符串替换 function str_replace($substr , $newsubstr, $str) { $m = st ...