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

  no
  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 [主席树]的更多相关文章

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

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

  2. BZOJ2223[Coci 2009]PATULJCI——主席树

    题目描述 输入  先输入一个数n,然后一个数表示这n个数中最大的是多少,接下来一行n个数.然后一个数m,最后m行询问每次两个数l,r. 输出 no或者yes+这个数 样例输入 10 3 1 2 1 2 ...

  3. 【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 ...

  4. BZOJ 3524 Couriers | 主席树

    BZOJ 3524 Couriers 题意 求一个区间内出现超过区间长度的一半的数,如果没有则输出0. 题解 我可能太菜了吧--这道题愣是没想出来-- 维护权值主席树,记录每个数都出现过多少次: 查询 ...

  5. BZOJ 2223 [Coci 2009]PATULJCI | 主席树练习 (好像是个权限题啊)

    题目: 给个序列,问[l,r]区间内是否存在x>(r-l+1)>>1 题解: 好像大家都觉得这个题比较简单,没人写题解啊 先说BZOJ样例的格式应该是,第二个数是序列中数的范围(就是 ...

  6. BZOJ 2223: [Coci 2009]PATULJCI 主席树

    Code: #include<bits/stdc++.h> #define maxn 300001 #define mid ((l+r)>>1) using namespace ...

  7. 主席树||可持久化线段树||BZOJ 3524: [Poi2014]Couriers||BZOJ 2223: [Coci 2009]PATULJCI||Luogu P3567 [POI2014]KUR-Couriers

    题目:[POI2014]KUR-Couriers 题解: 要求出现次数大于(R-L+1)/2的数,这样的数最多只有一个.我们对序列做主席树,每个节点记录出现的次数和(sum).(这里忽略版本差值问题) ...

  8. 2018.09.30 bzoj2223: [Coci 2009]PATULJCI(主席树)

    传送门 主席树经典题目. 直接利用主席树差分的思想判断区间中数的个数是否合法然后决定左走右走就行了. 实际上跟bzoj3524是同一道题. 代码: #include<bits/stdc++.h& ...

  9. BZOJ 3524: [Poi2014]Couriers [主席树]

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

随机推荐

  1. python 产生有序整数序列

    其中一种方法 A = np.linspace(0,n,n)

  2. ubuntu 关闭触控板

    第一种: 1 sudo rmmod psmouse    这个是禁用的 2 sudo modprobe psmouse 这个是启用的 这个方法很便捷,但是会将触点和触板都禁用了,一般还是希望保持触点是 ...

  3. 「日常训练」 Soldier and Cards (CFR304D2C)

    题意 (Codeforces 546C) 按照指定的规则打牌,问谁胜或无穷尽. 分析 又是一条模拟,用set+queue(这里手写了)处理即可.注意到两种局势"1 234"和&qu ...

  4. Laravel 5.5 创建全局公共函数

    一.需求 我在使用 Laravel 进行项目逻辑处理的时候要加载一些方法,需要全局调用 这个方法又必须得是一个全局函数,因此需要给 Laravel 创建全局的公共函数 二.实现 1.创建文件 在 ap ...

  5. LeetCode - 20. Valid Parentheses(0ms)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. CentOS7安装Jenkins Master

    一.安装java环境 1.查看服务器版本 cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 升级操作系统 yum update ...

  7. Hangman游戏源代码 --- python实现

    #hangman.py from PythonCard import model,dialog import random def find_letters(letter,a_string): loc ...

  8. Codeforces Round #553 F Sonya and Informatics

    题目 题目大意 给定一个长为 $n$($2 \le n \le 100$)的01串 $S$ .对 $S$ 进行 $k$($1 \le k \le 10^9$)次操作:等概率地选取两个下标 $i, j$ ...

  9. ZOJ 3496 Assignment | 二分+有上下界网络流

    题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3496 大概意思:给你一个网络,有源汇,在保证最大流的情况下求下面两 ...

  10. [Leetcode] Reverse linked list ii 反转链表

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2 ...