题解

练习一下带修改莫队

先按照左端点的块排序,再按照右端点的块排序,然后按照时间排序

每个修改操作存一下修改前这个位置的值就可以逆序操作了

代码

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define pdi pair<db,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 10005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
} int N,M,S;
int c[1000005],res,ans[MAXN],op[MAXN],col[2][MAXN],tot,id[MAXN],a[MAXN],tmp[MAXN];
int p,q,x;
struct qry_node {
int id,l,r,bl,br;
friend bool operator < (const qry_node &a,const qry_node &b) {
if(a.bl != b.bl) return a.bl < b.bl;
if(a.br != b.br) return a.br < b.br;
return a.id < b.id;
}
}qry[MAXN];
void update_pos(int pos,int v) {
if(c[pos] == 0 && c[pos] + v == 1) ++res;
else if(c[pos] == 1 && c[pos] + v == 0) --res;
c[pos] += v;
}
void Change(int t) {
if(t > x) {
for(int i = x + 1 ; i <= t ; ++i) {
if(op[i]) {
if(op[i] <= q && op[i] >= p) {
update_pos(a[op[i]],-1);
update_pos(col[1][i],1);
}
a[op[i]] = col[1][i];
}
}
}
else {
for(int i = x ; i > t ; --i) {
if(op[i]) {
if(op[i] <= q && op[i] >= p) {
update_pos(a[op[i]],-1);
update_pos(col[0][i],1);
}
a[op[i]] = col[0][i];
}
}
}
x = t;
}
void Move(int l,int r) {
while(q < r) {update_pos(a[++q],1);}
while(p > l) {update_pos(a[--p],1);}
while(q > r) {update_pos(a[q--],-1);}
while(p < l) {update_pos(a[p++],-1);}
}
void Solve() {
read(N);read(M);
S = pow(N,2.0 / 3.0);
int h = 0;
for(int i = 1 ; i <= N ; ++i) {read(a[i]);tmp[i] = a[i];}
for(int i = 1 ; i <= N ; i += S) {
int r = min(i + S - 1,N);
++h;
for(int j = i ; j <= r ; ++j) {
id[j] = h;
}
}
char s[5];
int l,r;
for(int i = 1 ; i <= M ; ++i) {
scanf("%s",s + 1);
read(l);read(r);
if(s[1] == 'Q') {
qry[++tot] = (qry_node){i,l,r,id[l],id[r]};
}
else {
op[i] = l;col[1][i] = r;col[0][i] = tmp[l];
tmp[l] = r;
}
}
sort(qry + 1,qry + tot + 1);
x = 0,p = 1,q = 0;
for(int i = 1 ; i <= tot ; ++i) {
Change(qry[i].id);
Move(qry[i].l,qry[i].r);
ans[qry[i].id] = res;
}
for(int i = 1 ; i <= M ; ++i) {
if(!op[i]) {
out(ans[i]);enter;
}
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}

【BZOJ】2120: 数颜色的更多相关文章

  1. BZOJ 2120 数颜色(带修改的莫队)

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MB Submit: 3478  Solved: 1342 [Submit][Status][Discus ...

  2. BZOJ 2120: 数颜色

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 3623  Solved: 1396[Submit][Status][Discuss] ...

  3. BZOJ 2120: 数颜色 分块

    2120: 数颜色 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php? ...

  4. Bzoj 2120: 数颜色 && 2453: 维护队列 莫队,分块,bitset

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 2645  Solved: 1039[Submit][Status][Discuss] ...

  5. BZOJ 2120 数颜色 (带修莫队)

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 6367  Solved: 2537[Submit][Status][Discuss] ...

  6. BZOJ 2120 数颜色 【带修改莫队】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 2120: 数颜色 Time Limit: 6 Sec  Memory Limit: ...

  7. bzoj 2120 数颜色 题解

    转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23990489 [原题] 2120: 数颜色 Time Limit: 6 Sec  M ...

  8. bzoj 2120 数颜色 (带修莫队)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2120 题意:两种操作:Q 询问区间  l - r  内颜色的种类 ,R 单点修改 思路 ...

  9. BZOJ 2120 数颜色(带修改莫队)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2120 [题目大意] 给出一颜色序列,每次可以修改一个位置的颜色或者询问一个区间不同颜色 ...

  10. BZOJ 2120 数颜色&2453 维护队列 [带修改的莫队算法]【学习笔记】

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 3665  Solved: 1422[Submit][Status][Discuss] ...

随机推荐

  1. 【转】Git 安装和使用教程

    git 提交 全部文件 git add .  git add xx命令可以将xx文件添加到暂存区,如果有很多改动可以通过 git add -A .来一次添加所有改变的文件.注意 -A 选项后面还有一个 ...

  2. CF1110D Jongmah(DP)

    题目链接:CF原网  洛谷 题目大意:有 $n$ 个数,每个都不超过 $m$.一个三元组 $(a,b,c)$ 是合法的当且仅当 $a=b=c$ 或者 $a+1=b=c-1$.每个数只能用一次.问最多能 ...

  3. AIO 开始不定时的抛异常: java.io.IOException: 指定的网络名不再可用

    一天里会抛出几个这样的错误,但发现服务还在正常的运行. java.io.IOException: 指定的网络名不再可用. at sun.nio.ch.Iocp.translateErrorToIOEx ...

  4. struts2拦截器interceptor的配置方法及使用

    转: struts2拦截器interceptor的配置方法及使用 (2015-11-09 10:22:28) 转载▼ 标签: it 365 分类: Struts2  NormalText Code  ...

  5. Tensorflow图像处理

    Tensorflow图像处理主要包括:调整尺寸,图像翻转,调整色彩,处理标注框. 代码如下: #coding=utf-8 import matplotlib.pyplot as plt import ...

  6. Openstack 错误日志查看方法

    openstack错误日志查看方法 https://blog.csdn.net/ZanShichun/article/details/72672945

  7. linux命令总结之echo命令

    echo是一种最常用的与广泛使用的内置于Linux的bash和C shell的命令,通常用在脚本语言和批处理文件中来在标准输出或者文件中显示一行文本或者字符串. echo命令的语法是: echo [选 ...

  8. JAVA (字符串学习)

    String类 String类是不可改变的,所以你一旦创建了String对象,那它的值就无法改变了. 如果需要对字符串做很多修改,那么应该选择使用StringBuffer&StringBuil ...

  9. webpack进阶--loader

    webpack的核心就是它的配置文件,只要配置好配置文件webpack就可以用得利索-- 而配置文件主要就是7个部分entry.output.plugins.resolve.devserver(web ...

  10. 【1】ConcurrentModificationException 异常解析和快速失败,安全失败

    目录 一.引起异常的代码 二.foreach原理 三.从ArrayList源码找原因 四.单线程解决方案 五.在多线程环境下的解决方法 一.引起异常的代码 以下三种的遍历集合对象时候,执行集合的rem ...