【BZOJ3524】Couriers
题面
Description
给一个长度为\(n\)的序列\(a\)。\(1\le a[i]\le 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\le 500000\)
分析
主席树维护权值线段树,直接在树上二分。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<climits>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<ctime>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
inline char nc(){
/*
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
*/return getchar();
}
inline void read(int &x){
char c=nc();int b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline void read(LL &x){
char c=nc();LL b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline void read(char &x){
for (x=nc();!(x=='('||x==')');x=nc());
}
inline int read(char *s)
{
char c=nc();int len=1;
for(;!(c=='('||c==')');c=nc()) if (c==EOF) return 0;
for(;(c=='('||c==')');s[len++]=c,c=nc());
s[len++]='\0';
return len-2;
}
int wt,ss[19];
inline void print(int x){
if (x<0) x=-x,putchar('-');
if (!x) putchar(48); else {
for (wt=0;x;ss[++wt]=x%10,x/=10);
for (;wt;putchar(ss[wt]+48),wt--);}
}
inline void print(LL x){
if (x<0) x=-x,putchar('-');
if (!x) putchar(48); else {for (wt=0;x;ss[++wt]=x%10,x/=10);for (;wt;putchar(ss[wt]+48),wt--);}
}
int T,n,m,s,k,h[500010],b[500010],d[500010];
struct ST
{
int lc,rc,sum;
}a[10000010];
void build(int l,int r,int x)
{
s++;x=s;
if (l==r) {a[x].lc=a[x].rc=0;return ;}
int mid=(l+r)>>1;
a[x].lc=s+1;build(l,mid,x);
a[x].rc=s+1;build(mid+1,r,x);
}
void change(int x,int y,int z,int xx,int l,int r) //新结点编号,,,对应结点编号,[l,r]
{
if (l==r) {a[x].sum=a[xx].sum+z;return ;}
int mid=(l+r)>>1;
if (y<=mid)
{
a[x].rc=a[xx].rc;
s++;a[x].lc=s;
change(s,y,z,a[xx].lc,l,mid);
}
else
{
a[x].lc=a[xx].lc;
s++;a[x].rc=s;
change(s,y,z,a[xx].rc,mid+1,r);
}
a[x].sum=a[a[x].lc].sum+a[a[x].rc].sum;
}
int query(int x,int y,int z,int l,int r)
{
//printf("%d %d %d %d\n",l,r,z,a[y].sum-a[x].sum);
if (l==r)
{
if (a[y].sum-a[x].sum>z) return b[l];
else return 0;
}
if (a[a[y].lc].sum-a[a[x].lc].sum>z) return query(a[x].lc,a[y].lc,z,l,(l+r)>>1);
else if (a[a[y].rc].sum-a[a[x].rc].sum>z) return query(a[x].rc,a[y].rc,z,((l+r)>>1)+1,r);
else return 0;
}
int main()
{
read(n);read(m);
memset(a,0,sizeof(a));
for (int i=1;i<=n;i++)
read(d[i]),b[i]=i;
s=0;
build(1,n,1);
h[0]=1;
for (int i=1;i<=n;i++)
{
s++;h[i]=s;
change(s,d[i],1,h[i-1],1,n);
}
int x,y,z;
while(m--)
{
read(x);read(y);
print(query(h[x-1],h[y],(y-x+1)/2,1,n));putchar('\n');
}
return 0;
}
【BZOJ3524】Couriers的更多相关文章
- 【BZOJ3524】Couriers(主席树)
题意:给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. n,m≤5000 ...
- 【BZOJ3524】 [Poi2014]Couriers
Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. ...
- 【bzoj3524】【Poi2014】【Couriers】可持久化线段树(主席树)水题
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62485671 向大(hei)佬(e)势力学(di ...
- 【bzoj3524】[Poi2014]Couriers 主席树
题目描述 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入 第一行 ...
- 【BZOJ 3524】【Poi2014】Couriers 可持久化线段树
为什么这个主席树叫可持久化线段树,我不知道,具体得问达神.我无限T,然后DaD3zZ一针见血地指出了我的N*50爆内存导致无限编译超时O)ZO)ZO)Z真是太神啦.以图为鉴: 达神题解传送门:http ...
- 【BZOJ】【3524】【POI2014】Couriers
可持久化线段树 裸可持久化线段树,把区间第K大的rank改成num即可……(往儿子走的时候不减少) 苦逼的我……MLE了一次(N*30),RE了一次(N*10)……数组大小不会开…… 最后开成N*20 ...
- 【BZOJ3524/2223】[Poi2014]Couriers 主席树
[BZOJ3524][Poi2014]Couriers Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大 ...
- 【BZOJ】3524: [Poi2014]Couriers
[算法]主席树 [题解]例题,记录和,数字出现超过一半就递归查找. 主席树见[算法]数据结构 #include<cstdio> #include<algorithm> #inc ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
随机推荐
- ClassNotFoundException: http.nio.NHttpClientEventHandle
解决方案是打开maven仓库中的jar包看看报错的类所对应版本的类存在不存在,若不存在就换个版本的jar包
- (转)Unreal Shader模块(四): 着色器编译
本文为(转):Unreal 调试着色器编译过程 调试着色器编译过程 Rolando Caloca 在 April 19, 2016 | 学习编程 Share on Facebook Shar ...
- 用archlinux作为日常开发机的感受
机器配置 CPU: Intel Core i5-6200U RAM: 8G Resolution: 1920x1080 我在arch下常用的软件 图形桌面环境 i3wm wifi无线管理 Networ ...
- URAL 1934 spfa算法
D - Black Spot Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- 用IE滤镜实现的一些特效
CSS3是当下非常火的一个话题,很多浏览器都已经开始支持这一特性,然后IE这个拥有庞大用户群体的平台,却无法提供这样的支持,即便是IE9发布,也无法改变这一事实,然而,幸运的是,IE并非在这方面毫无作 ...
- Java面试题之hashmap中用什么hash算法解决碰撞的?
查了一下源码(jdk8),记录一下吧,能记住就记一下吧! static final int hash(Object key) { int h; return (key == null) ? 0 : ( ...
- vue2.0 v-tap简洁(漏)版 (只解决300ms问题)
Vue.directive('tap',{ bind:function(el,binding){ var startTx, startTy, endTx, endTy, startTime, endT ...
- WKWebview 和 WebViewJavascriptBridge
WKWebview 和 WebViewJavascriptBridge https://www.cnblogs.com/L-vincen/p/6681435.html 链接在这里,有很多不错的文章,大 ...
- SpringBoot Redis序列化配置
Redis配置 #Redis spring.redis.host= spring.redis.port=6379 spring.redis.database=0 # Redis服务器连接密码(默认为空 ...
- 洛谷 P2916 [USACO08NOV]为母牛欢呼Cheering up the C…
题目描述 Farmer John has grown so lazy that he no longer wants to continue maintaining the cow paths tha ...