当前位置:网站首页>【ACWing】2521. 数颜色
【ACWing】2521. 数颜色
2022-07-17 19:06:00 【记录算法题解】
题目地址:
https://www.acwing.com/problem/content/description/2523/
墨墨购买了一套 N N N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会像你发布如下指令:Q L R代表询问你从第 L L L支画笔到第 R R R支画笔中共有几种不同颜色的画笔。R P Col把第 P P P支画笔替换为颜色Col。
为了满足墨墨的要求,你知道你需要干什么了吗?
输入格式:
第 1 1 1行两个整数 N , M N,M N,M,分别代表初始画笔的数量以及墨墨会做的事情的个数。
第 2 2 2行 N N N个整数,分别代表初始画笔排中第 i i i支画笔的颜色。
第 3 3 3行到第 2 + M 2+M 2+M行,每行分别代表墨墨会做的一件事情,格式见题干部分。
输出格式:
对于每一个Query的询问,你需要在对应的行中给出一个数字,代表第 L L L支画笔到第 R R R支画笔中共有几种不同颜色的画笔。
数据范围:
1 ≤ N , M ≤ 10000 1≤N,M≤10000 1≤N,M≤10000
修改操作不多于 1000 1000 1000次
所有的输入数据中出现的所有整数均大于等于 1 1 1且不超过 1 0 6 10^6 106。
带修改莫队。莫队基本思想参考https://blog.csdn.net/qq_46105170/article/details/125827776。本题里还有修改操作,所以我们挪动指针的时候,除了挪动区间端点的两个指针以外,还需要挪动时间指针。首先将每个询问和修改都存起来,存修改的时候,数组下标即为此次修改的时间戳;这样对于每个询问操作,除了记录该询问的编号、左右端点之外,还要把时间戳记录下来。
接下来类似莫队的操作,对询问排序,先按左端点所在分块的编号递增排,左端点所在块相同则按右端点所在分块的编号递增排,如果还相同,为了时间指针移动次数尽量少,按时间递增排。
接下来处理每个询问,开三个指针 i , j , t i, j, t i,j,t分别代表左右端点的指针和时间指针。一开始 t = 0 t=0 t=0。先按基础莫队的办法,将 i i i和 j j j移动到左右端点,同时更新计数。端点指针移动到位之后,开始移动时间指针。如果 t t t小于询问时间,那么我们顺着时间增长的方向修改数组,并且如果修改的位置是区间内的,还需要更新答案;如果 t t t大于询问时间,那么我们顺着时间逆流的方向修改数组,同样的,如果修改的位置是区间内的,还需要更新答案。为了方便操作,在修改的时候,我们直接将被修改的数和修改操作里记录的数做交换,这样时间顺流的时候如果修改是 x → y x\to y x→y,那么时间逆流的时候修改就是 y → x y\to x y→x,这样恰好满足需求。
考虑指针移动的总次数。设块的大小为 a a a,则 j j j指针移动次数为 O ( a m + 2 n ) O(am+2n) O(am+2n), t t t指针移动次数为 O ( n 2 2 a 2 T ) O(\frac{n^2}{2a^2}T) O(2a2n2T), T T T为时间戳最大值,即修改总次数(固定 i i i和 j j j所在块之后, t t t最多移动 T T T次,而所在块的方案数大概是 n 2 2 a 2 \frac{n^2}{2a^2} 2a2n2)。而对于 i i i指针, j j j块内移动的时候,其最多移动 O ( n ) O(n) O(n),总共 O ( n 2 a ) O(\frac{n^2}{a}) O(an2),当 j j j跨块的时候,总次数 O ( n a m ) O(\frac{n}{a}m) O(anm),所以总共是 O ( a m + 2 n + n 2 2 a 2 T + n 2 a + n a m ) O(am+2n+\frac{n^2}{2a^2}T+\frac{n^2}{a}+\frac{n}{a}m) O(am+2n+2a2n2T+an2+anm)。如果分块每块大小 n \sqrt n n的话,每次询问平均需要 O ( n + T ) O(\sqrt n+T) O(n+T)(注意本题中 n ∼ m n\sim m n∼m)。
代码如下:
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 1e4 + 10, S = 1e6 + 10;
int n, m, len, mc, mq;
int w[N], cnt[S], res[N], ans;
struct Query {
int id, l, r, t;
} q[N];
// c[t]代表t这个时间戳,做了将w[p]变成c的操作。t从1开始
struct Modify {
int p, c;
} c[N];
void add(int x) {
if (!cnt[x]) ans++;
cnt[x]++;
}
void del(int x) {
cnt[x]--;
if (!cnt[x]) ans--;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &w[i]);
for (int i = 0; i < m; i++) {
char op[2];
int a, b;
scanf("%s%d%d", op, &a, &b);
if (*op == 'Q') mq++, q[mq] = {
mq, a, b, mc};
else c[++mc] = {
a, b};
}
len = sqrt(n);
sort(q + 1, q + 1 + mq, [&](Query& a, Query& b) {
int al = a.l / len, bl = b.l / len;
if (al != bl) return al < bl;
int ar = a.r / len, br = b.r / len;
// 单调优化(玄学优化)
if (ar != br) if (al & 1) return ar < br; else return ar > br;
return a.t < b.t;
});
for (int k = 1, i = 0, j = 1, t = 0; k <= mq; k++) {
int id = q[k].id, l = q[k].l, r = q[k].r, tm = q[k].t;
while (i < r) add(w[++i]);
while (i > r) del(w[i--]);
while (j < l) del(w[j++]);
while (j > l) add(w[--j]);
while (t < tm) {
t++;
if (l <= c[t].p && c[t].p <= r) {
del(w[c[t].p]);
add(c[t].c);
}
swap(w[c[t].p], c[t].c);
}
while (t > tm) {
if (l <= c[t].p && c[t].p <= r) {
del(w[c[t].p]);
add(c[t].c);
}
swap(w[c[t].p], c[t].c);
t--;
}
res[id] = ans;
}
for (int i = 1; i <= mq; i++) printf("%d\n", res[i]);
}
预处理时间复杂度 O ( m q log m q ) O(m_q\log m_q) O(mqlogmq), m q m_q mq为询问次数,每次询问时间 O ( n + T ) O(\sqrt n+T) O(n+T), T T T为最大时间戳,空间 O ( m + S ) O(m+S) O(m+S), S S S为不同颜色数量。
边栏推荐
- STL string find substring
- 弹性负载均衡将访问流量自动分发到多台云服务器,扩展应用系统对外的服务能力,提高应用程序安全性。
- Responsive dream weaving template wine cellar website
- Weekly summary (*65): planned output
- [code hoof set novice village question 600] format specifier of float and double
- 忘掉Postman,Apifox更好用
- 深度学习从入门到放弃100天挑战
- STL string output and modification
- [7.14] code source - [open box] [XOR inverse] [continuous subsequence] [trigonometric fruit count]
- Forget about postman. Apifox is better
猜你喜欢

onvif协议相关:3.1.4 Digest方式获取流地址

「技术播客月」Day 10: Meta Podcast: 聊聊播客这件事
![Codeforce:a. doremy's IQ [reverse greed]](/img/3d/065f9f1cbd857d324b6ed074d1afbf.png)
Codeforce:a. doremy's IQ [reverse greed]
![[code hoof set novice village question 600] format specifier of float and double](/img/19/433f794617f3c3c72732f1a4efe252.png)
[code hoof set novice village question 600] format specifier of float and double

onvif协议相关:3.1.2 Digest方式获取token列表

Onvif protocol related: 4.1.3 WS username token method to obtain screenshot URL

Responsive Zhimeng template logistics and freight service website
![[postgraduate entrance examination vocabulary training camp] day 5 - alarm, cooperate, point, benefit, industrial, revolution, mechanism](/img/50/07dd47d1bc46681e19d133b2e219c4.png)
[postgraduate entrance examination vocabulary training camp] day 5 - alarm, cooperate, point, benefit, industrial, revolution, mechanism
![Codeforce:a. difference operations [mathematical thinking]](/img/be/28bcb5dd8b9a36f2955f1912f289a3.png)
Codeforce:a. difference operations [mathematical thinking]
![[Tencent blue whale] the seventh 7.24 operation and maintenance day holiday greetings ~ come and make a wish~](/img/60/ab96abd599230078b19abad0aecba2.png)
[Tencent blue whale] the seventh 7.24 operation and maintenance day holiday greetings ~ come and make a wish~
随机推荐
codeforce:A. Doremy‘s IQ【反向贪心】
Design and Simulation of anti reverse connection circuit based on MOS transistor
OpenSSL operation
Hello, everyone. How to synchronize binlog in real time before the database starts? Is there a good scheme
Health prevention guide 3: health care
2. Sum of three numbers
【考研词汇训练营】Day 6 —— eventually,state,create,productivity,stimulate
【6.15】Codeforces Round #798 (Div. 2)
Codeforces Round #808 (Div. 2)ABCD
【南瓜书ML】(task2)线性模型的数学推导(最小二乘估计、广义瑞利商、极大似然估计等)
asterisk: rejected because extension not found in context ‘from-ipphone‘
Programming examples of stm32f1 and stm32subeide -mpu-6050 six axis (gyroscope + accelerometer) drive
Some common operation commands of the command line and solutions to common errors
【码蹄集新手村 600 题】运算符 / 在不同的运算顺序中的类型转换
弹性负载均衡将访问流量自动分发到多台云服务器,扩展应用系统对外的服务能力,提高应用程序安全性。
Framework construction of business card management
Simulation of overvoltage protection (OVP) circuit based on PMOS
S32K148_ Can drive (bare metal development)
onvif协议相关:3.1.1 Digest方式获取Authorization
【6.15】Codeforces Round #798 (Div. 2)