Loading

代码-SDOI2009 HH的项链

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define x first
#define y second
#define bg begin()
#define ed end()
#define pb push_back
#define mp make_pair
#define sz(a) int((a).size())
#define R(i,n) for(int i(0);i<(n);++i)
#define L(i,n) for(int i((n)-1);~i;--i)
const int iinf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;

//Data
const int N=1e6;
int n,m,a[N],pre[N],ns[N];
vector<pair<int,int>> q[N];

//FenwickTree
int fen[N];
void add(int i,int v){
    for(;i<n;i|=i+1) fen[i]+=v;
}
int sum(int i,int v=0){
    for(;~i;(i&=i+1)--) v+=fen[i];
    return v;
}

//Main
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n;
    R(i,n) cin>>a[i],--a[i];
    R(i,N) pre[i]=-1;
    cin>>m;
    R(i,m){
        int l,r; cin>>l>>r,--l,--r;
        q[r].pb(mp(l,i));
    }
    R(i,n){
        if(~pre[a[i]]) add(pre[a[i]],-1);
        add(pre[a[i]]=i,1);
        for(pair<int,int> u:q[i])
            ns[u.y]=sum(i)-sum(u.x-1);
    }
    R(i,m) cout<<ns[i]<<'\n';
    return 0;
}
posted @ 2020-11-25 18:47  George1123  阅读(37)  评论(0编辑  收藏  举报