[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. MyBatis小问题(1)-Mapper中错误No constructor found...

    前两天又被公司叫去修改其他产品的一些问题了,没有看java相关的,今天周六,看了看MyBatis东西. 就是简单的在MySql中建了个users表,很简单,包含id,name,age,写了个bean. ...

  2. 【SQL】INSERT INTO SELECT语句与SELECT INTO FROM语句

    INSERT INTO SELECT语句与SELECT INTO FROM语句,都是将一个结果集插入到一个表中: #INSERT INTO SELECT语句 1.语法形式: Insert into T ...

  3. .net Core 下数据库访问

    SqlSugar :是一款高性能(达到ADO.NET最高性能水平)SqlSugar :是除EF外拉姆达解析最完善的ORM,多表 .UnionALL. 交叉子查询.真实的批量操作和分页SqlSugar ...

  4. vue使用md5,base64方法

    在前端加密代码虽然对安全没有提高,但是可以避免明文传输,提供用户隐私保护,还是很有必要的. 首先安装js-md5,js-base64. 在vue中引入. 之后就可以直接使用了,一般的做法是先把密码转行 ...

  5. PL/SQl编程 基本语法

    /*输出hello world*/ DECLARE BEGIN DBMS_OUTPUT.PUT_LINE('Hello World'); END; --set serveroutput on; /** ...

  6. Java同步容器

    一.为什么会出现同步容器 Java的集合框架中,主要有四大类别:List,Set,Queue,Map List,Set,Queue接口分别继承了Collection接口,Map本身是一个接口. 注意C ...

  7. websocket 与Socket.IO介绍

    一  websocket WebSocket是html5新增加的一种通信协议,目前流行的浏览器都支持这个协议,例如 Chrome,Safrie,Firefox,Opera,IE等等,对该协议支持最早的 ...

  8. 用户态与内核态 & 文件流与文件描述符 简介

    用户态和内核态 程序代码的依赖和调用关系如下图所示: Lib:标准ASCI C函数,几乎所有的平台都支持该库函数,因此依赖该库的程序可移植性好: System Function:系统调用函数,与系统内 ...

  9. 【原创】大叔经验分享(45)kibana添加index pattern卡住 返回403 Forbidden

    kibana添加index pattern卡住,通过浏览器查看请求返回状态为403 Forbidden,返回消息为: {"message":"blocked by: [F ...

  10. C# 微信开发-----微信会员卡(一)

    这是微信的官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025283,能看懂的朋友就请不要往下看了,我是看不懂 ...