传送门(权限题)

题目分析

题意为:求区间内有多少种不同的数,带修改。 首先对原序列分块,用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】维护队列 (分块 + 二分)的更多相关文章

  1. [BZOJ2453]维护队列|分块

    Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会 ...

  2. 【BZOJ2473/2120】维护队列 分块+二分

    Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会 ...

  3. BZOJ2453: 维护队列

    2453: 维护队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 183  Solved: 89[Submit][Status] Descripti ...

  4. [bzoj2453]维护队列_带修改莫队

    维护队列 bzoj-2453 题目大意:给定一个n个数序列,支持查询区间数的种类数,单点修改.不强制在线. 注释:$1\le n,m\le 10^5$. 想法: 带修改莫队裸题. 如果没有修改操作的话 ...

  5. [BZOJ2120] 数颜色 && [bzoj2453] 维护队列(莫队 || 分块)

    传送门 只有第一个,第二个权限题. 分块,然而wa,没看出来错在哪里,有时间再看. #include <cmath> #include <cstdio> #include &l ...

  6. 【分块】bzoj2453 维护队列

    http://www.cnblogs.com/autsky-jadek/p/4020296.html 同bzoj2120. #include<cstdio> #include<cma ...

  7. BZOJ2453维护队列&&BZOJ2120数颜色

    2016-05-28 11:20:22 共同的思路: 维护某种颜色上一次在哪里出现pre,可以知道当pre<询问的l时更新答案 块内按照pre排序 修改的时候重新O(n)扫一遍,如果和之前的不一 ...

  8. bzoj2120: 数颜色 &&bzoj2453: 维护队列

    题目大意: 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会依据个人喜好 ...

  9. BZOJ 2453 维护队列 | 分块

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2453 题解: 考虑维护每个位置的颜色上一次出现在哪里,计为pre[i],在询问l到r的时候, ...

随机推荐

  1. 从软件project的角度写机器学习3——主要监督学习算法的project性分析

    主要机器学习算法的project适用性分析 前段时间AlphaGo跟李世石的大战及相关的深度学习的新闻刷了一遍又一遍的朋友圈.只是这件事情,也仅仅是在机器学习的深度上进一步拓展,而机器学习的广度(也即 ...

  2. js进阶 14-2 如何用ajax验证登陆状态(这里用load方法)

    js进阶 14-2 如何用ajax验证登陆状态(这里用load方法) 一.总结 一句话总结:$('#test').load('test.php?password=1234560'),这样就get方式提 ...

  3. POJ 3278 Catch That Cow(BFS 剪枝)

    题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出 ...

  4. Eclipse "Could not create java virtual machine"的问题解决

    今天到了新的环境,需要重新搭建Android的开发环境,下载eclipse并安装了JDK1.6后,启动eclipse,发现出现了错误“Could not create Javavirtual mach ...

  5. Spring Boot 2.x 使用 jpa 连接 mysql

    在spring boot网站上生成一个项目,如图: 我使用的是Maven项目,java使用是jdk8(spring boot 2.x必须要jdk8及以上),dependencies分别输入选择 web ...

  6. 【习题 5-11 UVA 12504 】Updating a Dictionary

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 不确定某个map里面是否有某个关键字的时候. 要用find来确定. 如果直接用访问下标的形式去做的话. 会强行给他加一个那个关键字( ...

  7. shiro 中的filterChainDefinitions详解(转)

    springrain使用shiro控制权限,配置filterChainDefinitions结合数据库校验权限. 我们在web.xml中配置一个全局过滤器,也就是在springrain配置的是一个sp ...

  8. shiro 静态页面资源不显示 解决方案(转)

    最近做一个ssm+shiro的框架整和 不加shiro之前ssm中css和图片显示正常.加上以后无法显示. 解决方案: shiro有静态资源过滤. 配置资源匿名访问即可 <property na ...

  9. 设置cell背景色和选中色

    // 设置cell的背景色 UIView *bg = [[[UIView alloc] init] autorelease]; bg.backgroundColor = [UIColor colorW ...

  10. 【7001】n阶法雷序列

    Time Limit: 10 second Memory Limit: 2 MB 问题描述      对任意给定的一个自然数n(n<=100),将分母小于等于n的不可约的真分数按上升的次序排序, ...