2016 China College Student Programming Competition (Changchun) Sequence II HDU-5919 Chairman Tree

Portal

Article Directory

Title:

Give a length of nnSequence of n , one query at a time[l, r] [l, r][l,r ] , find the median of the position where the number appears for the first time.

Ideas:

First consider how to find the number of different numbers in the interval.
Because the median position is required, we consider building a line segment tree based on the position. Because the position of the first occurrence is required, we insert backwards, remember pre [i] pre[i]the p- R E [ I ] currency andiiThe position of i last time, the position of the current number+ 1 +1each time it is inserted+1 p r e [ i ] pre[i] The position of p r e [ i ] − 1 -11 , it can be made persistent, so when asked[l, r] [l,r][l,r ] , just ask thell[l, r] [l, r] of l tree[l,r ] and within.
We found the number of different numbers in the intervalsum sums u m , then the position of the median is⌊ sum + 1 2 ⌋ \left \lfloor \frac{sum+1}{2} \right \rfloor2sum+1 , which is the querylll tree of⌊ sum + 1 2 ⌋ \ left2sum+1⌋A large number is fine .

//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,m;
int a[N],pre[N];
int root[N],tot;
struct Node
{
    
    
    int l,r;
    int sum;
}tr[N*40];

void insert(int p,int &q,int l,int r,int x,int c)
{
    
    
    q=++tot; tr[q]=tr[p];
    tr[q].sum+=c;
    if(l==r) return;
    int mid=l+r>>1;
    if(x<=mid) insert(tr[p].l,tr[q].l,l,mid,x,c);
    else insert(tr[p].r,tr[q].r,mid+1,r,x,c);
}

int query_sum(int u,int l,int r,int ql,int qr)
{
    
    
    if(!u) return 0;
    if(ql<=l&&r<=qr) return tr[u].sum;
    int mid=l+r>>1,ans=0;
    if(ql<=mid) ans+=query_sum(tr[u].l,l,mid,ql,qr);
    if(qr>mid) ans+=query_sum(tr[u].r,mid+1,r,ql,qr);
    return ans;
}

int query_k(int u,int l,int r,int k)
{
    
    
    if(l==r) return l;
    int mid=l+r>>1;
    if(tr[tr[u].l].sum>=k) return query_k(tr[u].l,l,mid,k);
    else return query_k(tr[u].r,mid+1,r,k-tr[tr[u].l].sum);
}

int main()
{
    
    
//	ios::sync_with_stdio(false);
//	cin.tie(0);

    int _; scanf("%d",&_);
    for(int __=1;__<=_;__++)
    {
    
    
        scanf("%d%d",&n,&m); tot=0;
        for(int i=1;i<=n;i++) root[i]=0; root[n+1]=0;
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=n;i>=1;i--)
        {
    
    
            insert(root[i+1],root[i],1,n,i,1);
            if(pre[a[i]]) insert(root[i],root[i],1,n,pre[a[i]],-1);
            pre[a[i]]=i;
        }
        int p=0;
        printf("Case #%d:",__);
        while(m--)
        {
    
    
            int ql,qr; scanf("%d%d",&ql,&qr);
            int l=min((ql+p)%n+1,(qr+p)%n+1);
            int r=max((ql+p)%n+1,(qr+p)%n+1);
            int sum=query_sum(root[l],1,n,l,r);
            int mid=(sum+1)/2;
            printf(" %d",p=query_k(root[l],1,n,mid));
        }
        puts("");
        for(int i=1;i<=n;i++) pre[a[i]]=0;
    }


	return 0;
}
/*

*/


Guess you like

Origin blog.csdn.net/m0_51068403/article/details/115304887