[POI2014] KUR-Couriers (主席树)

[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.

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

输入输出格式

输入格式:

The first line of the standard input contains two integers, kur-en-tex.1.png and kur-en-tex.2.png (kur-en-tex.3.png), 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 kur-en-tex.4.png to (at most) kur-en-tex.5.png.

The second line of input contains kur-en-tex.6.png integers, kur-en-tex.7.png (kur-en-tex.8.png), separated by single spaces; kur-en-tex.9.png is the number of the courier company that delivered the kur-en-tex.10.png-th package (in shipment chronology).

The kur-en-tex.11.png lines that follow specify the time period queries, one per line.

Each query is specified by two integers, kur-en-tex.12.png and kur-en-tex.13.png (kur-en-tex.14.png), separated by a single space.

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

In tests worth kur-en-tex.17.png of total score, the condition kur-en-tex.18.png holds, and in tests worth kur-en-tex.19.png of total score kur-en-tex.20.png.

输出格式:

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

(Thus a total of kur-en-tex.21.png 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 kur-en-tex.22.png 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

前置技能:

主席树

主席树学习传送门
对原序列建主席树,对于一段区间,我们先尝试左子树是否可以满足条件,再查右子树,如果都不能就返回0

其实如果会主席树,这道题挺显然的

Code

#include<bits/stdc++.h>
#define in(i) (i=read())
#define il extern inline
#define rg register
#define mid ((l+r)>>1)
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define lol long long
using namespace std;

const lol N=1e6+10;

lol read() {
    lol ans=0, f=1; char i=getchar();
    while (i<'0' || i>'9') {if(i=='-') f=-1; i=getchar();}
    while (i>='0' && i<='9') ans=(ans<<1)+(ans<<3)+(i^48), i=getchar();
    return ans*f;
}

int n,m,tot,num;
int a[N],b[N],rt[N<<6];
struct Chair_Tree {
    int l,r,v;
}t[N<<6];

void build(int &u,int l,int r) {
    u=++tot;
    if(l==r) {t[u].v=a[l]; return;}
    build(t[u].l,l,mid);
    build(t[u].r,mid+1,r);
}

void insert(int &u,int l,int r,int pre,int p) {
    t[u=++tot]=t[pre]; t[u].v++;
    if(l==r) return;
    if(p<=mid) insert(t[u].l,l,mid,t[pre].l,p);
    else insert(t[u].r,mid+1,r,t[pre].r,p);
}

int query(int x,int y,int l,int r,int k) {
    if(l==r) return b[l];
    int ln=t[t[y].l].v-t[t[x].l].v;
    int rn=t[t[y].r].v-t[t[x].r].v;
    if(k<=ln) return query(t[x].l,t[y].l,l,mid,k);
    if(k<=rn) return query(t[x].r,t[y].r,mid+1,r,k);
    return 0;
}

int main()
{
    in(n), in(m);
    for (int i=1;i<=n;i++) in(a[i]),b[i]=a[i];
    sort(b+1,b+1+n); num=unique(b+1,b+1+n)-b-1;
    for (int i=1;i<=n;i++) {
        int p=lower_bound(b+1,b+1+num,a[i])-b;
        insert(rt[i],1,num,rt[i-1],p);
    }
    for (int i=1,l,r,k;i<=m;i++) {
        in(l),in(r), k=(r-l+1)/2+1;
        printf("%d\n",query(rt[l-1],rt[r],1,num,k));
    }
}

转载于:https://www.cnblogs.com/real-l/p/9895767.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值