CF813E Army Creation

CF813E Army Creation

#include<bits/stdc++.h>
#define RG register
#define IL inline
#define _ 100100
#define inf 1e9+7
using namespace std;

IL int gi(){
    RG int data = 0 , m = 1; RG char ch = 0;
    while(ch != '-' && (ch<'0' || ch > '9')) ch = getchar();
    if(ch == '-'){m = 0; ch = getchar();}
    while(ch>='0' && ch<='9'){data = (data<<1) + (data<<3) + ch - '0' ;  ch = getchar();}
    return (m) ? data : -data ; 
}

struct YCB{int ls,rs,sum;}t[30*_] ;
int n,m,K,a[_],pre[_],rt[_],ans,qL,qR,Case,tot,nxt[_],head[_],tail[_],sz[_],cnt;

void Update(int &o,int l,int r,int p){
    t[++tot] = t[o] ; o = tot ;
    t[o].sum ++ ; if(l == r) return;
    RG int mid = (l + r) >> 1;
    if(p <= mid) Update(t[o].ls , l , mid , p) ;
    else if(p > mid) Update(t[o].rs , mid + 1 , r , p) ; 
}
int Query(int o,int l,int r,int ql,int qr){
    if(!o) return 0; if(ql <= l && r <= qr) return t[o].sum ;
    RG int mid = (l + r) >> 1 , sgm = 0;
    if(ql <= mid) sgm += Query(t[o].ls , l , mid , ql , qr) ;
    if(qr  > mid) sgm += Query(t[o].rs , mid + 1 , r , ql , qr) ;
    return sgm ; 
}

int main(){
    n = gi(); K = gi() ;
    for(RG int i = 2; i <= n+1; i ++) a[i] = gi();
    for(RG int i = 2; i <= n+1; i ++){
        if(sz[a[i]] < K) pre[i] = 1;
        else pre[i] = head[a[i]] , head[a[i]] = nxt[head[a[i]]] , sz[a[i]]--;
        nxt[tail[a[i]]] = i ; tail[a[i]] = i;
        if(!head[a[i]]) head[a[i]] = tail[a[i]] ; sz[a[i]] ++;  		
    }
    rt[1] = ++ tot;
    for(RG int i = 2; i <= n+1; i ++){
        rt[i] = rt[i-1] ;
        Update(rt[i] , 1 , n + 1 , pre[i]) ; 
    }
    Case = gi(); ans = 0;
    while(Case --){
        RG int x = gi() , y = gi();
        qL = (x + ans) % n + 1 ;
        qR = (y + ans) % n + 1 ;
        if(qL > qR) swap(qL , qR) ; ++ qL; ++ qR ;
        //cout << "Query [" << qL << "," << qR << "];" << endl;
        ans =
            Query(rt[qR] , 1 , n + 1 , 1 , qL-1) -
            Query(rt[qL-1] , 1 , n + 1 , 1 , qL-1) ;
        printf("%d\n" , ans) ; 
    }return 0;
}

posted @ 2018-04-03 14:49  Guess2  阅读(274)  评论(0编辑  收藏  举报