bzoj2453/2120 数颜色
2024-09-25 18:00:49
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2120
http://www.lydsy.com/JudgeOnline/problem.php?id=2453
【题解】
带修改莫队,分块大小n^(2/3),总复杂度O(n^(5/3))
简单说下,就是按照开始block,结束block,时间顺序分别排序后,不仅按照两个端点像莫队那样做,还把时间也按莫队那样做,就是维护恰好到询问时间的所有修改。
# include <math.h>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 2e5 + , N = 2e6 + ;
const int mod = 1e9+; # define RG register
# define ST static int n, m, a[M], t[M];
int BLOCK, bl[M];
struct pa {
int x, y, t;
pa() {}
pa(int x, int y, int t) : x(x), y(y), t(t) {}
friend bool operator < (pa a, pa b) {
return bl[a.x] < bl[b.x] || (bl[a.x] == bl[b.x] && bl[a.y] < bl[b.y]) ||
(bl[a.x] == bl[b.x] && bl[a.y] == bl[b.y] && a.t < b.t);
}
}q[M]; struct paa {
int x, oc, nc, t;
paa() {}
paa(int x, int oc, int nc, int t) : x(x), oc(oc), nc(nc), t(t) {}
}p[M]; int pn, qn, ans[M]; int c[N], cnt, L, R; inline void add(int x) {
++ c[a[x]];
if(c[a[x]] == ) ++cnt;
} inline void del(int x) {
-- c[a[x]];
if(c[a[x]] == ) --cnt;
} inline void DEL(int x) {
--c[x];
if(c[x] == ) --cnt;
} inline void ADD(int x) {
++c[x];
if(c[x] == ) ++cnt;
} int main() {
cin >> n >> m;
BLOCK = pow(n, 2.0/3.0);
for (int i=; i<=n; ++i) {
scanf("%d", a+i); t[i] = a[i];
bl[i] = (i-)/BLOCK + ;
}
char opt[];
for (int i=, x, y; i<=m; ++i) {
scanf("%s%d%d", opt, &x, &y);
if(opt[] == 'R') {
p[++pn] = paa(x, t[x], y, i);
t[x] = y;
} else q[++qn] = pa(x, y, i);
ans[i] = ;
}
sort(q+, q+qn+);
L = , R = ;
for (int i=, j=; i<=qn; ++i) {
while(j <= pn && q[i].t > p[j].t) {
if(L <= p[j].x && p[j].x <= R) DEL(p[j].oc), ADD(p[j].nc);
a[p[j].x] = p[j].nc; ++j;
}
while(j- >= && q[i].t < p[j-].t) {
--j; if(L <= p[j].x && p[j].x <= R) DEL(p[j].nc), ADD(p[j].oc);
a[p[j].x] = p[j].oc;
}
while(R < q[i].y) ++R, add(R);
while(R > q[i].y) del(R), --R;
while(L > q[i].x) --L, add(L);
while(L < q[i].x) del(L), ++L;
ans[q[i].t] = cnt;
}
for (int i=; i<=m; ++i)
if(ans[i]) printf("%d\n", ans[i]);
return ;
}
最新文章
- java基础_集合List与Set接口
- mysql复习相关
- jquery easyui使用(三)&#183;&#183;&#183;&#183;&#183;&#183;datagrid加载数据(已解决)
- ajax 传递JSON对象参数
- 5.3(2)----机器人走方格2(CC150)
- ECshop导入淘宝数据包乱码问题解决方法
- 《Python核心编程》 第八章 条件和循环
- PowerDesigner从SqlServer 数据库中导入实体模型
- 关于Win8对getElementsByTagName等dom方法兼容性的替代方法
- Java+Velocity模板引擎集成插件到Eclipse及使用例子
- ArcGIS jsAPI (4.x)本地部署字体符号乱码
- ionic3 笔记
- Java语言程序设计课程学期总结
- C++对象模型(一):The Semantics of Constructors The Default Constructor (默认构造函数什么时候会被创建出来)
- docker容器日志收集方案(方案N,其他中间件传输方案)
- GIT &; VersionControl
- 深度学习 ——style reconstruction
- Codeforces 1139D Steps to One dp
- WIN 10下Mysql 5.7.21解压缩(免安装版)配置
- 【个人博客作业II】有关代码规范问题的讨论