Codeforces 1221F. Choose a Square
对于某个点 $(x,y)$ ,不妨设 $x<y$ 因为如果 $x>y$ 直接按 $y=x$ 对称一下即可
当且仅当正方形左下角 $(a,a)$ 满足 $a<=x$,右上角 $(b,b)$ 满足 $b>=y$ ,才能得到这个点的价值
所以发现其实是个二维偏序的问题,直接把 $(a,b)$ 看成另一个平面上的点,$(x,y)$ 放到那个平面上
这样就问题变成选一个点 $(a,b)$ ,你得到的价值为所有 $x>=a$ 并且 $y<=b$ 的点 $(x,y)$ 的价值和再减去 $a,b$ 之间的差值
考虑把点按第一关键字 $x$ 从大到小,按第二关键字 $y$ 从小到大排序,维护一个线段树表示当前 $a=x$ 的情况下 $y$ 取各个值时能够得到的最大价值
因为当前 $a=x$ 的情况下,所有 $x>a$ 的点的代价都加入了,每次加入一个点以后直接查询线段树上 $b>=y$ 和下一个点 $b<=y'-1$ 之间的那一段的最大价值,当然如果下一个点的 $x$ 和当前点不同,那么查询直接查询当前点到线段树最大位置的值即可
发现这样是有问题的,因为对于不同的 $b=y$ ,直接取点的值最大还不行,因为代价还要考虑 $-(y-x)$,所以线段树上维护的应该是点值和再减 $y$ 以后的最大值,当然要随便维护一下取最大值时 $b$ 的值
然后就没了,代码因为主体是比赛时写的,可能比较丑,但是还能看...吧
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=1e6+;
const ll INF=1e18;
int n,x[N],y[N],c[N],t[N],xx[N],yy[N];
struct dat {
int l,r,val,id;
dat (int _l=,int _r=,int _val=,int _id=) { l=_l,r=_r,val=_val,id=_id; }
inline bool operator < (const dat &tmp) const {
if(l!=tmp.l) return l>tmp.l;
return r!=tmp.r ? r<tmp.r : val>tmp.val;
}
}d[N];
int tot;
int inv[N];
struct SegTree {
ll T[N<<],id[N<<],tag[N<<];
inline void pushup(int o) { T[o]=max(T[o<<],T[o<<|]); id[o]=T[o<<]>=T[o<<|] ? id[o<<] : id[o<<|]; }
inline void pushdown(int o,int l,int r)
{
if(!o||!tag[o]) return;
T[o]+=tag[o]; if(l==r) { tag[o]=; return; }
tag[o<<]+=tag[o]; tag[o<<|]+=tag[o];
tag[o]=;
}
void build(int o,int l,int r)
{
if(l==r) { id[o]=l; T[o]=-inv[l]; return; }
int mid=l+r>>; build(o<<,l,mid); build(o<<|,mid+,r);
pushup(o);
}
inline void change(int o,int l,int r,int ql,int qr,int v)
{
pushdown(o,l,r);
if(l>qr||r<ql) return;
if(l>=ql&&r<=qr) { tag[o]=v; pushdown(o,l,r); return; }
int mid=l+r>>;
change(o<<,l,mid,ql,qr,v); change(o<<|,mid+,r,ql,qr,v);
pushup(o);
}
ll pos,val;
inline void query(int o,int l,int r,int ql,int qr)
{
pushdown(o,l,r);
if(l>qr||r<ql) return;
if(l>=ql&&r<=qr) { if(T[o]>val) val=T[o],pos=id[o]; return; }
int mid=l+r>>; query(o<<,l,mid,ql,qr); query(o<<|,mid+,r,ql,qr);
pushup(o);
}
}ST;
int main()
{
n=read();
for(int i=;i<=n;i++) xx[i]=x[i]=read(),yy[i]=y[i]=read(),c[i]=read();
for(int i=;i<=n;i++) t[++tot]=x[i],t[++tot]=y[i];
sort(t+,t+tot+); tot=unique(t+,t+tot+)-t-;
for(int i=;i<=n;i++) x[i]=lower_bound(t+,t+tot+,x[i])-t;
for(int i=;i<=n;i++) y[i]=lower_bound(t+,t+tot+,y[i])-t;
int ansx=,ansy=;
for(int i=;i<=n;i++)
{
if(x[i]>y[i]) swap(x[i],y[i]),swap(xx[i],yy[i]);
d[i]=dat(x[i],y[i],c[i],i);
ansx=max(ansx,yy[i]+); ansy=max(ansy,yy[i]+);
inv[x[i]]=xx[i]; inv[y[i]]=yy[i];
}
sort(d+,d+n+); ll ans=; ST.build(,,tot);
d[n+].l=-;
for(int i=;i<=n;i++)
{
ST.change(,,tot,d[i].r,tot,d[i].val);
ST.val=-INF; ST.pos=;
if(d[i].l==d[i+].l) ST.query(,,tot,d[i].r,d[i+].r-);
else ST.query(,,tot,d[i].r,tot);
ll res=ST.val+inv[d[i].l];
if(res>ans) ans=res,ansx=inv[d[i].l],ansy=inv[ST.pos];
}
printf("%lld\n",ans);
printf("%d %d %d %d\n",ansx,ansx,ansy,ansy);
return ;
}
Codeforces 1221F. Choose a Square的更多相关文章
- Codeforces Round #448 C. Square Subsets
题目链接 Codeforces Round #448 C. Square Subsets 题解 质因数 *质因数 = 平方数,问题转化成求异或方程组解的个数 求出答案就是\(2^{自由元-1}\) , ...
- Codeforces 1221 F Choose a Square
题面 不知道大佬们怎么想的,反正我看到这种区间包含性质的并且score只和包含的区间与询问区间挂钩的题,马上就想到了扫描线23333 虽然革命方向无比正确,但却因为SB错误交了5次才 A. WA第一发 ...
- Codeforces 710C. Magic Odd Square n阶幻方
C. Magic Odd Square time limit per test:1 second memory limit per test:256 megabytes input:standard ...
- Codeforces 716C. Plus and Square Root-推公式的数学题
http://codeforces.com/problemset/problem/716/C codeforces716C. Plus and Square Root 这个题就是推,会推出来规律,发现 ...
- Codeforces 1187 F - Expected Square Beauty
F - Expected Square Beauty 思路:https://codeforces.com/blog/entry/68111 代码: #pragma GCC optimize(2) #p ...
- Codeforces 715A. Plus and Square Root[数学构造]
A. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 【模拟】Codeforces 710C Magic Odd Square
题目链接: http://codeforces.com/problemset/problem/710/C 题目大意: 构造一个N*N的幻方.任意可行解. 幻方就是每一行,每一列,两条对角线的和都相等. ...
- Codeforces 679C Bear and Square Grid
Bear and Square Grid 枚举k * k 的位置, 然后接上它周围白色连通块的数量, 再统计完全在k * k范围里的连通块, 这个只要某个连通块全部的方格 在k * k里面就好, 并且 ...
- CodeForces - 710C Magic Odd Square(奇数和幻方构造)
Magic Odd Square Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, c ...
随机推荐
- 设置object的key为变量
https://blog.csdn.net/shu580231/article/details/81367271
- DRL Hands-on book
代码:https://github.com/PacktPublishing/Deep-Reinforcement-Learning-Hands-On Chapter 1 What is Reinfor ...
- ARTS打卡计划第五周
Algorithms: https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 采用了map的 ...
- 同样的WiFi,手机能连上网,电脑不能。错误代码DNS_PROBE_POSSIBLE
今天电脑不知打为撒,出了这样个毛病,原因不明.先试着用电脑管家修复,无效.找了网上的很多办法,排除了dns.ip之类的问题.最后在贴吧里看到大神的解决办法,实测简单有效.链接http://tieba. ...
- Leetcode题目22.括号生成(动态规划-中等)
题目描述: 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "( ...
- (转)linux中wget未找到命令
转:https://blog.csdn.net/djj_alice/article/details/80407769 在装数据库的时候发现无法使用wget命令,提示未找到命令,如图所示 那是因为没有安 ...
- Forcepoint
Forcepoint One Endpoint Diagnostics Tool C:\Program Files\Websense\Websense Endpoint\WEPDiag.exe &qu ...
- Android新项目GBSS:第1篇 搭建开发环境
最近接手一个Android新项目,之前也没做过这方面的开发,算是边学边干,这两天看了一下Android开发的书,大致入门了一点,今天把所需要的软件都下了下来,准备开工,先列一下开发环境: 所有的软件都 ...
- leetcode-hard-array- 227. Basic Calculator II
mycode 29.58% class Solution(object): def calculate(self, s): """ :type s: str :rtyp ...
- DP&图论 DAY 4 下午图论
DP&图论 DAY 4 下午 后天考试不考二分图,双联通 考拓扑排序 图论 图的基本模型 边: 有向边构成有向图 无向边构成无向图 权值: 1.无权 2.点权 3.边权 4.负权(dij不 ...