题目描述

输入

 先输入一个数n,然后一个数表示这n个数中最大的是多少,接下来一行n个数。然后一个数m,最后m行询问每次两个数l,r。

输出

no或者yes+这个数

样例输入

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 2
no
yes 3

提示

Notice:输入第二个整数是序列中权值的范围Lim,即1<=ai(1<=i<=n)<=Lim。
1<=Lim<=10000

  原题样例比较懵逼,这里解释了一下输入写在上面。

  这题算是主席树模板题了,维护n个时刻的主席树,每一时刻开一棵权值线段树维护区间数的个数,然后在主席树上二分查找。

#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define mid (L+R)/2
using namespace std;
int n,m;
int lim;
int x,y;
int cnt;
int ans;
int l[10000010];
int r[10000010];
int sum[10000010];
int root[10000010];
int updata(int pre,int L,int R,int k)
{
int rt=++cnt;
l[rt]=l[pre];
r[rt]=r[pre];
sum[rt]=sum[pre]+1;
if(L==R)
{
return rt;
}
if(k<=mid)
{
l[rt]=updata(l[pre],L,mid,k);
}
else
{
r[rt]=updata(r[pre],mid+1,R,k);
}
return rt;
}
int query(int rr,int ll,int L,int R,int k)
{
if(L==R)
{
return L;
}
int x=sum[l[rr]]-sum[l[ll]];
int y=sum[r[rr]]-sum[r[ll]];
if(x>k)
{
return query(l[rr],l[ll],L,mid,k);
}
else if(y>k)
{
return query(r[rr],r[ll],mid+1,R,k);
}
else
{
return 0;
}
}
int main()
{
scanf("%d%d",&n,&lim);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
root[i]=updata(root[i-1],1,lim,x);
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
int ans=query(root[y],root[x-1],1,lim,(y-x+1)/2);
if(ans==0)
{ printf("no\n");
}
else
{ printf("yes %d\n",ans);
}
}
}

BZOJ2223[Coci 2009]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 主席树

    题目描述 样例输入 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 ...

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

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

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

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

  5. BZOJ_2223_[Coci 2009]PATULJCI_主席树

    BZOJ_2223_[Coci 2009]PATULJCI_主席树 Description 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. 2018.09.30 bzoj2223: [Coci 2009]PATULJCI(主席树)

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

  7. 【BZOJ2223&&3524】PATULJCI [主席树]

    PATULJCI Time Limit: 10 Sec  Memory Limit: 259 MB[Submit][Status][Discuss] Description Input 第一行两个整数 ...

  8. BZOJ2223 [Coci 2009]PATULJCI

    求区间内个数大于rank的一个数 主席树求一下就好啦! /************************************************************** Problem: ...

  9. 【莫队算法】【权值分块】bzoj2223 [Coci 2009]PATULJCI

    不带修改主席树裸题<=>莫队+权值分块裸题. 复杂度O(m*sqrt(n)). P.S.题目描述坑爹,第二个数是权值的范围. #include<cstdio> #include ...

随机推荐

  1. 【Codeforces Round 1132】Educational Round 61

    Codeforces Round 1132 这场比赛做了\(A\).\(B\).\(C\).\(F\)四题,排名\(89\). \(A\)题\(wa\)了一次,少考虑了一种情况 \(D\)题最后做出来 ...

  2. ubuntu和windows系统双系统的开机选项界面有很多无关选项

    我的电脑是双系统,在进入系统选项的时候有很多无关的选项, 例如: 解决的方法是在终端输入 sudo gedit /boot/grub/grub.cfg 把文件多余的开机选项删除例如: 保存就可以,开机 ...

  3. Android java.lang.RuntimeException: Unable to instantiate activity ComponentInfo 特殊异常

    本来是不想写的,因为这个异常太常见了,而且也容易处理.但是还是决定记录一下,因为之前遇到过,没留心,今天又遇到了,苦逼了,想了好大一会儿才想起来. 通常容易找的就不写了,今天写个特殊的. 现象:当你在 ...

  4. Android Studio 导入工程

    最简单的方式 等待加载完就好了 第二种方式 在导入别人的android studio项目(假设为项目A)时,会遇到gradle不一致的情况,以下简短介绍解决方法: 1. 打开要导入的项目的目录,删除下 ...

  5. [02] URL和HttpURLConnection类

    1.URL的概念 统一资源定位符URL(Uniform Resource Locator)是www客户机访问Internet时用来标识资源的名字和地址. URL的基本格式是: <METHOD&g ...

  6. Spring Cloud 入门教程(九): 路由网关zuul

    在微服务架构中,需要几个关键的组件,服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个组件可以组建一个简单的微服务架构.客户端的请求首先经过负载均衡(zuul.Ngnix),再 ...

  7. 如何用区块链技术解决信任问题?Fabric 架构深度解读

    阿里妹导读:区块链技术,随着比特币的兴起而为大家所知.但是具体到技术本身,大家相对熟悉的几个词可能是“数据不可篡改”.“公开链”.“分布式数据”.“共识机制”等. 这篇文章将抛砖引玉,通过深度解读Hy ...

  8. SQLAlchemy模块的使用教程

    数据库表是一个二维表,包含多行多列.把一个表的内容用Python的数据结构表示出来的话,可以用一个list表示多行,list的每一个元素是tuple,表示一行记录,比如,包含id和name的user表 ...

  9. 【亲测有效】运行docker ps 出现Got permission denied问题的解决方案

    问题描述 今天在运行 docker ps 命令的时候出现如下问题: Got permission denied while trying to connect to the Docker daemon ...

  10. TRIO-basic指令--FLEXLINK

    Type: Axis Command Syntax: FLEXLINK(base_dist, excite_dist, link_dist, base_in, base_out, excite_acc ...