求卡常HDU-5172
  • 板块灌水区
  • 楼主lfxxx_
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/12/14 10:17
  • 上次更新2024/12/14 12:35:55
查看原帖
求卡常HDU-5172
795344
lfxxx_楼主2024/12/14 10:17
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,m;
int a[N];
int tr[N];
void add(int x,int d)
{
    for(int i=x;i<=n;i+=i&-i)
        tr[i]+=d;
}
int sum(int x)
{
    int res=0;
    for(int i=x;i;i-=i&-i)
        res+=tr[i];
    return res;
}
int ans[N];
struct qry{int l,r,id;}q[N];
bool cmp(qry a,qry b){return a.r<b.r;}
int pre[N];
int tree[N<<2];
void pushup(int p){tree[p]=max(tree[p<<1],tree[p<<1|1]);}
void build(int p,int pl,int pr)
{
    if(pl==pr)
    {
        tree[p]=a[pl];
        return;
    }
    int mid=(pl+pr)>>1;
    build(p<<1,pl,mid);
    build(p<<1|1,mid+1,pr);
    pushup(p);
}
int query(int p,int pl,int pr,int l,int r)
{
    if(r<pl||pr<l)return 0;
    if(l<=pl&&pr<=r)return tree[p];
    int mid=(pl+pr)>>1;
    return max(query(p<<1,pl,mid,l,r),query(p<<1|1,mid+1,pr,l,r));

}
void solve()
{
    memset(tr,0,sizeof tr);
    memset(ans,-1,sizeof ans);
    memset(pre,0,sizeof pre);
    for(int i=1;i<=n;++i)
        cin>>a[i];
    build(1,1,n);
    for(int i=1;i<=m;++i)
    {
        cin>>q[i].l>>q[i].r;
        q[i].id=i;
    }
    sort(q+1,q+m+1,cmp);
    int lst=0;
    for(int i=1;i<=m;++i)
    {
        if(query(1,1,n,q[i].l,q[i].r)>q[i].r-q[i].l+1)
        {
            ans[q[i].id]=0;
            continue;
        }
        for(int j=lst+1;j<=q[i].r;++j)
        {
            if(pre[a[j]])add(pre[a[j]],-1);
            add(j,1);
            pre[a[j]]=j;
        }
        lst=q[i].r;
        ans[q[i].id]=((sum(q[i].r)-sum(q[i].l-1))==(q[i].r-q[i].l+1));
    }
    for(int i=1;i<=m;++i)
        cout<<(ans[i]?"YES":"NO")<<'\n';
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>n>>m)
        solve();
}
2024/12/14 10:17
加载中...