BZOJ2223[Coci 2009]PATULJCI——主席树
题目描述

输入
输出
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
样例输出
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——主席树的更多相关文章
- [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 ...
- 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_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 ...
- 2018.09.30 bzoj2223: [Coci 2009]PATULJCI(主席树)
传送门 主席树经典题目. 直接利用主席树差分的思想判断区间中数的个数是否合法然后决定左走右走就行了. 实际上跟bzoj3524是同一道题. 代码: #include<bits/stdc++.h& ...
- 【BZOJ2223&&3524】PATULJCI [主席树]
PATULJCI Time Limit: 10 Sec Memory Limit: 259 MB[Submit][Status][Discuss] Description Input 第一行两个整数 ...
- BZOJ2223 [Coci 2009]PATULJCI
求区间内个数大于rank的一个数 主席树求一下就好啦! /************************************************************** Problem: ...
- 【莫队算法】【权值分块】bzoj2223 [Coci 2009]PATULJCI
不带修改主席树裸题<=>莫队+权值分块裸题. 复杂度O(m*sqrt(n)). P.S.题目描述坑爹,第二个数是权值的范围. #include<cstdio> #include ...
随机推荐
- java 迭代器遍历List Set Map
Iterator接口: 所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现Iterator接口的对象 Iterator对象称作为迭代器,用以方便的对容器内元素的遍历 ...
- TerraGate SFS 4.5 版本 发布矢量数据使用的Cache数据如何再返回成shapefile文件
TerraGate SFS 4.5 版本 发布矢量数据使用的Cache数据如何再返回成shapefile文件? 两年前帮一个朋友解决过这个问题: 如果原来用4.5版本的时候,在网络环境下,为了提升调用 ...
- [01] Why Spring
1.何为框架 简单来说,框架就是制定了一套规范或者规则(思想),让程序员在该规范或规则下进行工作.如上图修楼房,在有框架的情况下,修建工作才高效且有条不紊.楼层钢架规定了大楼的结构,工人按架构添砖放瓦 ...
- Linux上vim编辑器缩进的设置(方便如书写python代码)
因为刚刚安装了Ubuntu虚拟机,知乎大神推荐用VIM编辑器,然后发现在Python缩进上不方便,查了点资料,整理出来,以便自己以后忘了还可以再看看. 第一步: 打开终端,在终端上输入 vim ~/. ...
- VB6 变量定义作用域的一个奇特形式
C#或JAVA 下面的i定义是只会限定在if 条件块里的: if (1 == 2) { int i = 000; } else { i = 111;// 错误,未定义. } i = 222;//错误 ...
- NOIP2018题解
Preface 联赛结束后趁着自己还没有一下子把题目忘光,所以趁机改一下题目. 没有和游记一起写主要是怕篇幅太长不美观. 因此这里我们直接讲题目,关于NOIP2018的一些心得和有趣的事详见:NOIP ...
- C# 调用微信接口上传素材和发送图文消息
using Common;using Newtonsoft.Json.Linq;using System;using System.IO;using System.Net;using System.T ...
- (6)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- AOP框架
AOP 框架基础 要求懂的知识:AOP.Filter.反射(Attribute). 如果直接使用 Polly,那么就会造成业务代码中混杂大量的业务无关代码.我们使用 AOP (如果不了解 AOP,请自 ...
- Docker 小记 — Docker Engine
前言 用了 Docker 方才觉得生产环境终于有了他该有的样子,就像集装箱普及之后大型货轮的价值才逐渐体现出来,Docker 详细说明可查阅"官方文档".本篇为 Docker En ...
- 我的AutoHotkey脚本
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. ; #Warn ; En ...