题目链接:https://www.luogu.org/problemnew/show/P1903

裸的。。。带修莫队。。。

比较麻烦吧(对我来说是的)

两个变量分开记录查询和修改操作。

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define ri register
using namespace std;
const int maxn = ;
int n, m, bl, answer = , qnum = , unum = , curR, curL, now;
int ans[maxn],a[maxn],cnt[maxn];
struct query{
int l, r, p, t;
bool operator < (const query &x) const {
if(l / bl != x.l / bl) return l / bl < x.l / bl;
if(r / bl != x.r / bl) return r / bl < x.r / bl;
return t < x.t;
}
}q[maxn];
struct update{
int pos, val;
}u[maxn];
inline void add(int pos) {
if(++ cnt[pos] == ) answer ++;
}
inline void remove(int pos) {
if(-- cnt[pos] == ) answer --;
}
inline void change(int now, int i) {
if(q[i].l <= u[now].pos && u[now].pos <= q[i].r) {
if( --cnt[a[u[now].pos]] == ) answer--;
if( ++cnt[u[now].val] == ) answer++;
}
swap(a[u[now].pos], u[now].val);
}
int main()
{
scanf("%d%d",&n,&m);
bl = pow(n,1.0/);
for(ri int i = ; i <= n; i++)
scanf("%d",&a[i]);
for(ri int i = ; i <= m; i++)
{
int ch, x, y;
while((ch = getchar()) != 'Q' && ch != 'R');
scanf("%d%d", &x, &y);
if(ch == 'Q') {
q[++ qnum].l = x;
q[qnum].r = y;
q[qnum].t = unum;
q[qnum].p = qnum;
} else {
u[++unum].pos = x;
u[unum].val = y;
}
}
sort(q+,q+qnum+);
curL = q[].l, curR = curL - , now = ;
for(ri int i = ; i <= qnum; i ++) {
while(curL < q[i].l) remove(a[curL ++]);
while(curL > q[i].l) add(a[-- curL]);
while(curR < q[i].r) add(a[++ curR]);
while(curR > q[i].r) remove(a[curR --]);
while(now < q[i].t) change(++ now, i);
while(now > q[i].t) change(now --, i);
ans[q[i].p] = answer;
}
for(ri int i = ; i <= qnum; i++)
printf("%d\n",ans[i]);
return ;
}

【luogu P1903 [国家集训队]数颜色】 题解的更多相关文章

  1. Luogu P1903 [国家集训队]数颜色 or 维护队列

    标准的带修莫队...咕到了现在$qwq$ 莫队是对询问排序来优化复杂度的(不带修就是对询问区间$[l,r]$排序).. 那么现在带修了,我们再可以维护一个时间维度$tm$:对于每个询问,每次回答前先检 ...

  2. LUOGU P1903 [国家集训队]数颜色 / 维护队列

    传送门 解题思路 带修莫队,第一次写,其实和普通莫队差不多,就是多了个时间轴,块分n^(2/3)最优,时间复杂度O(n^(5/3)). #include<iostream> #includ ...

  3. Luogu P1903 [国家集训队]数颜色 / 维护队列 (带修莫队)

    #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...

  4. bzoj2120 / P1903 [国家集训队]数颜色 / 维护队列(带修改莫队)

    P1903 [国家集训队]数颜色 / 维护队列 带修改的莫队 在原有指针$(l,r)$上又添加了时间指针$t$ 贴一段dalao的解释 带修改的莫队,和原版莫队相比,多了一个时间轴 原版莫队是将区间( ...

  5. 洛谷 P1903 [国家集训队]数颜色 解题报告

    P1903 [国家集训队]数颜色 题目描述 墨墨购买了一套\(N\)支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1.Q L R代表询问你从第\(L\) ...

  6. P1903 [国家集训队]数颜色 / 维护队列(莫队区间询问+单点修改)

    题目链接:https://www.luogu.org/problemnew/show/P1903 题目大意:中文题目 具体思路:莫队单点修改+区间询问模板题,在原来的区间询问的基础上,我们要记录当前这 ...

  7. BZOJ2120/洛谷P1903 [国家集训队] 数颜色 [带修改莫队]

    BZOJ传送门:洛谷传送门 数颜色 题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R ...

  8. ●洛谷P1903 [国家集训队]数颜色

    题链: https://www.luogu.org/problemnew/show/P1903题解: 序列带修莫队, 推荐博客https://www.cnblogs.com/Paul-Guderian ...

  9. P1903 [国家集训队]数颜色 / 维护队列 带修改的莫队

    \(\color{#0066ff}{ 题目描述 }\) 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支 ...

随机推荐

  1. Oracle命令整理

    1 常用命令 常用命令 1 sqlplus  scott/tiger@192.168.47.10:1521/orcl      后面不要加: sqlplus  sys/oracle  as sysdb ...

  2. IDEA 导入cordova3.5工程目录注意事项

    IDEA 导入cordova3.5工程目录注意事项 1 eclipse很不稳定,有很多小问题.平时我自己用idea,但是当用cordova3.5创建好工程目录是,用eclipse导入时没有问题的.但是 ...

  3. HDU 1166——敌兵布阵——————【线段树单点增减、区间求和】

    敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  4. 10 个最佳的 Node.js 的 MVC 框架

    补充:http://nokit.org/ https://thinkjs.org/zh-cn/doc/index.html Node.js 是一个基于Chrome JavaScript 运行时建立的一 ...

  5. Oracle新手常遇到的问题

    1.问题描述:尝试加载 Oracle 客户端库时引发 BadImageFormatException.如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题. 解决方 ...

  6. mysql安装与使用

    一.Mysql官方下载地址:https://www.mysql.com/downloads/ 二.下载 Community Server,这个版本是完全免费的  https://dev.mysql.c ...

  7. node express formidable 文件上传后修改文件名

    //我是用php的思想来学习nodejs var express = require('express'); var router = express.Router(); var fs = requi ...

  8. 04.Path类的学习

    path 是路径的意思. path类是一个静态类,所以path是一个工具类. Path类是专门用来操作路径的. Path的常用方法: namespace _15.Path类的学习 { class Pr ...

  9. java集合框架(一):HashMap

    有大半年没有写博客了,虽然一直有在看书学习,但现在回过来看读书基本都是一种知识“输入”,很多时候是水过无痕.而知识的“输出”会逼着自己去找出没有掌握或者了解不深刻的东西,你要把一个知识点表达出来,自己 ...

  10. Python contextlib.contextmanager

    看着代码又发现了一个奇怪的东西: @contextlib.contextmanager def __call__(self, incoming): result_wrapper = [] yield ...