BZOJ3022 : [Balkan2012]The Best Teams

时间:2022-01-13 03:53:47

将选手和询问按照年龄排序,即可去掉年龄的限制。

将所有选手按水平排序后维护线段树,显然最优解一定是从大到小贪心选择。

线段树上每个节点维护:

$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的更多相关文章

  1. 【BZOJ】3022&colon; &lbrack;Balkan2012&rsqb;The Best Teams

    原题链接 题面 (为啥这题没有题面-- 给出\(N\)个人,和年龄\(age_{i},skill_{i}\) 然后给出\(M\)个询问,就是年龄在\(a\)以下选不超过\(k\)个人 要求选择的人水平 ...

  2. BZOJ 3022 &lbrack;Balkan2012&rsqb;The Best Teams(扫描线&plus;线段树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3022 [题目大意] 给定n个球员,第i个球员年龄为AGEi,水平为SKILLi. 没有 ...

  3. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  4. Rnadom Teams

    Rnadom  Teams 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.actioncid=88890#problem/B 题目: Descript ...

  5. 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 ...

  6. timus 1106 Two Teams&lpar;二部图&rpar;

    Two Teams Time limit: 1.0 secondMemory limit: 64 MB The group of people consists of N members. Every ...

  7. CF478 B&period; Random Teams 组合数学 简单题

    n participants of the competition were split into m teams in some manner so that each team has at le ...

  8. UVA 11609 Teams 组合数学&plus;快速幂

    In a galaxy far far away there is an ancient game played among the planets. The specialty of the gam ...

  9. SCAU 07校赛 10317 Fans of Footbal Teams

    10317 Fans of Footbal Teams 时间限制:1000MS  内存限制:65535K 题型: 编程题   语言: 无限制 Description Two famous footba ...

随机推荐

  1. html内的空格占位

    写html的时候有时因为字数不够会根据字段长度添加多个空格,但是在html中添加空格是没有用的,所以使用空格的代替符号有:   不断行的空白(1个字符宽度)   半个空白(1个字符宽度)   一个空白 ...

  2. C&plus;&plus;&commat;类的静态成员变量和静态成员函数

    参考: http://blog.csdn.net/morewindows/article/details/6721430 http://www.cnblogs.com/lzjsky/archive/2 ...

  3. MVC运行原理

    Global.asax Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法.你可以使用这个文件实现应用程序安全性以及其它一 ...

  4. Linux 命令 - mkdir&colon; 创建目录

    命令格式 mkdir [OPTION]... DIRECTORY... 命令参数 -m, --mode=MODE 设置文件的模式,类似于 chmod 命令. -p, --parents 需要时创建指定 ...

  5. python基础知识十一

    图形软件 使用Python的GUI库——你需要使用这些库来用Python语言创建你自己的图形程序.使用GUI库和它们的Python绑定,你可以创建你自己的IrfanView.Kuickshow软件或者 ...

  6. jQuery EasyUI 1&period;4&period;4 Combobox无法检索中文输入的问题

    在项目里使用了EasyUI的Combobox,当ComboBox的item是英文时,都能正常检索出对应项,但是如果使用中文输入法输入几个字母然后通过按shift键输入时,奇怪的事情发生了,combob ...

  7. jQuery validate api&lpar;转&rpar;

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...

  8. SourceInsight - 常用设置和快捷键大全

    1. 让{ 和 } 不缩进 Options -> Document Options -> Auto Indenting -> Auto Indent Type 选 Simple 2. ...

  9. 关于vmvawe的光驱,iso镜像&comma;挂载,卸载

    勾选这个使用iso镜像文件,就相当于真实的环境下,将一张光盘插进了光驱里,然后再勾选启动时连接,那么在linux开机后,/dev/cdrom或者/dev/sr0(前者是后者的软连接)就表示这个硬件设备 ...

  10. linux神器strace

    man strace: strace - trace system calls and signals DESCRIPTION In the simplest case strace runs the ...