UVA 12345 Dynamic len(带修莫队)
Dynamic len
【题目链接】Dynamic len
【题目类型】带修莫队
&题解:
莫队可以单点更改,只要再多加一维,代表查询次数,排序的时候3个关键字.
之后循环离线的时候,先暴力时间指针(oi大佬说的),之后l,和r就随便了.还有要会用vis数组.
【时间复杂度】\(O(n^{\frac{5}{3}})\)
&代码:
#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
const int maxn = 5e4 + 7;
struct nt {
int l, r, id, ts;
} ask[maxn];
struct nd {
int x, pre, v;
} p[maxn];
int n, m, an[maxn], c[maxn], pos[maxn], num[maxn * 20], last[maxn], ans;
bool vis[maxn];
char s[9];
bool cmp(nt a, nt b) {
return pos[a.l] < pos[b.l] ||
pos[a.l] == pos[b.l] && pos[a.r] < pos[b.r] ||
pos[a.l] == pos[b.l] && pos[a.r] == pos[b.r] && a.ts < b.ts;
}
void update(int x) {
if(vis[x]) {
if(!--num[c[x]]) --ans;
}
else if(++num[c[x]] == 1) ans++;
vis[x] ^= 1;
}
void change(int x, int v) {
if(vis[x]) {
update(x); c[x] = v; update(x);
}
else c[x] = v;
}
int main() {
//("E:1.in", "r", stdin);
scanf("%d%d", &n, &m);
int x, y, tot = 0, top = 0;
int bk = sqrt(n);
for(int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
last[i] = c[i];
pos[i] = i / bk;
}
for(int i = 1; i <= m; i++) {
scanf("%s%d%d", s, &x, &y);
x++;
if(s[0] == 'Q') {
//[++tot].id必须要赋为tot. 而且这句话必须在第一位写
ask[++tot].id = tot;
ask[tot].l = x, ask[tot].r = y, ask[tot].ts = top;
}
else {
p[++top].x = x, p[top].v = y, p[top].pre = last[x];
last[x] = y;
}
}
sort(ask + 1, ask + 1 + tot, cmp);
int now = 0, pl = 1, pr = 0;
for(int i = 1; i <= tot; i++) {
if(now < ask[i].ts) {
for(int j = now + 1; j <= ask[i].ts; j++)
change(p[j].x, p[j].v);
}
else {
for(int j = now; j >= ask[i].ts + 1; j--)
change(p[j].x, p[j].pre);
}
if(pr < ask[i].r) {
for(int j = pr + 1; j <= ask[i].r; j++)
update(j);
}
else {
for(int j = pr; j >= ask[i].r + 1; j--)
update(j);
}
if(pl < ask[i].l) {
for(int j = pl; j <= ask[i].l - 1; j++)
update(j);
}
else {
for(int j = pl - 1; j >= ask[i].l; j--)
update(j);
}
pl = ask[i].l, pr = ask[i].r, now = ask[i].ts;
an[ask[i].id] = ans;
}
for(int i = 1; i <= tot; i++) {
printf("%d\n", an[i]);
}
return 0;
}
UVA 12345 Dynamic len(带修莫队)的更多相关文章
- LG4074【WC2013】糖果公园 【树上莫队,带修莫队】
题目描述:给出一棵 \(n\) 个点的树,点有颜色 \(C_i\),长度为 \(m\) 的数组 \(V\) 和长度为 \(n\) 的数组 \(W\).有两种操作: 将 \(C_x\) 修改为 \(y\ ...
- P5168 xtq玩魔塔 [克鲁斯卡尔重构树+带修莫队]
P5168 xtq玩魔塔 又是码农题- 利用克鲁斯卡尔重构树的性质 我们就可以得出 \(dep\) 值小的,肯定比 \(dep\) 大的值要优. 于是第二问就可以直接 LCA 求出来了- 至于第三问, ...
- 【BZOJ-3052】糖果公园 树上带修莫队算法
3052: [wc2013]糖果公园 Time Limit: 200 Sec Memory Limit: 512 MBSubmit: 883 Solved: 419[Submit][Status] ...
- 「洛谷1903」「BZOJ2120」「国家集训队」数颜色【带修莫队,树套树】
题目链接 [BZOJ传送门] [洛谷传送门] 题目大意 单点修改,区间查询有多少种数字. 解法1--树套树 可以直接暴力树套树,我比较懒,不想写. 稍微口胡一下,可以直接来一个树状数组套主席树,也就是 ...
- BZOJ2120 数颜色 莫队 带修莫队
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2120.html 题目传送门 - BZOJ2120 题意 给定一个长度为 $n$ 的序列 $a$ ,有 ...
- BZOJ3052/UOJ#58 [wc2013]糖果公园 莫队 带修莫队 树上莫队
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3052.html 题目传送门 - BZOJ3052 题目传送门 - UOJ#58 题意 给定一棵树,有 ...
- bzoj 2120 数颜色 (带修莫队)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2120 题意:两种操作:Q 询问区间 l - r 内颜色的种类 ,R 单点修改 思路 ...
- BZOJ 4129 Haruna’s Breakfast (分块 + 带修莫队)
4129: Haruna’s Breakfast Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 835 Solved: 409[Submit][St ...
- BZOJ 2120 数颜色 (带修莫队)
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MBSubmit: 6367 Solved: 2537[Submit][Status][Discuss] ...
随机推荐
- C语言 · 滑动解锁
题目:滑动解锁 滑动解锁是智能手机一项常用的功能.你需要在3x3的点阵上,从任意一个点开始,反复移动到一个尚未经过的"相邻"的点.这些划过的点所组成的有向折线,如果与预设的折线在图 ...
- yii2快速導出phpexcel
https://packagist.org/packages/moonlandsoft/yii2-phpexcel 安装方式:首先是已经安装过Composer,则通过 Composer 下载安装 Mo ...
- centos7 RTMP直播服务器搭建
首先需要下载 nginx-1.8.1 : http://nginx.org/download/nginx-1.8.1.tar.gz nginx-rtmp-module : https://github ...
- 初学UML之-------用例图
本文转载至:http://blog.csdn.net/a649518776/article/details/7493148 一.UML简介 UML(统一建模语言,Unified Modeling L ...
- OpenGL step by step 38 : Skeletal Animation with Assimp
一般骨架模型由两部分组成: Rigging(bone):相当于骨架,可以用来控制模型的动作 Mesh(skin):相当于表面皮肤 骨架模型一般是层级结构的,比如上面 背骨是root,他的孩子包括胳膊. ...
- Linux usb子系统(二) _usb-skeleton.c精析
"./drivers/usb/usb-skeleton.c"是内核提供给usb设备驱动开发者的海量存储usb设备的模板程序, 程序不长, 通用性却很强,十分经典, 深入理解这个文件 ...
- 使用ionic2开发一个二维码扫描功能
界面添加一个按钮: <button ion-button block color="secondary" class="Scan-button" (cli ...
- hdu 2899
mxy终于学会求函数极值了. 先写一道板子. #include <bits/stdc++.h> using namespace std; typedef double db; ; cons ...
- phpcms栏目标签调用
$CATEGORY[$catid][catid] 栏目id $CATEGORY[$catid][module] 栏目所在的模块 $C ...
- JQuery限制文本框只能输入数字和小数点的方法
<input type="text" class="txt NumText" Width="100px" /> $(func ...