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

和上道题一样就不说了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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\n",get(xx,yy));
        }
    }
}
int main(){
    //freopen("All.in","r",stdin);
    //freopen("a.out","w",stdout);
    using namespace zhangenming;
    init();
    solve();
    return 0;
}

  

 

posted @   zhangenming  阅读(123)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示