BZOJ2223:[Coci2009]PATULJCI——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2223
Description

Sample Input
10 3 1 2 1 2 1 2 3 2 3 3 8 1 2 1 3 1 4 1 5 2 5 2 6 6 9 7 10
Sample Output
yes 1
no
yes 1
no
yes 2
no
yes 3
HINT
Notice:输入第二个整数是序列中权值的范围Lim,即1<=ai(1<=i<=n)<=Lim。
1<=Lim<=10000
————————————————————————————
求区间众数(或者可以说求区间内重复次数大于k的数?)
显然主席树可以胜任,我们只需要在query操作修改一下,变成查找区间使得该区间的数的个数必须大于k,那么就很简单了。
这题的范围比较小,所以不需要离散化。
#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int N=;
inline int read(){
int X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct tree{
int l;
int r;
int sum;
}tr[N*];
int rt[N],n,m,lim,q,pool;
inline void insert(int &y,int &x,int l,int r,int p){
tr[x=++pool]=tr[y];
tr[x].sum++;
if(l==r)return;
int mid=(l+r)>>;
if(p<=mid)insert(tr[y].l,tr[x].l,l,mid,p);
else insert(tr[y].r,tr[x].r,mid+,r,p);
return;
}
inline int query(int nl,int nr,int l,int r,int k){
if(l==r)return l;
int d1=tr[tr[nr].l].sum-tr[tr[nl].l].sum;
int d2=tr[tr[nr].r].sum-tr[tr[nl].r].sum;
int mid=(l+r)>>;
if(d1>k)return query(tr[nl].l,tr[nr].l,l,mid,k);
if(d2>k)return query(tr[nl].r,tr[nr].r,mid+,r,k);
return ;
}
int main(){
n=read();
lim=read();
for(int i=;i<=n;i++)insert(rt[i-],rt[i],,lim,read());
q=read();
for(int i=;i<=q;i++){
int l=read(),r=read();
int ans=query(rt[l-],rt[r],,lim,(r-l+)>>);
if(ans)printf("yes %d\n",ans);
else puts("no");
}
return ;
}
BZOJ2223:[Coci2009]PATULJCI——题解的更多相关文章
- 【BZOJ-2223】PATULJCI 可持久化线段树
2223: [Coci 2009]PATULJCI Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 728 Solved: 292[Submit][S ...
- BZOJ3524 & LOJ2432:[POI2014]代理商Couriers——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3524 https://loj.ac/problem/2432 给一个长度为n的序列a.1≤a[i] ...
- [bzoj3524==bzoj2223][Poi2014]Couriers/[Coci 2009]PATULJCI——主席树+权值线段树
题目大意 给定一个大小为n,每个数的大小均在[1,c]之间的数列,你需要回答m个询问,其中第i个询问形如\((l_i, r_i)\),你需要回答是否存在一个数使得它在区间\([l_i,r_i]\)中出 ...
- 【bzoj2223】[Coci 2009]PATULJCI 主席树
题目描述 样例输入 10 3 1 2 1 2 1 2 3 2 3 3 8 1 2 1 3 1 4 1 5 2 5 2 6 6 9 7 10 样例输出 no yes 1 no yes 1 no yes ...
- 【BZOJ2223&&3524】PATULJCI [主席树]
PATULJCI Time Limit: 10 Sec Memory Limit: 259 MB[Submit][Status][Discuss] Description Input 第一行两个整数 ...
- 【BZOJ2223/3524】[Coci 2009]PATULJCI
Description Input Output 10 3 1 2 1 2 1 2 3 2 3 3 8 1 2 1 3 1 4 1 5 2 5 2 6 6 9 7 10 Sample Input ...
- BZOJ2223[Coci 2009]PATULJCI——主席树
题目描述 输入 先输入一个数n,然后一个数表示这n个数中最大的是多少,接下来一行n个数.然后一个数m,最后m行询问每次两个数l,r. 输出 no或者yes+这个数 样例输入 10 3 1 2 1 2 ...
- 2018.09.30 bzoj2223: [Coci 2009]PATULJCI(主席树)
传送门 主席树经典题目. 直接利用主席树差分的思想判断区间中数的个数是否合法然后决定左走右走就行了. 实际上跟bzoj3524是同一道题. 代码: #include<bits/stdc++.h& ...
- BZOJ2223 [Coci 2009]PATULJCI
求区间内个数大于rank的一个数 主席树求一下就好啦! /************************************************************** Problem: ...
随机推荐
- 初识java atomic
2018-8-19 昨天看到java.util.concurrent.atomic相关的文章,之前有过留意但并未去了解,正好有空学习一下.本人理解atomic包是concurrent子包,当是为并发所 ...
- [JSON].remove( keyPath )
语法:[JSON].remove( keyPath ) 返回:无 说明:移除指定路径的键 示例: Set jsonObj = toJson("{div:{'#text-1': 'is tex ...
- JSON.parse() 与 eval()
JSON(JavaScript Object Notation)是一种轻量级的数据格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是Javascript原生格式,这意味着在ja ...
- leetcode-最大子序和(动态规划讲解)
最大子序和(动态规划讲解) 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输 ...
- stm32之SPI通信协议
SPI (Serial Peripheral interface),顾名思义就是串行外围设备接口.SPI是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚,同时为P ...
- JS原型与面向对象总结
ECMAScript有两种开发模式:1.函数式(过程化),2.面向对象(OOP).面向对象的语言有一个标志,那就是类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.但 是,ECMAScrip ...
- Android屏幕适配解析 - 详解像素,设备独立像素,归一化密度,精确密度及各种资源对应的尺寸密度分辨率适配问题
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19698511 . 最近遇到了一系列的屏幕适配问题, 以及 ...
- block知识总结
一.block在内存中存在的形式 1.当把block句法写在函数或者方法外面时,系统会在静态数据区分配一块内存区域给block对象.这片区域在程序执行期会一直存在. 2.当block句法写在函数或者方 ...
- "亿家App"问卷调查分析结果及心得体会
一.问卷问题设计 调查背景:随着现代社会互联网的发展,基于家庭产生的服务项目也越来越多.为增加家庭之间的交流和互助,增加家庭内部.家庭与家庭之间的沟通互助,并利用互联网便捷交流的优势,使家庭在享受服务 ...
- 【Docker 命令】- pull命令
docker pull : 从镜像仓库中拉取或者更新指定镜像 语法 docker pull [OPTIONS] NAME[:TAG|@DIGEST] OPTIONS说明: -a :拉取所有 tagge ...