【bzoj3524】[Poi2014]Couriers

题目描述

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

输入

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

输出

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

样例输入

7 5

1 1 3 2 3 4 3

1 3

1 4

3 7

1 7

6 6

样例输出

1

0

3

0

4



题解

主席树

同bzoj2223,也不需要离散化。

bzoj2223题解:http://www.cnblogs.com/GXZlegend/p/6292609.html

 1 #include <cstdio>
 2 int si[10000010] , lp[10000010] , rp[10000010] , root[500010] , tot;
 3 void pushup(int x)
 4 {
 5     si[x] = si[lp[x]] + si[rp[x]];
 6 }
 7 void ins(int x , int &y , int l , int r , int p)
 8 {
 9     y = ++tot;
10     if(l == r)
11     {
12         si[y] = si[x] + 1;
13         return;
14     }
15     int mid = (l + r) >> 1;
16     if(p <= mid) rp[y] = rp[x] , ins(lp[x] , lp[y] , l , mid , p);
17     else lp[y] = lp[x] , ins(rp[x] , rp[y] , mid + 1 , r , p);
18     pushup(y);
19 }
20 int query(int x , int y , int l , int r , int p)
21 {
22     if(l == r) return l;
23     int mid = (l + r) >> 1;
24     if(si[lp[y]] - si[lp[x]] > p) return query(lp[x] , lp[y] , l , mid , p);
25     if(si[rp[y]] - si[rp[x]] > p) return query(rp[x] , rp[y] , mid + 1 , r , p);
26     return 0;
27 }
28 int main()
29 {
30     int n , m , i , a , b;
31     scanf("%d%d" , &n , &m);
32     for(i = 1 ; i <= n ; i ++ )
33     {
34         scanf("%d" , &a);
35         ins(root[i - 1] , root[i] , 1 , n , a);
36     }
37     while(m -- )
38     {
39         scanf("%d%d" , &a , &b);
40         printf("%d\n" , query(root[a - 1] , root[b] , 1 , n , (b - a + 1) >> 1));
41     }
42     return 0;
43 }
时间: 2024-07-29 04:07:45

【bzoj3524】[Poi2014]Couriers的相关文章

【BZOJ3831】[Poi2014]Little Bird 单调队列

[BZOJ3831][Poi2014]Little Bird Description In the Byteotian Line Forest there are   trees in a row. On top of the first one, there is a little bird who would like to fly over to the top of the last tree. Being in fact very little, the bird might lack

【BZOJ3526】[Poi2014]Card 线段树

[BZOJ3526][Poi2014]Card Description 有n张卡片在桌上一字排开,每张卡片上有两个数,第i张卡片上,正面的数为a[i],反面的数为b[i].现在,有m个熊孩子来破坏你的卡片了!第i个熊孩子会交换c[i]和d[i]两个位置上的卡片.每个熊孩子捣乱后,你都需要判断,通过任意翻转卡片(把正面变为反面或把反面变成正面,但不能改变卡片的位置),能否让卡片正面上的数从左到右单调不降. Input 第一行一个n.接下来n行,每行两个数a[i],b[i].接下来一行一个m.接下来

bzoj3524/2223 [Poi2014]Couriers

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3524 http://www.lydsy.com/JudgeOnline/problem.php?id=2223 [题解] 由于出现次数超过区间长度的一半的数最多只有1个,所以就可以分两半找了.. # include <stdio.h> # include <string.h> # include <iostream> # include <algorithm

【bzoj 3524】[Poi2014]Couriers

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

【bzoj3834】[Poi2014]Solar Panels 数论

题目描述 Having decided to invest in renewable energy, Byteasar started a solar panels factory. It appears that he has hit the gold as within a few days  clients walked through his door. Each client has ordered a single rectangular panel with specified w

【bzoj3522】[Poi2014]Hotel 树形dp

题目描述 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房(间).三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽满意,你需要让三个房间两两距离相同.有多少种方案能让吉丽满意? 输入 第一行一个数n.接下来n-1行,每行两个数x,y,表示x和y之间有一条边相连. 输出 让吉丽满意的方案数. 样例输入 7 1 2 5 7 2 5 2 3 5 6 4 5 样例输出 5 题解 树形dp 如果树上三个点之间两两距离相同

[BZOJ2223][BZOJ3524][Poi2014]Couriers 主席树

3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2436  Solved: 960[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行,

【主席树】BZOJ3524-[Poi2014]Couriers

[题目大意] 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. [思路] 只有query部分需要稍作修改.由于每个节点代表的是一个大小区间数的总数,所以只需判断左右子数的sum值是否大于(r-l+1)/2,满足继续往左右子树查询.没有满足条件的就返回0.由于当前的节点的sum值一定是大于(r-l+1)/2的,如果l==r直接返回l即可. 1 #include<iost

[Poi2014]Couriers

bzoj3524: [Poi2014]Couriers 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