UPC组队赛第三场——G: The Famous ICPC Team Again (主席树)

简介: UPC组队赛第三场——G: The Famous ICPC Team Again (主席树)

问题 G: The Famous ICPC Team Again

时间限制: 5 Sec 内存限制: 128 MB


题目描述

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.

输入

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.

输出

For each query, output a single line containing an integer that denotes the difficultness of the problem that Mr. B should choose.

样例输入 Copy

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

样例输出 Copy

Case 1:

3

3

2

Case 2:

6

6

4

题意:

给定一个长度为n的序列和m次询问,每次询问【L,R】区间的中位数。

思路:

中位数可以转化成区间第K大,可以用主席树维护。

代码:

#pragma GCC optimize(3)
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
#define x first
#define y second
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
char F[200];
inline void out(I_int x) {
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0) {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
ll ksm(ll a,ll b,ll p){ll res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
const int inf=0x3f3f3f3f,mod=1000000007;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn=1e5+7,maxm=3e5+7;
const double PI = atan(1.0)*4;
int n,m;
int a[maxn];
vector<int>nums;
struct node{
    int l,r,cnt;
}tr[maxn*20];
int root[maxn],idx;
int Find(int x){
    return lower_bound(nums.begin(),nums.end(),x)-nums.begin();
}
int build(int l,int r){
    int p=++idx;
    if(l==r) return p;
    int mid=(l+r)>>1;
    tr[p].l=build(l,mid);tr[p].r=build(mid+1,r);
    return p;
}
int Insert(int p,int l,int r,int x){
    int q=++idx;
    tr[q]=tr[p];
    if(l==r){
        tr[q].cnt++;
        return q;
    }
    int mid=(l+r)>>1;
    if(x<=mid) tr[q].l=Insert(tr[p].l,l,mid,x);
    else tr[q].r=Insert(tr[p].r,mid+1,r,x);
    tr[q].cnt=tr[tr[q].l].cnt+tr[tr[q].r].cnt;
    return q;
}
int qask(int q, int p, int l, int r, int k)
{
    if (l == r) return r;
    int cnt = tr[tr[q].l].cnt - tr[tr[p].l].cnt;
    int mid=(l+r)>>1;
    if (k <= cnt) return qask(tr[q].l, tr[p].l, l, mid, k);
    else return qask(tr[q].r, tr[p].r, mid + 1, r, k - cnt);
}
int main(){
    int Case=1;
    while(~scanf("%d",&n)){
        printf("Case %d:\n",Case++);
        nums.clear();idx=0;
        memset(tr,0,sizeof tr);memset(root,0,sizeof root);
        for(int i=1;i<=n;i++){
            a[i]=read();nums.push_back(a[i]);
        }
        sort(nums.begin(),nums.end());
        nums.erase(unique(nums.begin(), nums.end()), nums.end());
        root[0]=build(0,nums.size()-1);
        for(int i=1;i<=n;i++)
            root[i]=Insert(root[i-1],0,nums.size()-1,Find(a[i]));
        m=read();
        while(m--){
            int l=read(),r=read(),k=(r-l)/2+1;
            int tmp=qask(root[r],root[l-1],0,nums.size()-1,k);
            out(nums[tmp]);puts("");
        }
    }
    return 0;
}
目录
相关文章
|
Java
hdu2519 新生晚会
hdu2519 新生晚会
63 0
|
人工智能 机器人
第10届山东省赛Wandering Robot(详细思路)
第10届山东省赛Wandering Robot(详细思路)
89 0
upc 2021秋组队训练赛第二场
upc 2021秋组队训练赛第二场
67 1
upc 2021秋组队训练赛第二场
upc2021秋组队训练赛第一场 ICPC North Central NA Contest 2020
upc2021秋组队训练赛第一场 ICPC North Central NA Contest 2020
95 0
upc2021秋组队训练赛第一场 ICPC North Central NA Contest 2020
|
存储
UPC组队第三场——K: A Famous Grid (BFS+细节)
UPC组队第三场——K: A Famous Grid (BFS+细节)
87 0
UPC组队第三场——K: A Famous Grid (BFS+细节)
|
人工智能 BI 网络架构
[计蒜客] ACM-ICPC 2018 南京赛区网络预赛 | 部分题解 | 线段树 + 线性筛 + 最短路(上)
E. AC Challenge 题目描述 输入 输出 样例输入 样例输出 提示 题意: A. An Olympian Math Problem G. Lpl and Energy-saving Lamps 题目描述 输入 输出 样例输入 样例输出 提示 ac_code:
173 0
[计蒜客] ACM-ICPC 2018 南京赛区网络预赛 | 部分题解 | 线段树 + 线性筛 + 最短路(上)
2020ICPC昆明M.Stone Games(主席树)
2020ICPC昆明M.Stone Games(主席树)
88 0
|
数据格式
UPC新生赛—— 排序(思维)
UPC新生赛—— 排序(思维)
113 0
2021牛客国庆集训派对day1E Removal(dp 去重)
2021牛客国庆集训派对day1E Removal(dp 去重)
79 0

热门文章

最新文章