题目链接: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. 动态替换animator的研究

    http://blog.csdn.net/tonnychu/article/details/49903657 http://blog.csdn.net/tlrainty/article/details ...

  2. 【密码学】RSA算法原理

    RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密. RSA的算法涉及三个参数,n.e1.e2. 其中,n是两个大质数p.q的积,n的二进制 ...

  3. 请以excel管理你的接口测试用例

    闲话休扯,上需求:自动读取.执行excel里面的接口测试用例,测试完成后,返回错误结果并发送邮件通知. 分析: 1.设计excel表格2.读取excel表格3.拼接url,发送请求4.汇总错误结果.发 ...

  4. mysql 存储过程(proceduce)查询一个表的结果插入另外一个表

    公司的时间戳存证业务,对发版过程中间数据处理需要用到存储过程.对此做一个简短记录,以免遗忘. DROP procedure record_timestamp_deal ; ##创建存储过程 creat ...

  5. oracle dblink简介

    database link概述 database link是定义一个数据库到另一个数据库的路径的对象,database link允许你查询远程表及执行远程程序.在任何分布式环境里,database都是 ...

  6. 关于Javascript模块化和命名空间管理的问题说明

    最近闲下来的时候,稍微想了想这个问题.关于Javascript模块化和命名空间管理 [关于模块化以及为什么要模块化] 先说说我们为什么要模块化吧.其实这还是和编码思想和代码管理的便利度相关(没有提及名 ...

  7. 给string添加新的函数

    var str = "abcdefg";String.prototype.constr = function(){ return this.split('').join('-'); ...

  8. c# 远程连接共享文件

    c# 远程连接共享文件 /// <summary> /// 连接远程共享文件夹 /// </summary> /// <param name="path&quo ...

  9. [转]谷歌Chrome浏览器开发者工具教程—JS调试篇

    来源:http://blog.csdn.net/cyyax/article/details/51242720 上一篇我们学习了谷歌Chrome浏览器开发者工具的基础功能,下面介绍的是Chrome开发工 ...

  10. 9th week -the history of program 1950~2020

    We already know that programming language is a formal language designed to communicate instructions ...