原题传送门

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

这道题和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 dataTables例子

    https://datatables.net/examples/styling/bootstrap.html http://datatables.club/example/#styling http: ...

  2. WebAPI安全

    1.Oauth Oauth2.0各语言版本公认框架https://oauth.net/code/

  3. 从零开始一起学习SLAM | 你好,点云

    本文提纲 先热热身点云是啥你知道点云优缺点吗?点云库PCL:开发者的福音PCL安装指北炒鸡简单的PCL实践留个作业再走先热热身 小白:hi,师兄,好久不见师兄:师妹好,上周单应矩阵作业做了吗?小白:嗯 ...

  4. cocos2dx JS 游戏切到后台再进入游戏的处理

    由于Cocos引擎中,游戏切入后台后定时器后停掉会让某个Animation动作停止. 导致当重新进入游戏时,看到的不是你想要的画面.或者定时器倒计时的时间对不上等问题. cc.game.EVENT_H ...

  5. C#什么时候需要使用构造函数

    只要使用 new 运算符实例化对象,并且不为 new 提供任何参数,就需要调用默认构造函数.除非类是 static 的,否则 C# 编译器将为无构造函数的类提供一个公共的默认构造函数,以便该类可以实例 ...

  6. spring之继承配置

    我们有一下两个类,并且Gradate类继承了Student类 public class Student public class Gradate extends Student 在applicatio ...

  7. CSS background-image背景图片相关介绍

    这里将会介绍如何通过background-image设置背景图片,以及背景图片的平铺.拉伸.偏移.设置大小等操作. 1. 背景图片样式分类 CSS中设置元素背景图片及其背景图片样式的属性主要以下几个: ...

  8. ling join 报错The specified LINQ expression contains references to queries that are associated with different cont

    The specified LINQ expression contains references to queries that are associated with different cont ...

  9. XMLHttpRequest对象(Ajax)的状态码(readystate) HTTP状态代码(status)

    2018-11-28 14:19:00 来自 :XMLHttpRequest对象(Ajax)的状态码(readystate)  HTTP状态代码(status) XMLHttpRequest对象(Aj ...

  10. Python - 2. Built-in Collection Data Types

    From: http://interactivepython.org/courselib/static/pythonds/Introduction/GettingStartedwithData.htm ...