您的位置:首页 > 其它

[BZOJ]2120 数颜色 带修改莫队

2017-11-24 09:55 281 查看
2120: 数颜色

Time Limit: 6 Sec Memory Limit: 259 MB

Submit: 6419 Solved: 2556

[Submit][Status][Discuss]

Description

墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会像你发布如下指令: 1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。 2、 R P Col 把第P支画笔替换为颜色Col。为了满足墨墨的要求,你知道你需要干什么了吗?

Input

第1行两个整数N,M,分别代表初始画笔的数量以及墨墨会做的事情的个数。第2行N个整数,分别代表初始画笔排中第i支画笔的颜色。第3行到第2+M行,每行分别代表墨墨会做的一件事情,格式见题干部分。

Output

对于每一个Query的询问,你需要在对应的行中给出一个数字,代表第L支画笔到第R支画笔中共有几种不同颜色的画笔。

Sample Input

6 5

1 2 3 4 5 5

Q 1 4

Q 2 6

R 1 2

Q 1 4

Q 2 6

Sample Output

4

4

3

4

HINT

对于100%的数据,N≤10000,M≤10000,修改操作不多于1000次,所有的输入数据中出现的所有整数均大于等于1且不超过10^6。

2016.3.2新加数据两组by Nano_Ape

Source

题解

做了糖果公园之后再做这道题果然轻松~ 裸的带修改莫队.

#include<stdio.h>
#include<cmath>
#include<algorithm>
#define Acce register int
using namespace std;
const int maxn = 1e4 + 5;
const int maxm = 1e6 + 5;
char ss[2];
int n, m, ori, ret, l, r, now, cnt1, cnt2, block;
int a[maxn], blo[maxn], cnt[maxm], ans[maxn], last[maxn];
inline const int read()
{
Acce x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x;
}
struct query
{
int l, r, t, id;
friend bool operator < (query x, query y)
{
if (blo[x.l] == blo[y.l])
return (blo[x.r] == blo[y.r]) ? x.t < y.t : blo[x.r] < blo[y.r];
else
return blo[x.l] < blo[y.l];
}
}q[maxn], f[maxn];
inline void update(int x, int type)
{
ori = cnt[a[x]], cnt[a[x]] += type;
if (!ori) ret ++;
if (!cnt[a[x]]) ret --;
}
inline void modify(int x,int col)
{
if (l <= x && x <= r)
{
cnt[a[x]] --;
if (!cnt[a[x]]) ret --;
a[x] = col;
if (!cnt[a[x]]) ret ++;
cnt[a[x]] ++;
}
else a[x] = col;
}
inline void Captain_Mo()
{
sort(q + 1, q + cnt1 + 1);
l = 1, r = 0, now = 0;

for (Acce i = 1; i <= cnt1; ++ i)
{
while (now < q[i].t) ++ now, modify(f[now].l, f[now].r);
while (now > q[i].t) modify(f[now].l, f[now].t), -- now;

while (l < q[i].l) update(l ++, -1);
while (l > q[i].l) update(-- l, +1);
while (r < q[i].r) update(++ r, +1);
while (r > q[i].r) update(r --, -1);
ans[q[i].id] = ret;
}
}
int main()
{
n = read(), m = read();
block = (int) pow(n, 2.0 / 3);
for (Acce i = 1; i <= n; ++ i) blo[i] = (i - 1) / block + 1;
for (Acce i = 1; i <= n; ++ i) a[i] = read(), last[i] = a[i];
for (Acce i = 1; i <= m; ++ i)
{
scanf("%s", ss);
if (ss[0] == 'Q')
{
q[++ cnt1].l = read(), q[cnt1].r = read();
q[cnt1].id = cnt1, q[cnt1].t = cnt2;
} else
{
f[++ cnt2].l = read(), f[cnt2].r = read();
f[cnt2].t = last[f[cnt2].l], last[f[cnt2].l] = f[cnt2].r;
}
}
Captain_Mo();
for (Acce i = 1; i <= cnt1; ++ i) printf("%d\n", ans[i]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: