原题传送门

这题要用动态莫队,我博客里有介绍

这道题和luogu P1903 [国家集训队]数颜色 / 维护队列差不多,解法就在上面那篇博客里qaq

主要的问题是如何排序?

排序有三个关键字:

1.左端点所在块数 2.右端点所在块数 3.在这次修改之前查询的次数

在写莫队模板后面还要加上修改操作

注意,序列是从0n-1,查询是从lr-1

#include <bits/stdc++.h>
#define N 50005
using namespace std;
inline int read()
{
register int x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
inline void write(register int x)
{
if(!x)putchar('0');if(x<0)x=-x,putchar('-');
static int sta[25];int tot=0;
while(x)sta[tot++]=x%10,x/=10;
while(tot)putchar(sta[--tot]+48);
}
struct change{
int pos,pre,suf;
}ch[N];
struct query{
int l,r,id,time,bll,blr;
}q[N];
int a[N],blocksize=0,num[1000001],ans[N];
inline bool cmp(register query a,register query b)
{
return a.bll!=b.bll?a.bll<b.bll:(a.blr!=b.blr?a.blr<b.blr:a.time<b.time);
}
int main()
{
int n=read(),m=read(),tota=0,totb=0;
for(register int i=1;i<=n;++i)
a[i]=read();
for(register int i=1;i<=m;++i)
{
char cha=getchar();
while(cha!='Q'&&cha!='M')
cha=getchar();
if(cha=='Q')
{
int l=read()+1,r=read();
++tota;
q[tota]=(query){l,r,tota,totb,0,0};
}
else
{
int pos=read()+1,x=read();
++totb;
ch[totb]=(change){pos,a[pos],x};
a[pos]=x;
}
}
blocksize=ceil(exp((log(n)+log(tota))/3));
for(register int i=1;i<=tota;++i)
q[i].bll=(q[i].l-1)/blocksize+1,q[i].blr=(q[i].r-1)/blocksize+1;
for(register int i=totb;i>=1;--i)
a[ch[i].pos]=ch[i].pre;
sort(q+1,q+tota+1,cmp);
int l=1,r=0,t=0,sum=0;
for(register int i=1;i<=tota;++i)
{
int ll=q[i].l,rr=q[i].r,tt=q[i].time;
while(ll<l)
sum+=!num[a[--l]]++;
while(ll>l)
sum-=!--num[a[l++]];
while(rr>r)
sum+=!num[a[++r]]++;
while(rr<r)
sum-=!--num[a[r--]];
while(t<tt)
{
int pos=ch[++t].pos;
if(l<=pos&&pos<=r)
sum-=!--num[a[pos]];
a[pos]=ch[t].suf;
if(l<=pos&&pos<=r)
sum+=!num[a[pos]]++;
}
while(t>tt)
{
int pos=ch[t].pos;
if(l<=pos&&pos<=r)
sum-=!--num[a[pos]];
a[pos]=ch[t--].pre;
if(l<=pos&&pos<=r)
sum+=!num[a[pos]]++;
}
ans[q[i].id]=sum;
}
for(register int i=1;i<=tota;++i)
write(ans[i]),printf("\n");
return 0;
}

【题解】Luogu UVA12345 Dynamic len(set(a[L:R]))的更多相关文章

  1. Uva 3767 Dynamic len(set(a[L:R])) 树套树

    Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...

  2. (待修莫队 没过! 抽空在检查)Dynamic len(set(a[L:R])) UVA - 12345

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

  3. Dynamic len(set(a[L:R])) UVA - 12345(这么过分一定要写博客)

    给出一个有n个元素的数组,有以下两种操作:Q x y,求出区间[x,y)内不同元素的个数, M x y,把第x个元素的值修改为y.注意题目中的下标是从0开始的 这题超级超级坑 妈的一个水题找了几个小时 ...

  4. [UVa12345] Dynamic len (带 修 )

    题意:有n个数编号从0→n-1,两种操作:            Q L R:询问编号为L→R-1的数中共有多少种不同的数             M X Y:将编号为X的数改为Y           ...

  5. UVA 12345 Dynamic len(带修莫队)

    Dynamic len [题目链接]Dynamic len [题目类型]带修莫队 &题解: 莫队可以单点更改,只要再多加一维,代表查询次数,排序的时候3个关键字. 之后循环离线的时候,先暴力时 ...

  6. [题解] Luogu P5446 [THUPC2018]绿绿和串串

    [题解] Luogu P5446 [THUPC2018]绿绿和串串 ·题目大意 定义一个翻转操作\(f(S_n)\),表示对于一个字符串\(S_n\), 有\(f(S)= \{S_1,S_2,..., ...

  7. luogu P2617 Dynamic Rankings && bzoj 1901 (带修改区间第k大)

    链接:https://www.luogu.org/problemnew/show/P2617 思路: 如果直接在主席树上修改的话,每次修改都会对后面所有的树造成影响,一次修改的复杂度就会变成 : n* ...

  8. 题解 Luogu P1110 【[ZJOI2007]报表统计】

    感谢 @cmy962085349 提供的hack数据,已经改对了. 先声明,我好像是题解里写双$fhq$ $treap$里唯一能过的...(最后两个点啊) 思路:首先看题目,$MIN_GAP_SORT ...

  9. Luogu P2617 Dynamic Rankings

    带修主席树的模板,因为状态不好所以敲了很长时间,不过写完感觉能更好地理解主席树了. 核心其实就是树状数组套主席树,维护方法不再是以前的那种一步一修改,而是对于树状数组上的每一个点建立一棵权值线段树,然 ...

随机推荐

  1. 基于jquery ajax的多文件上传进度条

    效果图 前端代码,基于jquery <!DOCTYPE html> <html> <head> <title>主页</title> < ...

  2. 看开源代码利器—用Graphviz + CodeViz生成C/C++函数调用图(call graph) - 转

    From http://www.linuxidc.com/Linux/2015-01/111501.htm 实际按照上文操作,主要是安装gcc-4.6.2出现一些问题,原先在cygwin下安装,结果提 ...

  3. 从零开始一起学习SLAM | 为啥需要李群与李代数?

    很多刚刚接触SLAM的小伙伴在看到李群和李代数这部分的时候,都有点蒙蒙哒,感觉突然到了另外一个世界,很多都不自觉的跳过了,但是这里必须强调一点,这部分在后续SLAM的学习中其实是非常重要的基础,不信你 ...

  4. cocos2d JS-(JavaScript) 基础语法间的函数方法相互调用

    1.函数嵌套函数 function calcuate(opr, a, b) { // 定义函数,opr - -> 符号,a,b - -> 数值 //定义 + 函数 function add ...

  5. scrapy 参考教程及安装

    scrapy 参考教程及安装环境: win7/10 64bit, python 3.6.x教程: http://python.jobbole.com/86405/安装过程0. 预先安装 VC14 64 ...

  6. Hello py

    https://www.cnblogs.com/AdaminXie/p/8339863.html https://www.cnblogs.com/-clq/p/8340515.html https:/ ...

  7. django 定义文章url

    url(r'^firstcontent/'+str(date)+'/(?P<first_body_id>\d+)/$', views.firstcontent, name='firstco ...

  8. pdf转txt

    ubuntu pdf转jpg或txt chenlei posted @ 2009年12月30日 17:22 inLinux , 1818 阅读 呵呵,刚刚在网上定购了一款mp5,后来才发现它不支持PD ...

  9. python2.7之乱码问题

    python 3之后当然不存在乱码问题了.python 2的乱码问题有时就有点头疼了.(代码均为在windows下测试) 示例:保存为test1.py 报错信息如下: 解决办法: 我将代码保存为tes ...

  10. html5-微格式-时间的格式

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...