莫队算法模板

推荐阅读这篇博客

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n, m, bse, blc[10005], a[10005], qCnt, cCnt, qwq[10005], ans=0;
int cnt[10005];
char ss[5];
struct Query{
int xxx, yyy, pre, idx;
}q[10005];
struct Change{
int pos, val;
}c[1005];
bool cmp(Query u, Query v){
if(blc[u.xxx]==blc[v.xxx]) return u.yyy<v.yyy;
else return blc[u.xxx]<blc[v.xxx];
}
void add(int val){
if(++cnt[val]==1) ans++;
}
void del(int val){
if(--cnt[val]==0) ans--;
}
void work(int now, int i){
if(c[now].pos>=q[i].xxx && c[now].pos<=q[i].yyy){
del(a[c[now].pos]);
add(c[now].val);
}
swap(c[now].val, a[c[now].pos]);
}
void md(){
int l=1, r=0, now=0;
for(int i=1; i<=qCnt; i++){
while(l<q[i].xxx) del(a[l++]);
while(l>q[i].xxx) add(a[--l]);
while(r<q[i].yyy) add(a[++r]);
while(r>q[i].yyy) del(a[r--]);
while(now<q[i].pre) work(++now, i);
while(now>q[i].pre) work(now--, i);
qwq[q[i].idx] = ans;
}
}
int main(){
cin>>n>>m;
bse = sqrt(n);
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
blc[i] = (i - 1) / bse + 1;
}
while(m--){
scanf("%s", ss);
if(ss[0]=='Q'){
qCnt++;
scanf("%d %d", &q[qCnt].xxx, &q[qCnt].yyy);
q[qCnt].pre = cCnt;
q[qCnt].idx = qCnt;
}
else{
cCnt++;
scanf("%d %d", &c[cCnt].pos, &c[cCnt].val);
}
}
sort(q+1, q+1+qCnt, cmp);
md();
for(int i=1; i<=qCnt; i++)
printf("%d\n", qwq[i]);
return 0;
}

最新文章

  1. arp欺骗技术
  2. Win环境下的文件读写
  3. 用自己的算法实现startsWith和endsWith功能。
  4. 用SpringMvc实现Excel导出功能
  5. 【面向打野编程】——KMP算法入门
  6. Unity3d各平台资源路径文件夹
  7. docker一些命令
  8. java 环境变量设置
  9. jsp界面动态时间显示
  10. WordPress Pie Register插件‘wp-login.php’多个跨站脚本漏洞
  11. (转)Spring事务配置的五种方式
  12. Oracle 常用的十大 DDL 对象
  13. Spring的aop操作
  14. laravel容器container 阅读记录
  15. 程序bug导致了天大的损失,要枪毙程序猿吗?[ZZ]
  16. PHP的五大阶段
  17. centos云服务器安装Python3记录
  18. 模拟jQuery中的ready方法及实现按需加载css,js实例代码
  19. uC/OS-II 函数之消息队列相关函数
  20. 好用的Android屏幕适配

热门文章

  1. mitmproxy抓包软件在mac上边的安装
  2. android配置android studio not found target android-*.的问题
  3. 单列表变量与字符串拆分的对照(SqlServer)
  4. BZOJ 4491: 我也不知道题目名字是什么 RMQ
  5. Android(java)学习笔记129:对ListView等列表组件中数据进行增、删、改操作
  6. [numpy] 基础练习 (一)
  7. Python静态方法 类方法
  8. 用Windows Native API枚举所有句柄及查找文件句柄对应文件名的方法
  9. 12_1_Annotation注解
  10. Java制作桌面弹球下载版 使用如鹏游戏引擎制作 包含2个精灵球同时弹动