• BZOJ 3524 Poi2014 Couriers


    3524: [Poi2014]Couriers

    Time Limit: 20 Sec  Memory Limit: 256 MB
    Submit: 2471  Solved: 977
    [Submit][Status][Discuss]

    Description

    给一个长度为n的序列a。1≤a[i]≤n。
    m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2。如果存在,输出这个数,否则输出0。

    Input

    第一行两个数n,m。
    第二行n个数,a[i]。
    接下来m行,每行两个数l,r,表示询问[l,r]这个区间。

    Output

    m行,每行对应一个答案。

    Sample Input

    7 5
    1 1 3 2 3 4 3
    1 3
    1 4
    3 7
    1 7
    6 6

    Sample Output

    1
    0
    3
    0
    4

    HINT

    【数据范围】

    n,m≤500000


    2016.7.9重设空间,但未重测!

    Source

    By Dzy

    和上道题一样就不说了

    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    inline int read(){
        int x=0;int f=1;char ch=getchar();
        while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
        while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    const int MAXN=1e6+10;
    struct node{
        int son[2],sum;
    }T[MAXN*5];
    int rsum,a[10000010],b[10000010],siz=0,root[10000010],n,m;
    namespace zhangenming{
        inline void insert(int l,int r,int x,int &y,int v){
            y=++siz;T[y].sum=T[x].sum+1;
            if(l==r) return;
            T[y].son[0]=T[x].son[0];T[y].son[1]=T[x].son[1];
            int mid=(l+r)>>1;
            if(v<=mid) insert(l,mid,T[x].son[0],T[y].son[0],v);
            else insert(mid+1,r,T[x].son[1],T[y].son[1],v);
        }
        inline int get(int L,int R){
            int l=1;int r=n;
            int x=root[L-1];int y=root[R];int tmp=(R-L+1)>>1;
            int mid;
            while(l!=r){
                int mid=(l+r)>>1;
                if(T[y].sum-T[x].sum<=tmp) return 0;
                if(T[T[y].son[0]].sum-T[T[x].son[0]].sum>tmp){
                    y=T[y].son[0];x=T[x].son[0];r=mid;
                }
                else if(T[T[y].son[1]].sum-T[T[x].son[1]].sum>tmp){
                    y=T[y].son[1];x=T[x].son[1];l=mid+1;
                }
                else return 0;
            }
            return l;
        }
        void init(){
            n=read();m=read();
            for(int i=1;i<=n;i++){
                int xx=read();
                insert(1,n,root[i-1],root[i],xx);
            }
        }
        void solve(){
            for(int i=1;i<=m;i++){
                int xx=read();int yy=read();
                printf("%d
    ",get(xx,yy));
            }
        }
    }
    int main(){
        //freopen("All.in","r",stdin);
        //freopen("a.out","w",stdout);
        using namespace zhangenming;
        init();
        solve();
        return 0;
    }
    

      

  • 相关阅读:
    scanf函数读入整数后接着读字符串的换行符残余问题
    二分查找的细节
    PAT 1016 Phone Bills
    1044. Shopping in Mars (25)-PAT甲级真题(二分查找)
    第0次作业
    FPGA开发流程
    quartus2 13.0+modelsim联合开发环境搭建(win10)
    50 years of Computer Architecture: From the Mainframe CPU to the Domain-Specific TPU and the Open RISC-V Instruction Set
    2018.11.26-11.30工作总结
    《步步惊“芯”——软核处理器内部设计分析》前两章读书笔记
  • 原文地址:https://www.cnblogs.com/something-for-nothing/p/7978673.html
Copyright © 2020-2023  润新知