【luogu P1903 [国家集训队]数颜色】 题解
题目链接: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 [国家集训队]数颜色】 题解的更多相关文章
- Luogu P1903 [国家集训队]数颜色 or 维护队列
标准的带修莫队...咕到了现在$qwq$ 莫队是对询问排序来优化复杂度的(不带修就是对询问区间$[l,r]$排序).. 那么现在带修了,我们再可以维护一个时间维度$tm$:对于每个询问,每次回答前先检 ...
- LUOGU P1903 [国家集训队]数颜色 / 维护队列
传送门 解题思路 带修莫队,第一次写,其实和普通莫队差不多,就是多了个时间轴,块分n^(2/3)最优,时间复杂度O(n^(5/3)). #include<iostream> #includ ...
- Luogu P1903 [国家集训队]数颜色 / 维护队列 (带修莫队)
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...
- bzoj2120 / P1903 [国家集训队]数颜色 / 维护队列(带修改莫队)
P1903 [国家集训队]数颜色 / 维护队列 带修改的莫队 在原有指针$(l,r)$上又添加了时间指针$t$ 贴一段dalao的解释 带修改的莫队,和原版莫队相比,多了一个时间轴 原版莫队是将区间( ...
- 洛谷 P1903 [国家集训队]数颜色 解题报告
P1903 [国家集训队]数颜色 题目描述 墨墨购买了一套\(N\)支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1.Q L R代表询问你从第\(L\) ...
- P1903 [国家集训队]数颜色 / 维护队列(莫队区间询问+单点修改)
题目链接:https://www.luogu.org/problemnew/show/P1903 题目大意:中文题目 具体思路:莫队单点修改+区间询问模板题,在原来的区间询问的基础上,我们要记录当前这 ...
- BZOJ2120/洛谷P1903 [国家集训队] 数颜色 [带修改莫队]
BZOJ传送门:洛谷传送门 数颜色 题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R ...
- ●洛谷P1903 [国家集训队]数颜色
题链: https://www.luogu.org/problemnew/show/P1903题解: 序列带修莫队, 推荐博客https://www.cnblogs.com/Paul-Guderian ...
- P1903 [国家集训队]数颜色 / 维护队列 带修改的莫队
\(\color{#0066ff}{ 题目描述 }\) 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支 ...
随机推荐
- 啊啊啊啊啊啊啊今天就写,炒鸡简单 数据库Sqlite的创建,库的增删改查
啦啦啦啦啦啦啦 写这个不用多长时间,我直接写代码注释都是些语句,Sql语句和Api来操作数据库 ,语句的参数我会注释 SQLite数据库创建数据库需要使用的api:SQLiteOpenHelper必须 ...
- git使用问题及解决方法
1. 设置pull默认rebasegit config --global pull.rebase true 2. 问题解决:Unlink of file '.git/objects/pack/pack ...
- innosetup区分正常状态和静默安装状态(通过传递的参数)
命令行运行程序,如: myprogram.exe /abc /bcd 如果我们想获取其中的参数,“/abc”.“/bcd” 1. 直接使用innosetup自带的方法, GetCmdTail() ...
- centos 6.5搭建LNMP环境
1:查看环境: 1 2 [root@10-4-14-168 html]# cat /etc/redhat-release CentOS release 6.5 (Final) 2:关掉防火墙 1 [r ...
- BNU27932——Triangle——————【数学计算面积】
Triangle Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class nam ...
- openlayers 聚合效果
//cyd var cydclusterSource = new ol.source.Cluster({ distance: 40, source: new ol.source.Vector({ fe ...
- .Net下Redis使用注意事项
.Net下Redis使用注意事项 注:Redis的安装方法和桌面端工具很多,不在本文讨论范围内. 一:不结合适用场景的技术都是耍流氓,Redis主要适用场景: 简单字符串缓存 简单队列 简单发布订阅 ...
- Java Timer定时器原理
做项目很多时候会用到定时任务,比如在深夜,流量较小的时候,做一些统计工作.早上定时发送邮件,更新数据库等.这里可以用Java的Timer或线程池实现.Timer可以实现,不过Timer存在一些问题.他 ...
- mac的svn
http://xclient.info/s/cornerstone.html?t=c5242a66e53f1d866afe8c42aace2738c04ce9ee#versions 破解版的地址 打开 ...
- sharepoint2007就地升级2010系列(一)2007概览
大家可能熟悉了sharepoint2010,熟悉了sharepoint2013,做了N多个项目,但是有没有做过从sharepoint2007升级或者迁移到sharepoint2010的项目呢?我们做实 ...