• P3567 [POI2014]KUR-Couriers


    题目描述

    Byteasar works for the BAJ company, which sells computer games.

    The BAJ company cooperates with many courier companies that deliver the games sold by the BAJ company to its customers.

    Byteasar is inspecting the cooperation of the BAJ company with the couriers.

    He has a log of successive packages with the courier company that made the delivery specified for each package.

    He wants to make sure that no courier company had an unfair advantage over the others.

    If a given courier company delivered more than half of all packages sent in some period of time, we say that it dominated in that period.

    Byteasar wants to find out which courier companies dominated in certain periods of time, if any.

    Help Byteasar out!

    Write a program that determines a dominating courier company or that there was none.

    给一个数列,每次询问一个区间内有没有一个数出现次数超过一半

    输入输出格式

    输入格式:

    The first line of the standard input contains two integers, and (), separated by a single space, that are the number of packages shipped by the BAJ company and the number of time periods for which the dominating courier is to be determined, respectively.

    The courier companies are numbered from to (at most) .

    The second line of input contains integers, (), separated by single spaces; is the number of the courier company that delivered the -th package (in shipment chronology).

    The lines that follow specify the time period queries, one per line.

    Each query is specified by two integers, and (), separated by a single space.

    These mean that the courier company dominating in the period between the shipments of the -th and the -th package, including those, is to be determined.

    In tests worth of total score, the condition holds, and in tests worth of total score .

    输出格式:

    The answers to successive queries should be printed to the standard output, one per line.

    (Thus a total of lines should be printed.) Each line should hold a single integer: the number of the courier company that dominated in the corresponding time period, or if there was no such company.

    输入输出样例

    输入样例#1: 
    7 5
    1 1 3 2 3 4 3
    1 3
    1 4
    3 7
    1 7
    6 6
    
    输出样例#1: 
    1
    0
    3
    0
    4
    

    说明

    给一个数列,每次询问一个区间内有没有一个数出现次数超过一半

    Solution:

      本题主席树板子题

      由于题意是静态查询区间$[l,r]$是否有数出现次数超过区间一半,很显然可以用区间$[1,r]-[1,l-1]$得到$[l,r]$的数出现情况,且查询的答案只可能有1个,于是直接主席树咯。

      在查询时,判断左右区间是否满足数的个数$>$区间长度的一半,有就遍历子树,否则返回0。

      很裸的板子。

    代码:

    /*code by 520 -- 9.12*/
    #include<bits/stdc++.h>
    #define RE register
    #define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
    using namespace std;
    const int N=500005;
    int n,m,tot,rt[N],rk[N];
    struct node{
        int ls,rs,sum;
    }t[N*40];
    
    int gi(){
        int a=0;char x=getchar();
        while(x<'0'||x>'9')x=getchar();
        while(x>='0'&&x<='9')a=(a<<3)+(a<<1)+(x^48),x=getchar();
        return a;
    }
    
    void build(int l,int r,int &rt){
        t[rt=++tot].sum=0;
        if(l==r) return;
        int m=l+r>>1;
        build(l,m,t[rt].ls),build(m+1,r,t[rt].rs);
    }
    
    void update(int l,int r,int c,int lst,int &rt){
        rt=++tot;
        t[rt].ls=t[lst].ls,t[rt].rs=t[lst].rs,t[rt].sum=t[lst].sum+1;
        if(l==r) return;
        int m=l+r>>1;
        if(c<=m) update(l,m,c,t[lst].ls,t[rt].ls);
        else update(m+1,r,c,t[lst].rs,t[rt].rs);
    }
    
    int query(int L,int R,int l,int r,int k){
        if(l==r) return l;
        int m=l+r>>1;
        if(t[t[R].ls].sum-t[t[L].ls].sum>k) return query(t[L].ls,t[R].ls,l,m,k);
        if(t[t[R].rs].sum-t[t[L].rs].sum>k) return query(t[L].rs,t[R].rs,m+1,r,k);
        return 0;
    }    
    
    int main(){
        n=gi(),m=gi();
        build(1,n,rt[0]);
        For(i,1,n) rk[i]=gi(),update(1,n,rk[i],rt[i-1],rt[i]);
        int x,y,ans;
        while(m--){
            x=gi(),y=gi();
            printf("%d
    ",query(rt[x-1],rt[y],1,n,y-x+1>>1));
        }
        return 0;
    }    
  • 相关阅读:
    转:mac下安装homebrew
    jdk1.7下载路径
    转: Mac 使用ADT的问题
    转:mac下安装Sublime Text
    转: SVN和Git的一些用法总结
    转: SVN使用教程总结(图文丰富,相当详细)
    转:Tortoise SVN 版本控制常用操作知识
    Android Java混淆(ProGuard)
    转:美团Android资源混淆保护实践
    【IPC进程间通讯之三】内存映射文件Mapping File
  • 原文地址:https://www.cnblogs.com/five20/p/9636619.html
Copyright © 2020-2023  润新知