2453: 维护队列
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 755 Solved: 340
[ Submit][ Status][ Discuss]
Description
你小时候玩过弹珠吗?
小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N。为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少。当然,A有时候会依据个人喜好,替换队列中某个弹珠的颜色。但是A还没有学过编程,且觉得头脑风暴太浪费脑力了,所以向你来寻求帮助。
Input
输入文件第一行包含两个整数N和M。
第二行N个整数,表示初始队列中弹珠的颜色。
接下来M行,每行的形式为“Q L R”或“R x c”,“Q L R”表示A想知道从队列第L个弹珠到第R个弹珠中,一共有多少不同颜色的弹珠,“R x c”表示A把x位置上的弹珠换成了c颜色。
Output
对于每个Q操作,输出一行表示询问结果。
Sample Input
2 3
1 2
Q 1 2
R 1 2
Q 1 2
Sample Output
2
1
1
HINT
对于100%的数据,有1 ≤ N ≤ 10000, 1 ≤ M ≤ 10000,小朋友A不会修改超过1000次,所有颜色均用1到10^6的整数表示。
Source
与2120同(一份代码A两题。。233)
没修改多欢乐呀(废话...)
加上修改,那就考虑分块吧
怎么分?
pre[i]:位置i的数字上次出现的位置
同理维护nex[i]
对于每个询问区间,显然有用的颜色只有在区间第一次出现的,,它有什么性质呢?
pre[i] < L
然后查询就和3343教主的魔法差不多了,块内维护有序对+暴力重建etc.
但是修改???这是和一个链一样的东西
原来位置的数字很容易找到它在链上的位置,easy
但是修改后的新数???
苟蒻是这样做的
first[i][j]:第i个块中,j第一次出现的位置
同理维护last[i][j]
修改成新数后,块内找下有没有相邻的,没有的话,,往左边的块&&右边的块暴力for
利用last && first数组快速找到
记得处理好边界
这样查询是根号级别的
O(n根号nlogn)
下标又炸了,,RE好久
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 2E4 + 1E3 + 10;
const int SQR = 210;
struct data{
int typ,x,c;
data(int _typ = 0,int _x = 0,int _c = 0) {typ = _typ; x = _x; c = _c;}
}Q[maxn];
int n,m,Sqrt,tot,T,tail[maxn],pre[maxn],nex[maxn],num[maxn],first[SQR][maxn],last[SQR][maxn],
cur = 1,a[maxn],block[SQR][maxn],belong[maxn],from[maxn],to[maxn];
void Build(int now)
{
//if (now == 7591) printf("%d 7591 \n",T);
block[now][0] = 0;
memset(first[now],127,sizeof(first[now]));
memset(last[now],0,sizeof(last[now]));
for (int i = from[now]; i <= to[now]; i++) {
first[now][num[i]] = min(first[now][num[i]],i);
last[now][num[i]] = max(last[now][num[i]],i);
block[now][++block[now][0]] = pre[i];
}
sort(block[now] + 1,block[now] + block[now][0] + 1);
}
int Query(int now,int L)
{
int pos = lower_bound(block[now] + 1,block[now] + block[now][0] + 1,L) - block[now];
if (block[now][pos] >= L || pos > block[now][0]) --pos;
return pos;
}
int getcom()
{
char ch = getchar();
while (ch != 'Q' && ch != 'R') ch = getchar();
if (ch == 'Q') return 1;
else return 2;
}
void Solve(int j,int now)
{
int typ = Q[j].typ,L,R;
if (typ == 1) {
int ans = 0; L = Q[j].x,R = Q[j].c;
if (belong[L] == belong[R]) {
for (int i = L; i <= R; i++) if (pre[i] < L) ++ans;
printf("%d\n",ans);
return;
}
for (int i = L; i <= to[belong[L]]; i++) if (pre[i] < L) ++ans;
for (int i = belong[L] + 1; i < belong[R]; i++) ans += Query(i,L);
for (int i = from[belong[R]]; i <= R; i++) if (pre[i] < L) ++ans;
printf("%d\n",ans);
}
else {
int x = Q[j].x,c = Q[j].c;
c = lower_bound(a + 1,a + cur + 1,c) - a;
int modi1 = 0,modi2 = belong[x],modi3 = 0;
if (pre[x]) nex[pre[x]] = nex[x]; else pre[nex[x]] = 0;
if (nex[x]) pre[nex[x]] = pre[x],modi1 = belong[nex[x]]; else nex[pre[x]] = 0;
L = ~0U>>1; R = 0; num[x] = c;
for (int i = x - 1; i >= from[belong[x]]; i--)
if (num[i] == num[x]) {L = i; break;}
if (L > x) {
for (L = belong[x] - 1; L; L--)
if (last[L][num[x]] < x && last[L][num[x]]) break;
}
else L = belong[x];
L = last[L][num[x]];
if (L) nex[L] = x,pre[x] = L; else pre[x] = 0;
for (int i = x + 1; i <= to[belong[x]]; i++)
if (num[i] == num[x]) {R = belong[x]; break;}
if (!R) for (R = belong[x] + 1; R <= now; R++)
if (first[R][num[x]] > x && first[R][num[x]] <= n) break;
modi3 = R; R = first[R][num[x]];
if (R <= n && R > x) nex[x] = R,pre[R] = x,Build(modi3); else nex[x] = 0;
if (modi1) Build(modi1);
if (modi2) Build(modi2);
}
}
int main()
{
#ifdef DMC
freopen("maintain18.in","r",stdin);
#endif
cin >> n >> m; Sqrt = sqrt(n);
int now = 1,L = 1,R = Sqrt; tot = n;
for (int i = 1; i <= n; i++) scanf("%d",&num[i]),a[i] = num[i];
for (int i = 1; i <= m; i++) {
int typ,x,c;
typ = getcom(); scanf("%d%d",&x,&c);
Q[i] = data(typ,x,c);
if (typ == 2) a[++tot] = c;
}
sort(a + 1,a + tot + 1);
for (int i = 2; i <= tot; i++)
if (a[i] != a[i-1])
a[++cur] = a[i];
for (int i = 1; i <= n; i++) {
num[i] = lower_bound(a + 1,a + cur + 1,num[i]) - a;
if (tail[num[i]]) nex[tail[num[i]]] = i,pre[i] = tail[num[i]];
tail[num[i]] = i;
if (i > R) {
from[now] = L; to[now] = R;
++now; L += Sqrt; R += Sqrt;
}
belong[i] = now;
}
from[now] = L; to[now] = n;
for (int i = 1; i <= now; i++) Build(i);
pre[0] = nex[0] = 0;
for (T = 1; T <= m; T++) Solve(T,now);
//Solve(3787,now);
return 0;
}