【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的时候, ...
随机推荐
- 11.Cocos2dx2.2下使用JNI技术调用jar包里面的一些方法遇到的一些问题及解决方式。
<span style="font-family: Arial, Helvetica, sans-serif;">步骤一:导入JniHelper.h头文件.</s ...
- uva 1463 - Largest Empty Circle on a Segment(二分+三分+几何)
题目链接:uva 1463 - Largest Empty Circle on a Segment 二分半径,对于每一个半径,用三分求出线段到线段的最短距离,依据最短距离能够确定当前R下每条线段在[0 ...
- Varnish 实战
Varnish 实战项目 目录 实现基于Keepalived+Haproxy+Varnish+LNMP企业级架构 一.环境准备 1.1 相关配置 1.2 安装服务 1.3 关闭防火墙及selinu ...
- 15.SpringBoot简介-SpringBoot是什么可以做什么
转自:https://blog.csdn.net/kingboyworld/article/details/77713743 在过去的两年时间里,最让人兴奋.回头率最高.最能改变游戏规则的东西,大概就 ...
- 【】queue
[链接]点击打开链接 [题意] 实话实说,给 OIER 大神们排队这种工作是最让人头疼的事情了.因为同学们都有自尊 心,都不愿意排后面. 现在共有 n 个同学要排成一列,每个同学有两个属性:影响力和承 ...
- HTML 5 中FileReader的使用
FileReader 接口主要用来把文件读入到内存中,而且读取文件里的数据.FileReader接口提供了一个异步API,使用该API能够在浏览器主线程中异步訪问文件系统 ,读取文件里的数据. Fil ...
- Web--CSS控制页面(link与import方式差别)
先了解: [1] "Table"和"DIV"这两个网页元素诞生的目的不同,首先Table诞生的目的是为了存储数据,而DIV诞生的目的就是 ...
- C语言18个经典问题答录
原文地址:转载:C语言18个经典问题答录作者:lloo 1.这样的初始化有什么问题?char *p = malloc(10); 编译器提示"非法初始式" 云云. 答:这个声明是静态 ...
- 嵌入式Linux学习笔记 NAND Flash控制器
一.NAND Flash介绍和NAND Flash控制器的使用 NAND Flash在嵌入式系统中的作用,相当于PC上的硬盘 常见的Flash有NOR Flash和NAND Flash,NOR Fla ...
- DateTime与timeStamp的转换
DateTime转换为timeStamp: DateTime dt = DateTime.Now; DateTime startTime = TimeZone.CurrentTi ...