HDU 4251 The Famous ICPC Team Again 主席树

The Famous ICPC Team Again



Problem Description
 
When Mr. B, Mr. G and Mr. M were preparing for the 2012 ACM-ICPC World Final Contest, Mr. B had collected a large set of contest problems for their daily training. When they decided to take training, Mr. B would choose one of them from the problem set. All the problems in the problem set had been sorted by their time of publish. Each time Prof. S, their coach, would tell them to choose one problem published within a particular time interval. That is to say, if problems had been sorted in a line, each time they would choose one of them from a specified segment of the line.

Moreover, when collecting the problems, Mr. B had also known an estimation of each problem’s difficultness. When he was asked to choose a problem, if he chose the easiest one, Mr. G would complain that “Hey, what a trivial problem!”; if he chose the hardest one, Mr. M would grumble that it took too much time to finish it. To address this dilemma, Mr. B decided to take the one with the medium difficulty. Therefore, he needed a way to know the median number in the given interval of the sequence.
 
Input
 
For each test case, the first line contains a single integer n (1 <= n <= 100,000) indicating the total number of problems. The second line contains n integers xi (0 <= xi <= 1,000,000,000), separated by single space, denoting the difficultness of each problem, already sorted by publish time. The next line contains a single integer m (1 <= m <= 100,000), specifying number of queries. Then m lines follow, each line contains a pair of integers, A and B (1 <= A <= B <= n), denoting that Mr. B needed to choose a problem between positions A and B (inclusively, positions are counted from 1). It is guaranteed that the number of items between A and B is odd.
 
Output
 
For each query, output a single line containing an integer that denotes the difficultness of the problem that Mr. B should choose.
 
Sample Input
 
5 5 3 2 4 1 3 1 3 2 4 3 5 5 10 6 4 8 2 3 1 3 2 4 3 5
 
Sample Output
 
Case 1: 3 3 2 Case 2: 6 6 4
 
题意:
  给你n个数,m个询问
  每次问你l,r这个区间内的 中位数是多少
题解
  离散化
  再套可持久化线段树即可
#include<bits/stdc++.h>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair

typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e5+10, M = 1e6+11, mod = 1e6+3, inf = 2e9;

int n,a[N],q,san[N],fsan[N],b[N],c;
struct cooltree{
        int root[N],l[N*20],r[N*20],v[N*20];
        int sz;
        void init()
        {
            memset(root,0,sizeof(root));
            memset(l,0,sizeof(l));
            memset(r,0,sizeof(r));
            memset(v,0,sizeof(v));
            sz = 0;
        }
        void update(int &k,int ll,int rr,int x)
        {
            ++sz;
            l[sz] = l[k];
            r[sz] = r[k];
            v[sz] = v[k] + 1;
            k = sz;
            if(ll == rr) return ;
            if(x <= mid) update(l[k],ll,mid,x);
            else update(r[k],mid+1,rr,x);
        }
        int ask(int x,int y,int k)
        {
            int ll = 1, rr = c;
            x = root[x-1], y = root[y];
            while(ll != rr)
            {
                int md = (ll+rr)>>1, now = v[l[y]] - v[l[x]];
                if(k <= now) x = l[x], y = l[y], rr = md;
                else x = r[x], y = r[y], ll = mid + 1,k-=now;
            }
            return b[ll];
        }
}T;
int main() {
    int cas = 1;
    while(scanf("%d",&n)!=EOF) {
        T.init();
        for(int i = 1; i <= n; ++i) scanf("%d",&a[i]), b[i] = a[i];
        sort(b+1,b+n+1);
        c = unique(b+1,b+n+1) - b - 1;
        
        for(int i = 1; i <= n; ++i) san[i] = lower_bound(b+1,b+c+1,a[i]) - b;
        for(int i = 1; i <= n; ++i) T.update(T.root[i] = T.root[i-1],1,c,san[i]);
        
        scanf("%d",&q);
        printf("Case %d:
",cas++);
        for(int i = 1; i <= q; ++i) {
            int x,y;
            scanf("%d%d",&x,&y);
            printf("%d
",T.ask(x,y,(T.v[T.root[y]] - T.v[T.root[x-1]] +1)/2));
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zxhl/p/5835405.html