zoukankan      html  css  js  c++  java
  • 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:
    ",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;
    }
     
     
     
    
  • 相关阅读:
    题目1202:排序------注意每个数字的后面都有空格
    题目1178:复数集合------------结构体的的比较,cmp()函数的错误让我WA了多次
    题目1041:Simple Sorting-------注意最后一个数字的处理
    题目1034:寻找大富翁---用了sort()函数,注意头文件;
    题目1415:不一样的循环队列------注意细节小地方循环队列注意%M;还有为什么M要加一!!!!!!!!!!!!!
    题目1342:寻找最长合法括号序列II---注意:不要求就近匹配,只要求( 在 )前面的任一个位置即可
    题目1398:移动次数-----最少移动的次数,那么相同的最大值越靠后越好,相同的最小值越靠前越好
    题目1375:陈博的完美主义(25分)----------------题目中对输入数字的长度有要求,所以输入字符串,用atoi()转换函数
    题目1363:欢乐斗地主------用数组计数的应用,注意1和2得排在最后,表示最大
    题目1174:查找第K小数-------qsort()可以直接对数组用;还有用unique(a,a+n);的,服!
  • 原文地址:https://www.cnblogs.com/OvOq/p/14853118.html
Copyright © 2011-2022 走看看