[CF542A]Place Your Ad Here

题目大意:

有\(n(n\le2\times10^5)\)个广告和\(m(m\le2\times10^5)\)个电视台,第\(i\)个广告只能在\([l_i,r_i]\)内播放,第\(j\)个电视台会在时间段\([a_j,b_j]\)播出,并且有\(c_j\)个人收看。选择第\(x\)个广告和第\(y\)个电视台的收益为\((v-u)c_y\),其中\([u,v]=[l_x,r_x]\cap[a_y,b_y]\)。

从中选取一个广告和一个电视台,使收益最大。求最大收益,并输出任意一种方案。

思路:

将广告拆成两个端点排序,将电视台按照右端点排序。

枚举每个电视台\([l,r]\),若一个广告只有左端点在\(r\)前,那么我们只关心最左的左端点;若一个广告左右端点均在\([l,r]\)内,那么我们只关心其长度;若一个广告左端点在\(l\)前,右端点在\([l,r]\)内,那么我们只关心其右端点的位置。

对于第一种情况,用set维护即可;后两种情况只需用线段树维护区间最大值。

时间复杂度\(\mathcal O(n\log n)\)。

源代码:

#include<set>
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
struct Node {
int p,id;
bool operator < (const Node &rhs) const {
return p<rhs.p;
}
};
struct Seg {
int l,r,w,id;
bool operator < (const Seg &rhs) const {
return r<rhs.r;
}
};
const int N=2e5+1,K=8e5+1;
Node a[N],b[N];
Seg c[N];
int rnk[N],tmp[K];
class SegmentTree {
#define _left <<1
#define _right <<1|1
#define mid ((b+e)>>1)
private:
std::pair<int,int> val[K<<2];
void push_up(const int &p) {
val[p]=std::max(val[p _left],val[p _right]);
}
public:
void modify(const int &p,const int &b,const int &e,const int &x,const std::pair<int,int> &v) {
if(b==e) {
val[p]=std::max(val[p],v);
return;
}
if(x<=mid) modify(p _left,b,mid,x,v);
if(x>mid) modify(p _right,mid+1,e,x,v);
push_up(p);
}
std::pair<int,int> query(const int &p,const int &b,const int &e,const int &l,const int &r) const {
if(b==l&&e==r) return val[p];
std::pair<int,int> ret(0,0);
if(l<=mid) ret=std::max(ret,query(p _left,b,mid,l,std::min(mid,r)));
if(r>mid) ret=std::max(ret,query(p _right,mid+1,e,std::max(mid+1,l),r));
return ret;
}
#undef _left
#undef _right
#undef mid
};
SegmentTree t1,t2;
std::set<std::pair<int,int> > set;
int main() {
const int n=getint(),m=getint();
int tot=0;
for(register int i=1;i<=n;i++) {
const int l=getint(),r=getint();
a[i]=(Node){l,i};
b[i]=(Node){r,i};
tmp[++tot]=l;
tmp[++tot]=r;
}
for(register int i=1;i<=m;i++) {
tmp[++tot]=c[i].l=getint();
tmp[++tot]=c[i].r=getint();
c[i].w=getint();
c[i].id=i;
}
std::sort(&tmp[1],&tmp[tot]+1);
for(register int i=1;i<=n;i++) {
a[i].p=std::lower_bound(&tmp[1],&tmp[tot]+1,a[i].p)-tmp;
b[i].p=std::lower_bound(&tmp[1],&tmp[tot]+1,b[i].p)-tmp;
}
for(register int i=1;i<=m;i++) {
c[i].l=std::lower_bound(&tmp[1],&tmp[tot]+1,c[i].l)-tmp;
c[i].r=std::lower_bound(&tmp[1],&tmp[tot]+1,c[i].r)-tmp;
}
std::sort(&a[1],&a[n]+1);
std::sort(&b[1],&b[n]+1);
std::sort(&c[1],&c[m]+1);
for(register int i=1;i<=n;i++) {
rnk[a[i].id]=i;
}
int64 ans=0;
int x,y;
for(register int i=1,j=1,k=1;i<=m;i++) {
for(;j<=n&&a[j].p<=c[i].r;j++) {
set.insert(std::make_pair(tmp[a[j].p],a[j].id));
}
for(;k<=n&&b[k].p<=c[i].r;k++) {
const int j=rnk[b[k].id];
set.erase({tmp[a[j].p],a[j].id});
t1.modify(1,1,tot,a[j].p,{tmp[b[k].p]-tmp[a[j].p],a[j].id});
t2.modify(1,1,tot,a[j].p,{tmp[b[k].p],a[j].id});
}
//-: ad
//=: tv
int len=0,z=0;
if(!set.empty()) {
// ------
//======
const int t=tmp[c[i].r]-std::max(tmp[c[i].l],set.begin()->first);
if(t>len) {
len=t;
z=set.begin()->second;
}
}
{
// --
//======
const auto p=t1.query(1,1,tot,c[i].l,c[i].r);
if(p.first>len) {
len=p.first;
z=p.second;
}
}
{
//------
// ======
const auto p=t2.query(1,1,tot,1,c[i].l);
if(p.first-tmp[c[i].l]>len) {
len=p.first-tmp[c[i].l];
z=p.second;
}
}
if(1ll*len*c[i].w>ans) {
ans=1ll*len*c[i].w;
x=z,y=c[i].id;
}
}
printf("%lld\n",ans);
if(ans) printf("%d %d\n",x,y);
return 0;
}

[CF542A]Place Your Ad Here的更多相关文章

  1. Sharepoint2013 AD组用户不同步

    背景: SP2013列表库使用AD安全组授权访问,向AD安全组添加一个用户A,在Sharepoint AD同步(增量和完全)后,用户A仍然无法访问列表库:原因: 参考:安全令牌上的缓存  SP2013 ...

  2. freeradius整合AD域作anyconncet认证服务器

    一.服务器要求 Radius服务器:centos6.6.hostname.selinux  disabled.stop iptables AD域服务器:Windows Server 2008 R2 E ...

  3. 讲座:Modeling User Engagement for Ad and Search

    讲座:http://bdai.ruc.edu.cn/?p=118 Modeling User Engagement for Ad and Search ppt 链接: Dr. Ke(Adam) Zho ...

  4. Azure AD Connect 手动同步

    我们目前采用工具Azure AD Connect 目录同步工具将本地域控制器的用户信息同步至office365和Azure 在之前目录同步工具中使用Windows 任务计划程序或单独的 Windows ...

  5. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...

  6. 如何查看/统计当前AD域控制器的活动用户?

    最近公司想知道某台AD域控制器上当前连接了多少活动用户? 此前个人只知道以下不是非常完善且统计起来比较麻烦的方法: 方法1:查看共享会话数.(不完全准确) 方法2:查看当前的DNS记录.(这种方法统计 ...

  7. AD域-让共享目录只显示用户有权限访问的文件夹

    问题: 在AD域中,我们一般都会用到共享,如果有很多部门,我们可能还会按部门.职位配置权限.比如CSD,IT,PA等,但文件夹一多,用户看着就头大,而且用户没权限访问的文件夹误点击进去还会提示无权限访 ...

  8. AD域的安装(在Windows Server 2003中安装Active Directory)

    在Active Directory中提供了一组服务器作为身份验证服务器或登录服务器,这类服务器被称作域控制器(Domain Controller,简称DC).建立一个AD域的过程实际就是在一台运行Wi ...

  9. 使用Ruby来实现批量更新AD中字段

    准备工作 安装需要用到的gem gem install net-ldap gem install roo 准备好要更新的数据,比如exel表: /root/account.xlsx,内容如下 姓名 性 ...

随机推荐

  1. Python读写文件的几种方式

    一.pandas pandas模块是数据分析的大杀器,它使得对于文件相关的操作变得简单. 看一下它的简单使用 import pandas as pd # 读取 df = pd.read_csv('al ...

  2. 第四节:框架前期准备篇之进程外Session的两种配置方式

    一. 基本介绍 1. 背景:Asp.Net默认的Session机制是进程内,存储在服务器端内存中,有这么几个缺点: ①:既然存在内存中,空间有限,不能存储大数据量信息,数据量多的话Session会被挤 ...

  3. 轴对称 Navier-Stokes 方程组的点态正则性准则 II

    在 [Wei, Dongyi. Regularity criterion to the axially symmetric Navier-Stokes equations. J. Math. Anal ...

  4. mpvue体验微信小程序开发

    微信小程序 https://developers.weixin.qq.com/miniprogram/introduction/index.html?t=18082114 微信小程序是一种全新的连接用 ...

  5. Nginx详解篇

    Nginx主配置文件和参数: Nginx的默认站点目录是Nginx安装目录/application/nginx/下的html目录,如果要部署网站业务,只需要把开发号好的程序全部放置到/applicat ...

  6. ccse(CountDownLatch,CycliBarrier,Semaplore,Exchanger)

    关于等待状态的线程调用interrupt方法报异常:InterruptedException 当线程被阻塞,比如wait,join,sleep等,在调用interrupt方法,没有占用cpu运行的线程 ...

  7. ZOC7在Mac下发送命令到多个窗口设置

    1 详见截图,找了半天 2 然后,下边框就会出现命令发送多个窗口的输入框了

  8. mysql语句实战

    请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号:   这个比较难, 3.查询平均成绩大于60分的同学的学号和平均成绩: 4.查 ...

  9. Rich feature hierarchies for accurate object detection and semantic segmentation(理解)

    0 - 背景 该论文是2014年CVPR的经典论文,其提出的模型称为R-CNN(Regions with Convolutional Neural Network Features),曾经是物体检测领 ...

  10. 第一章 Java程序设计概述

    1.1 Java程序设计平台 Java是一门设计优秀的语言,更是一个完整的平台.Java平台包括了一个庞大可重用的类库以及提供了安全性,跨系统,自动垃圾收集等优秀特性的执行环境. 这也使其成为自发布以 ...