SPOJ DQUERY 主席树+lazy+乱搞

题意要求a,b区间中不同数的个数。

我们从头往后扫,对于前面已经出现过的我们不用管,因为我们现在要的是加入扫到p这个店就求以这个点为结尾的查询,至于之前已经出现过的加入出现在q,那么得在q~p-1每个数减一用lazy维护。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<map>
#include<vector>
using namespace std;
const int maxn=30015;
struct pi{
    int sum;
    int lson;
    int rson;
}pp[maxn*20];
int root[maxn],tot;
void build(int cnt,int le,int ri){
    pp[cnt].sum=0;
    if(le==ri) return ;
    tot++;
    pp[cnt].lson=tot;
    build(tot,le,(le+ri)/2);
    tot++;
    pp[cnt].rson=tot;
    build(tot,(le+ri)/2+1,ri);
}
void merg(int qq,int cnt,int n,int p,int k){
    int le,ri,mid;
    le=1;
    ri=n;
    while(le<=ri){
        pp[cnt]=pp[qq];
        pp[cnt].sum+=k;
        if(le==ri) break;
        mid=(le+ri)/2;
        if(p<=mid){
            pp[cnt].lson=tot+1;
            cnt=tot+1;
            qq=pp[qq].lson;
            tot++;
            ri=mid;
        }
        else{
            pp[cnt].lson=tot+1;
            cnt=tot+1;
            qq=pp[qq].rson;
            tot++;
            le=mid+1;
        }
    }
}
int a[maxn],b[maxn];
struct ppi{
    int x,y,id;
}pp1[maxn*10];
struct pppi{
    int le;
    int ri;
    int lazy;
    int sum;
}pp2[maxn<<2];
void build1(int tot,int le,int ri){
    pp2[tot].le=le;
    pp2[tot].ri=ri;
    pp2[tot].lazy=0;
    pp2[tot].sum=0;
    if(le==ri) return ;
    build1(2*tot,le,(le+ri)/2);
    build1(2*tot+1,(le+ri)/2+1,ri);
}
void merg1(int tot,int l,int r,int k){
    if(pp2[tot].le>=l&&pp2[tot].ri<=r){
        pp2[tot].lazy+=k;
        return ;
    }
    int mid=(pp2[tot].le+pp2[tot].ri)/2;
    pp[tot].sum+=k*(min(r,pp2[tot].ri)-max(l,pp2[tot].le)+1);
    if(l<=mid) merg1(2*tot,l,r,k);
    if(r>mid) merg1(2*tot+1,l,r,k);
}
int query1(int tot,int l,int r){
    if(pp2[tot].le>=l&&pp2[tot].ri<=r){
        return pp2[tot].lazy*(pp2[tot].ri-pp2[tot].le+1)+pp2[tot].sum;
    }
    int s=0,mid=(pp2[tot].le+pp2[tot].ri)/2;
    if(l<=mid) s+=query1(2*tot,l,r);
    if(r>mid) s+=query1(2*tot+1,l,r);
    s+=pp2[tot].lazy*(min(pp2[tot].ri,r)-max(pp2[tot].le,l)+1);
    return s;
}
int c[maxn*10];
int cmp(ppi a,ppi b){
    return a.y<b.y;
}
vector<ppi >g[maxn];
map<int,int>mm;
int main()
{
    int i,j,n,m;
    cin>>n;
    tot=0;
    root[0]=0;
    build(0,1,n);
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    for(i=1;i<=n;i++){
        a[i]=(int)(lower_bound(b+1, b+1+n,a[i])-b);
    }
    cin>>m;
    for(i=0;i<m;i++){
        scanf("%d%d",&pp1[i].x,&pp1[i].y);
        pp1[i].id=i;
    }
    sort(pp1,pp1+m,cmp);
    for(i=0;i<m;i++){
        g[pp1[i].y].push_back(pp1[i]);
    }
    mm.clear();
    build(0,1,n);
    build1(1,1,n);
    root[0]=0;
    for(i=1;i<=n;i++){
        if(mm[a[i]]){
            merg1(1,mm[a[i]],i-1,-1);
            root[i]=root[i-1];
        }
        else{
            root[i]=tot+1;
            tot++;
            merg(root[i-1],root[i],n,a[i],1);
        }
        mm[a[i]]=i;
        int p=g[i].size();
        for(j=0;j<p;j++){
            c[g[i][j].id]=pp[root[i]].sum-pp[root[g[i][j].x-1]].sum-query1(1,g[i][j].x-1,g[i][j].x-1);
        }
    }
    for(i=0;i<m;i++){
        printf("%d\n",c[i]);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值