洛谷 P3709 大爷的字符串题 莫队

题目背景

在那遥远的西南有一所学校

/被和谐部分/

然后去参加该省省选虐场

然后某蒟蒻不会做,所以也出了一个字符串题:

题目描述

给你一个字符串a,每次询问一段区间的贡献

贡献定义:

每次从这个区间中随机拿出一个字符x,然后把x从这个区间中删除,你要维护一个集合S

如果S为空,你rp减1

如果S中有一个元素不小于x,则你rp减1,清空S

之后将x插入S

由于你是大爷,平时做过的题考试都会考到,所以每次询问你搞完这段区间的字符之后最多还有多少rp?rp初始为0

询问之间不互相影响~

输入输出格式

输入格式:
第一行两个数n,m,表示字符串长度与询问次数

之后一行n个数,表示字符串

由于你是大爷,所以字符集1e9

之后m行每行两个数,表示询问的左右区间

输出格式:
m行,每行一个数表示答案

输入输出样例

输入样例#1: 复制
3 3
3 3 3
3 3
3 3
3 3
输出样例#1: 复制
-1
-1
-1
说明

前4个点1s,后面的点4s

对于10%的数据,是样例

对于另外10%的数据,n,m <= 100

对于另外10%的数据,n,m <= 1000

对于另外10%的数据,n,m <= 10000

对于另外10%的数据,n,m <= 100000

对于100%的数据,n,m <= 200000

保证数据向某省省选day1T2一样sb,大家尽情用暴力水过题吧!

没事,你只要在一个好学校,就算这题只能拿到10分,也可以进队了

分析:题目意思是求一个区间出现最多的数的个数。莫队就ok了,可以多打一个堆。反正后面的点有4s,O(n*sqrt(n)*log n)不会炸,好像洛谷上没有4s?

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

const int maxn=1000001;

using namespace std;

int belong[maxn],sum[maxn],t[maxn],g[maxn],a[maxn],f[maxn];
int n,i,m,block,l,r,ans,cnt,last;

struct node{
    int id,l,r,ans;
}q[maxn];

struct get{
    int x,rank;
}c[maxn];

bool cmp1(node x,node y)
{
    if (belong[x.l]==belong[y.l]) return x.r<y.r;
    return x.l<y.l;
}

bool cmp2(node x,node y)
{
    return x.id<y.id;
}

bool cmp3(get x,get y)
{
    if (x.x==y.x) return x.rank<y.rank;
    return x.x<y.x;
}

void up(int x)
{
    while ((x>1) && (t[x]>t[x/2]))
    {
        swap(t[x],t[x/2]);
        swap(g[f[x]],g[f[x/2]]);
        swap(f[x],f[x/2]);
        x=x/2;
    }
}

void down(int x)
{
    bool flag=true;
    int s;
    while (flag)
    {
        if ((t[x*2]>t[x*2+1]) || (x*2+1>cnt)) s=x*2;
                                         else s=x*2+1;
        if (t[s]>t[x])
        {
            swap(t[s],t[x]);
            swap(g[f[s]],g[f[x]]);
            swap(f[s],f[x]);
            x=s;
        }
        else flag=false;
    }
}


void updata(int x,int c)
{
    if (c==1)
    {
        t[g[x]]++;
        up(g[x]);
    }
    else
    {
        t[g[x]]--;
        down(g[x]);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++)
    {
        scanf("%d",&c[i].x);
        c[i].rank=i;
        a[i]=c[i].x;
    }    
    sort(c+1,c+n+1,cmp3);       
    int j=1;
    last=c[1].x; a[c[1].rank]=1; 
    for (i=2;i<=n;i++)
    {
        if (c[i].x!=last) j++;
        last=c[i].x;
        a[c[i].rank]=j;
    }           
    for (i=1;i<=j;i++)
    {
        cnt++;
        t[cnt]=0;
        g[i]=cnt;
        f[cnt]=i;
    }  
    for (i=1;i<=m;i++)
    {
        scanf("%d%d",&q[i].l,&q[i].r);
        q[i].id=i;
    }  
    block=trunc(sqrt(n));
    for (i=1;i<=n;i++)
        belong[i]=(i-1)/block+1;
    sort(q+1,q+m+1,cmp1);
    l=1; r=1; updata(a[1],1);     
    for (i=1;i<=m;i++)
    {
        for (r;r<q[i].r;r++) updata(a[r+1],1);
        for (r;r>q[i].r;r--) updata(a[r],0);
        for (l;l<q[i].l;l++) updata(a[l],0);
        for (l;l>q[i].l;l--) updata(a[l-1],1);
        q[i].ans=t[1];
    }
    sort(q+1,q+m+1,cmp2);
    for (i=1;i<=m;i++) printf("-%d\n",q[i].ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值