【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 ...
随机推荐
- APK反编译后添加日志
一.反编译 参考前一篇文章 二.添加寄存器(locals) 因为要添加日志,我们一般需要用一个变量来存储TAG,所以需要增加一个寄存器 如: # virtual methods .method pub ...
- 孤荷凌寒自学python第七十五天开始写Python的第一个爬虫5
孤荷凌寒自学python第七十五天开始写Python的第一个爬虫5 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...
- 1066 Root of AVL Tree (25 分)(平衡二叉树)
就是AVL的模板题了 注意细节 #include<bits/stdc++.h> using namespace std; typedef struct node; typedef node ...
- [GraphSAGE] docker安装与程序运行
安装Docker与程序运行 1. requirements.txt Problem: Downloading https://files.pythonhosted.org/packages/69/cb ...
- MySQL训练营02
一.表操作: 1.MySQL表的数据类型: MySQL的数据类型分为3种: 数值 时间/日期 字符/字符串 (1)数值类型: 包括:TinyInt.SmallInt.MediumInt.Int.Big ...
- 今日头条 2018 AI Camp 6 月 2 日在线笔试编程题第二道——两数差的和
题目 给 n 个实数 a_1, a_2 ... a_n, 要求计算这 n 个数两两之间差的绝对值下取整后的和是多少. 输入描述 第一行为一个正整数 n 和一个整数 m.接下来 n 行,第 i 行代表一 ...
- ACM做题随做随思
程序停止运行:数组开太大: 输入一串单词,可以“string s; while(cin>>s){//代码块}”,因为cin>>s遇到空格会停止: map<key,valu ...
- Java开发环境配置时的dt.jar与tools.jar是什么(转载)
你了解dt.jar吗 很多人在初学Java的时候,都要配置环境变量.在配置CLASSPATH的时候,都会加上一个当前目录.,还有两个jar:dt.jar和tools.jar.其实好多人都不了解这两个j ...
- 微信PC端授权页面提示授权入口所在域名为空
做第三方微信平台的时候做授权页面,用window.open方法从第三方平台页面打开新的授权标签页. 在IE浏览器上出问题,提示如下: 在chrome和firefox浏览器上正常. 搜了一下,发现微信是 ...
- [剑指Offer] 25.复杂链表的复制
/* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : ...