【BZOJ2223&&3524】PATULJCI [主席树]
PATULJCI
Time Limit: 10 Sec Memory Limit: 259 MB
[Submit][Status][Discuss]
Description

Input
第一行两个整数n,INF,表示序列长度和ai的上限;
第二行有n个数,表示ai;
然后有一个整数m,表示询问个数;
接下来每行两个l,r,表示询问区间[l,r]中的答案。
Output
输出m行,表示对于每个询问的答案。如果有这个数,则输出“yes”,然后输出数的值;否则输出“no”。
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
1<=n<=300000 , 1<=m<=10000 , 1<=ai<=10000。
Solution
显然是一个主席树,我们建立一棵主席树然后查询是否存在个数>(l+r-1)/2的即可。
Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std; const int ONE=; int n,INF,m;
int x,y,cnt;
int res_value,res_num; struct power
{
int root;
int value;
int left,right;
}Node[ONE*]; int get()
{
int res=,Q=;char c;
while( (c=getchar())< || c> )
if(c=='-')Q=-;
res=c-;
while( (c=getchar())>= && c<= )
res=res*+c-;
return res*Q;
} void Update(int &x,int y,int L,int R,int Q)
{
x = ++cnt;
Node[x].left = Node[y].left;
Node[x].right = Node[y].right;
Node[x].value = Node[y].value + ;
if(L == R) return; int M = (L+R)>>;
if(Q <= M)
Update(Node[x].left,Node[y].left, L,M, Q);
else
Update(Node[x].right,Node[y].right, M+,R, Q);
} void Query(int x,int y,int L,int R,int Kth)
{
if(L == R)
{
res_value = L;
res_num = Node[y].value - Node[x].value;
return;
} int M = (L+R)>>;
int record = Node[Node[y].left].value - Node[Node[x].left].value; if(Kth < record)
Query(Node[x].left,Node[y].left, L,M, Kth);
else
Query(Node[x].right,Node[y].right, M+,R, Kth);
} int main()
{
n=get(); INF=get();
for(int i=;i<=n;i++)
{
x=get();
Update(Node[i].root,Node[i-].root, ,INF, x);
} m=get();
for(int i=;i<=m;i++)
{
x=get(); y=get(); res_value = , res_num = ;
int M = (y-x+)/;
Query(Node[x-].root,Node[y].root, ,INF, M); if(res_num > M)
printf("yes %d",res_value);
else
printf("no");
printf("\n");
}
}
【BZOJ2223&&3524】PATULJCI [主席树]的更多相关文章
- [bzoj3524==bzoj2223][Poi2014]Couriers/[Coci 2009]PATULJCI——主席树+权值线段树
题目大意 给定一个大小为n,每个数的大小均在[1,c]之间的数列,你需要回答m个询问,其中第i个询问形如\((l_i, r_i)\),你需要回答是否存在一个数使得它在区间\([l_i,r_i]\)中出 ...
- BZOJ2223[Coci 2009]PATULJCI——主席树
题目描述 输入 先输入一个数n,然后一个数表示这n个数中最大的是多少,接下来一行n个数.然后一个数m,最后m行询问每次两个数l,r. 输出 no或者yes+这个数 样例输入 10 3 1 2 1 2 ...
- 【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 ...
- BZOJ 3524 Couriers | 主席树
BZOJ 3524 Couriers 题意 求一个区间内出现超过区间长度的一半的数,如果没有则输出0. 题解 我可能太菜了吧--这道题愣是没想出来-- 维护权值主席树,记录每个数都出现过多少次: 查询 ...
- BZOJ 2223 [Coci 2009]PATULJCI | 主席树练习 (好像是个权限题啊)
题目: 给个序列,问[l,r]区间内是否存在x>(r-l+1)>>1 题解: 好像大家都觉得这个题比较简单,没人写题解啊 先说BZOJ样例的格式应该是,第二个数是序列中数的范围(就是 ...
- BZOJ 2223: [Coci 2009]PATULJCI 主席树
Code: #include<bits/stdc++.h> #define maxn 300001 #define mid ((l+r)>>1) using namespace ...
- 主席树||可持久化线段树||BZOJ 3524: [Poi2014]Couriers||BZOJ 2223: [Coci 2009]PATULJCI||Luogu P3567 [POI2014]KUR-Couriers
题目:[POI2014]KUR-Couriers 题解: 要求出现次数大于(R-L+1)/2的数,这样的数最多只有一个.我们对序列做主席树,每个节点记录出现的次数和(sum).(这里忽略版本差值问题) ...
- 2018.09.30 bzoj2223: [Coci 2009]PATULJCI(主席树)
传送门 主席树经典题目. 直接利用主席树差分的思想判断区间中数的个数是否合法然后决定左走右走就行了. 实际上跟bzoj3524是同一道题. 代码: #include<bits/stdc++.h& ...
- BZOJ 3524: [Poi2014]Couriers [主席树]
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1892 Solved: 683[Submit][St ...
随机推荐
- Maven初步接触
最近随着搜资料,网上这样的字眼越来越多,我了解到这是构建项目的一种方式,于是准备简单看一下 首先粘几篇文章,作为学习的初步资料 Maven入门 http://blog.csdn.net/prstaxy ...
- Nullable可空类型
一个Nullable类型就是基本类型加上一个"是否为null指示器"的合成类型.对于一个类型,如果既可以给他分配一个值,也可以给它分配null引用,我们就说这个类型是可空的. 可空 ...
- 【Linux运维】Centos7上借助ansible搭建LVS+Keepalived
安装ansible 安装ansible: [root@localhost ~]# /etc/hosts 192.168.19.129 web129.yanglt.com web129 192.168. ...
- resetroot_169route_python2(用于ubuntu12.04和14.04,centos系列)
#!/usr/bin/python import os import json import subprocess from cloudinit.sources.DataSourceConfigDri ...
- AcCoder Contest-115 D - Christmas
D - Christmas Time limit : 2sec / Memory limit : 1024MB Score : 400 points Problem Statement In some ...
- python基础训练营02
任务二 时长:2天 1. 列表 a. 标志 b. 基本操作(创建,append( ),pop( ) ,del( ), 拷贝) c. 列表相关方法 2. 元组 a. 标志 b. 基本操作(创建及不可变性 ...
- 主外键多表查询demo
https://www.cnblogs.com/DragonFire/p/6949767.html mySQL练习-主外键多表查询 MySQL练习-主外键多表查询 练习: 1.建立表关系: 请创建如下 ...
- asp.net页面中的Console.WriteLine结果如何查看
其实用Console.WriteLine("xxxxx"),在asp.net Web程序,在输出窗口是不会输出结果的,应该用Debug.WriteLine("xxxxx& ...
- OpenCV平滑处理示例代码
#include<cv.h> #include<highgui.h> int main(int argc, char** argv) { IplImage* img = cvL ...
- Struts2-part1
作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) 1. Struts2应用的开发步骤: ① 在web.xml中配置核心的Filter来拦截用户的请求. <w ...