题目大意

区间众数

题解

莫队

代码
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define maxn 200010
#define blo 330
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
void write(int x)
{
if(x==0){putchar('0'),putchar('\n');return;}
int f=0;char ch[20];
if(x<0)putchar('-'),x=-x;
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar('\n');
return;
}
int n,m,a[maxn],num[maxn],num2[maxn],nowans,nowl,nowr;
struct node {int l,r,id,ans;}q[maxn];
struct node2 {int a,id;}b[maxn];
bool cmp1(node x,node y){return x.r<y.r;}
bool cmp2(node x,node y){return x.l<y.l;}
bool cmp3(node x,node y){return x.id<y.id;}
bool cmp(node2 x,node2 y){return x.a<y.a;}
bool cmp4(node2 x,node2 y){return x.id<y.id;}
void push(int x){num2[num[x]]--,num[x]++,num2[num[x]]++;while(num2[nowans+1]>0)nowans++;}
void del(int x){num2[num[x]]--,num[x]--,num2[num[x]]++;while(!num2[nowans])nowans--;}
int main()
{
n=read(),m=read();
rep(i,1,n)b[i].a=read(),b[i].id=i;
sort(b+1,b+n+1,cmp);a[b[1].id]=1;
rep(i,2,n)
{
if(b[i].a!=b[i-1].a)a[b[i].id]=a[b[i-1].id]+1;
else a[b[i].id]=a[b[i-1].id];
}
rep(i,1,m)q[i].l=read(),q[i].r=read(),q[i].id=i;
int lim=ceil(1.0*m/(1.0*blo));
sort(q+1,q+m+1,cmp2);
rep(i,0,lim-1){int l=i*blo+1,r=min((i+1)*blo,m);sort(q+l,q+r+1,cmp1);}
num[a[1]]=1,num2[1]++,nowl=nowr=1,nowans=1;
rep(i,1,m)
{
while(nowl>q[i].l)nowl--,push(a[nowl]);
while(nowr<q[i].r)nowr++,push(a[nowr]);
while(nowl<q[i].l)del(a[nowl]),nowl++;
while(nowr>q[i].r)del(a[nowr]),nowr--;
q[i].ans=-nowans;
}
sort(q+1,q+m+1,cmp3);
rep(i,1,m)write(q[i].ans);
return 0;
}

难度在于读题

并不对劲的p3709:大爷的字符串题的更多相关文章

  1. P3709 大爷的字符串题 (莫队)

    题目 P3709 大爷的字符串题 题意:求\([l,r]\)中众数的个数. 解析 维护两个数组: \(cnt[x]\),数\(x\)出现的次数. \(sum[x]\),出现次数为\(x\)的数的个数. ...

  2. P3709 大爷的字符串题(莫队+结论)

    题目 P3709 大爷的字符串题 做法 有一个显然的结论:一段区间里最小答案为众数的个数 用莫队来离线求众数 \(tmp_i\)表示出现\(i\)次的数的个数,\(num_i\)表示\(i\)出现的次 ...

  3. luogu P3709 大爷的字符串题

    二次联通门 : luogu P3709 大爷的字符串题 /* luogu P3709 大爷的字符串题 莫队 看了半天题目 + 题解 才弄懂了要求什么... 维护两个数组 一个记录数字i出现了几次 一个 ...

  4. 洛谷 P3709 大爷的字符串题

    https://www.luogu.org/problem/show?pid=3709 题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个 ...

  5. 洛谷P3709 大爷的字符串题(莫队)

    题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个字符串题: 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区 ...

  6. P3709 大爷的字符串题(50分)

    题目背景 在那遥远的西南有一所学校 /*被和谐部分*/ 然后去参加该省省选虐场 然后某蒟蒻不会做,所以也出了一个字符串题: 题目描述 给你一个字符串a,每次询问一段区间的贡献 贡献定义: 每次从这个区 ...

  7. P3709 大爷的字符串题

    题意 询问区间众数出现的次数 思路 唯有水题快人心 离散化+莫队 莫队一定要先加后减,有事会出错的 莫队维护区间众数: 维护两个数组,一个数组记录权值为x的出现次数,一个记录出现次数为x的数的个数 a ...

  8. 【题解】洛谷P3709大爷的字符串题

    最近想要练习一下莫队(实在是掌握的太不熟练了啊.)这题一开始看到有点懵(题面杀),后来发现是要求众数的个数.乍一看好像很难的样子. 但仔细分析一下:首先往序列当中加入一个数,这个是很简单的,只需要维护 ...

  9. 【luogu P3709 大爷的字符串题】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3709 离散化+区间众数..? #include <iostream> #include < ...

随机推荐

  1. step 1:begin to identify something in english(to becaome a baby again)

    long long ago , i think if i want to improve my english especially computer english . i must do so m ...

  2. BZOJ1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名

    n<=1000头牛各有一个未知值Ai,已知m<=10000条形如Ax>Ay的不等关系,求将整个序列排序的最少比较次数. Aa>Ab,Ab>Ac -------> A ...

  3. windows7 下安装使用memcached(二)

    Memcached 安装使用 本地环境:Windows7 64位web环境:wamp集成环境,php版本:PHP Version 7.1.17 学习参考网站: RUNOOB.COM官网  http:/ ...

  4. 240.Search in a 2D Matrix II

    /* * 240.Search in a 2D Matrix II * 2016-6-17by Mingyang * From left-bottom to right-top * 他这道题目虽说是用 ...

  5. MySQL命令行自动补全表名

    注意:在命令行下只有切换到数据库之后,才能补全表名,对于命令是不能补全的. 1.my.conf增加如下配置: [mysql] #no-auto-rehash auto-rehash #添加auto-r ...

  6. win7电脑定时开机设置方法

    在BIOS设置主界面中选择“Power Management Setup”,进入“电源管理”窗口. 注:缺省情况下,“Resume By Alarm”定时开机选项是关闭的. 将鼠标移到“Resume ...

  7. InfluxDB useful commands

    InfluxDB 配置文件地址:/etc/influxdb/influxdb.conf 通过curl写数据 curl -i -XPOST 'http://localhost:8086/write?db ...

  8. centos 安装python2.7

    安装pip sudo yum -y install epel-release sudo yum -y install python-pip 下载解压Python-2.7.3 #wget http:// ...

  9. VB6 如何添加自定义函数 模块 把代码放到一个模块中

    1 工程-添加模块,在右侧工程视图中可以发现多了一个Module1   2 比如我在这个模块中自定义两个函数,分别为写入和读取INI的函数   3 则在主程序中已经可以直接调用  

  10. js 终止执行的实现方法

    终止JS运行有如下几种可能: 1.终止函数的运行的方式有两种 (1)在函数中使用return,则当遇到return时,函数终止执行,控制权继续向下运行 (2)在函数中使用try-catch异常处理,需 ...