P1198 [JSOI2008]最大数(单调栈,并查集)

传送门

单调栈的做法显然,主要是这道题为什么可以用并查集来代替二分查找

在将一个数插入单调队列时,我们可以将被删除的数的父亲标记为插入的数,在查找时只需要找到查找的数的根,根上的数值即为答案。

证明?不会证qwq

复杂度 O ( n ) O(n) O(n)

code:

#include<cstdio>
using namespace std;
const int Maxn=200010;
struct node
{
    long long x;
    int y;
}a[Maxn];
int m,tot,cnt,f[Maxn];
long long d,t,x,num[Maxn];
char ch[3];
int find(int x)
{
    if(x!=f[x])f[x]=find(f[x]);
    return f[x];
}
int main()
{
    scanf("%lld%lld",&m,&d);
    for(int i=1;i<=m;i++)
    {
        getchar();
        scanf("%s",ch);
        scanf("%lld",&x);
        if(ch[0]=='A')
        {
            x+=t;
            x%=d;
            tot++;
            num[tot]=x;
            f[tot]=tot;
            while(x>a[cnt].x&&cnt)
            {
                f[a[cnt].y]=tot;
                cnt--;
            }
            a[++cnt].x=x;
            a[cnt].y=tot;
        }
        else
        {
            x=tot-x+1;
            int y=find(x);
            t=num[y];
            printf("%lld\n",t);
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值