BZOJ3022 : [Balkan2012]The Best Teams
将选手和询问按照年龄排序,即可去掉年龄的限制。
将所有选手按水平排序后维护线段树,显然最优解一定是从大到小贪心选择。
线段树上每个节点维护:
$g[0/1]:r+1$不选/选的时候,$l$选不选。
$c[0/1]:r+1$不选/选的时候,中间选了几个。
$s[0/1]:r+1$不选/选的时候,中间选的和。
然后查询的时候在线段树上二分即可。
时间复杂度$O((n+m)\log n)$。
#include<cstdio>
#include<algorithm>
#define N 300010
typedef long long ll;
int n,m,i,j,c[N],pos[N];ll ans[N];
struct P{int x,y,p;}a[N],b[N];
struct T{bool g[2];int c[2];ll s[2];}v[1050000];
inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
inline bool cmp(const P&a,const P&b){return a.x<b.x;}
inline int lower(int x){
int l=1,r=n,mid,t;
while(l<=r)if(c[mid=(l+r)>>1]<=x)l=(t=mid)+1;else r=mid-1;
return t;
}
void build(int x,int a,int b){
if(a==b){pos[a]=x;return;}
int mid=(a+b)>>1;
build(x<<1,a,mid),build(x<<1|1,mid+1,b);
}
inline void change(int x,int y){
x=pos[x];
v[x].g[0]=v[x].c[0]=1,v[x].s[0]=y;
for(x>>=1;x;x>>=1)for(int i=0;i<2;i++){
bool j=v[x<<1|1].g[i];
v[x].g[i]=v[x<<1].g[j];
v[x].c[i]=v[x<<1|1].c[i]+v[x<<1].c[j];
v[x].s[i]=v[x<<1|1].s[i]+v[x<<1].s[j];
}
}
inline ll ask(int k){
int x=1,a=1,b=n,mid,o=0;ll ret=0;
while(k){
if(k>=v[x].c[o]){ret+=v[x].s[o];break;}
if(a==b)break;
mid=(a+b)>>1;
x=x<<1|1;
if(k<=v[x].c[o])a=mid+1;
else{
k-=v[x].c[o];
ret+=v[x].s[o];
o=v[x].g[o];
b=mid;
x--;
}
}
return ret;
}
int main(){
read(n);
for(i=1;i<=n;i++)read(a[i].x),read(a[i].y),c[i]=a[i].y;
read(m);
for(i=1;i<=m;i++)read(b[i].x),read(b[i].y),b[i].p=i;
std::sort(a+1,a+n+1,cmp);
std::sort(b+1,b+m+1,cmp);
std::sort(c+1,c+n+1);
build(1,1,n);
for(i=j=1;i<=m;i++){
while(j<=n&&a[j].x<=b[i].x)change(lower(a[j].y),a[j].y),j++;
ans[b[i].p]=ask(b[i].y);
}
for(i=1;i<=m;i++)printf("%lld\n",ans[i]);
return 0;
}
BZOJ3022 : [Balkan2012]The Best Teams的更多相关文章
- 【BZOJ】3022: [Balkan2012]The Best Teams
原题链接 题面 (为啥这题没有题面-- 给出\(N\)个人,和年龄\(age_{i},skill_{i}\) 然后给出\(M\)个询问,就是年龄在\(a\)以下选不超过\(k\)个人 要求选择的人水平 ...
- BZOJ 3022 [Balkan2012]The Best Teams(扫描线+线段树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3022 [题目大意] 给定n个球员,第i个球员年龄为AGEi,水平为SKILLi. 没有 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- Rnadom Teams
Rnadom Teams 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.actioncid=88890#problem/B 题目: Descript ...
- URAL 1208 Legendary Teams Contest(DFS)
Legendary Teams Contest Time limit: 1.0 secondMemory limit: 64 MB Nothing makes as old as years. A l ...
- timus 1106 Two Teams(二部图)
Two Teams Time limit: 1.0 secondMemory limit: 64 MB The group of people consists of N members. Every ...
- CF478 B. Random Teams 组合数学 简单题
n participants of the competition were split into m teams in some manner so that each team has at le ...
- UVA 11609 Teams 组合数学+快速幂
In a galaxy far far away there is an ancient game played among the planets. The specialty of the gam ...
- SCAU 07校赛 10317 Fans of Footbal Teams
10317 Fans of Footbal Teams 时间限制:1000MS 内存限制:65535K 题型: 编程题 语言: 无限制 Description Two famous footba ...
随机推荐
- 论文阅读笔记二十二:End-to-End Instance Segmentation with Recurrent Attention(CVPR2017)
论文源址:https://arxiv.org/abs/1605.09410 tensorflow 代码:https://github.com/renmengye/rec-attend-public 摘 ...
- 常见的User-Agent及免费代理IP网站
常见的User-Agent 1.Android Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 ...
- Python学生信息管理系统的开发
# 第一题:设计一个全局变量,来保存很多个学生信息:学生(学号, 姓名,年龄):思考要用怎样的结构来保存:# 第二题:在第一题基础上,完成:让用户输入一个新的学生信息(学号,姓名,年龄):你将其保存在 ...
- Sqoop使用,mysql,hbase,hive等相互转换
Sqoop 是一款用来在不同数据存储软件之间进行数据传输的开源软件,它支持多种类型的数据储存软件. 安装 Sqoop 1.下载sqoop并加mysql驱动包 http://mirror.bit.edu ...
- net core 使用 rabbitmq
windows环境安装: https://www.cnblogs.com/ericli-ericli/p/5902270.html .NET Core 使用RabbitMQ https://www.c ...
- 原生开发小程序 和 wepy 、 mpvue, Taro 对比
https://blog.csdn.net/yang450712123/article/details/79623518 mpvue https://www.cnblogs.com/bgwhite/p ...
- docker-java Docker的java API
docker-java docker-java 是 Docker的 Java 版本API Docker 当前的实现基于 Jersey 2.x 因此 classpath 不兼容老版本的 Jersey 1 ...
- 修改element ui 默认样式最好的解释
KedAyAyA 17年10月 https://forum.vuejs.org/t/elementui/19171/5 首先添加了scoped的style标签会在vue-loader里进行处理 所谓的 ...
- mysql中trim()函数的用法
去除左空格函数: LTRIM(str) mysql> SELECT LTRIM(' barbar'); -> 'barbar' 去除右空格函数: RTRIM(str) mysql> ...
- FastJson 数组、List、Set、Map基本序列化与日期格式化
摘要: FastJson可以直接序列化数组.List.Set.Map等,也可以直接支持日期类型数据的格式化,包括java.util.Date.java.sql.Date.java.sql.Timest ...