给一个长度为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 仍然是基础的主席树;
不离散化也可以;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#pragma GCC optimize(2)
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 700005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++) inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} int n, m, q;
int cnt = 0;
int a[maxn], b[maxn], T[maxn];
int sum[maxn << 5], lson[maxn << 5], rson[maxn << 5]; int build(int l, int r) {
int rt = ++cnt;
sum[rt] = 0;
if (l < r) {
int mid = (l + r) >> 1;
lson[rt] = build(l, mid); rson[rt] = build(mid + 1, r);
}
return rt;
} int upd(int pre, int l, int r, int x) {
int rt = ++cnt;
lson[rt] = lson[pre]; rson[rt] = rson[pre];
sum[rt] = sum[pre] + 1;
if (l < r) {
int mid = (l + r) >> 1;
if (x <= mid)lson[rt] = upd(lson[pre], l, mid, x);
else rson[rt] = upd(rson[pre], mid + 1, r, x);
}
return rt;
} int query(int u, int v, int l, int r, int k) {
if (l >= r)return l;
int x = sum[lson[v]] - sum[lson[u]];
int y = sum[rson[v]] - sum[rson[u]];
int mid = (l + r) >> 1;
if (x > k)return query(lson[u], lson[v], l, mid, k);
else if (y > k) return query(rson[u], rson[v], mid + 1, r, k);
else return 0;
} int main(){
//ios::sync_with_stdio(0);
rdint(n); rdint(q);
for (int i = 1; i <= n; i++)rdint(a[i]), b[i] = a[i];
sort(b + 1, b + 1 + n);
m = unique(b + 1, b + 1 + n) - b - 1;
T[0] = build(1, m);
for (int i = 1; i <= n; i++) {
int t = lower_bound(b + 1, b + 1 + m, a[i]) - b;
T[i] = upd(T[i - 1], 1, m, t);
}
while (q--) {
int x, y, z; rdint(x); rdint(y); z = (y - x + 1) >> 1;
int t = query(T[x - 1], T[y], 1, m, z);
if (t == 0)printf("0\n");
else printf("%d\n", b[t]);
}
return 0;
}

[POI2014]KUR-Couriers BZOJ3524 主席树的更多相关文章

  1. bzoj3524: [Poi2014]Couriers(主席树)

    主席树(可持久化权值线段树)初探... 修改一个点只对树上logn个点有影响,所以新建logn个点就行了,总共新建mlogn个点. 查询一个区间[l,r],相当于将数一个一个加进树,询问第l到第r次操 ...

  2. BZOJ3524 [Poi2014]Couriers 【主席树】

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

  3. 【BZOJ3524】Couriers(主席树)

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

  4. 【BZOJ】3524 [POI2014] Couriers(主席树)

    题目 传送门:QWQ 传送到洛谷QWQ 分析 把求区间第k大的改一改就ok了. 代码 #include <bits/stdc++.h> using namespace std; ; ], ...

  5. 【BZOJ3524/2223】[Poi2014]Couriers 主席树

    [BZOJ3524][Poi2014]Couriers Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大 ...

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

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2436  Solved: 960[Submit][St ...

  7. [bzoj3524==bzoj2223][Poi2014]Couriers/[Coci 2009]PATULJCI——主席树+权值线段树

    题目大意 给定一个大小为n,每个数的大小均在[1,c]之间的数列,你需要回答m个询问,其中第i个询问形如\((l_i, r_i)\),你需要回答是否存在一个数使得它在区间\([l_i,r_i]\)中出 ...

  8. BZOJ3524[Poi2014]Couriers——主席树

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

  9. BZOJ3524: [Poi2014]Couriers(主席树)

    题意 题目链接 Sol 严格众数只会出现一次,那么建出主席树,维护子树siz,直接在树上二分即可 #include<bits/stdc++.h> #define LL long long ...

随机推荐

  1. 2016.6.18主窗体、子窗体InitializeComponent()事件、Load事件发生顺序以及SeleChanged事件的发生

    主窗体,子窗体的InitializeComponent(构造函数).Load事件执行顺序 1.主窗体定义事件 new 主窗体() 构造函数进入主窗体InitializeComponent函数,该函数中 ...

  2. Git中远程仓库的使用

    1.查看当前的远程库 要查看当前配置有哪些远程仓库,可以用 git remote 命令,它会列出每个远程库的简短名字.在克隆完某个项目后,至少可以看到一个名为 origin 的远程库,Git 默认使用 ...

  3. 工作的时候用到spring返回xml view查到此文章亲测可用

    spring mvc就是好,特别是rest风格的话,一个 org.springframework.web.servlet.view.ContentNegotiatingViewResolver就可以根 ...

  4. [Python Study Notes]双层柱状图绘制

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  5. eclipse 中使用 GreenUML 和 AmasterasUML 自动生成类图

    Green UML和AmaterasUML 两种 一.安装方法: 1.都是先安装GEF 通过eclipse-> install new software安装GEF的网址: http://down ...

  6. solr的copyFeild用法(改变各个feild的权重,修改打分结果)-注意!

    copyField的dest字段all本身有分析器处理:假设为mmseg4j name,title,description三个字段都复制到all字段上:其中title和description都是mms ...

  7. SVN资源库报错:Could not create the view: org.tigris.subversion.subclipse.ui.repository.RepositoriesView

    解决方法: 关闭正在运行的myeclipse,然后打开myeclipse安装路径(我的安装在c盘): c:\ProgramFiles\MyEclipse\MyEclipse Professional ...

  8. Java进程与多线程+线程中的join、yield、wait等方法+synchronized同步锁使用

    首先了解什么是多线程与进程 进程:是一个执行过程,动态的概念 --->会分配内存线程:是进程的一个单元,线程是系统最小的执行单元 详解: http://blog.csdn.net/luoweif ...

  9. js教程--从入门到精通 第一篇 js的前世今生以及js中基本数据类型和引入方式

    1.Javascript前世今生   1.1.什么是Javascript       Javascript运行于Javascript [解释器/引擎]中的解释性脚本语言      Javascript ...

  10. Android的性能优化

    ArrayList和Vector ArrayList和Vector都是内部以数组实现的List,它们两唯一的区别就是对多线程的支持,ArrayList是线程不安全的,而Vector内部对大多数方法都做 ...