这道题里线段树用来区间更新(每次给更大的区间加上当前区间的权重),用log的复杂度加快了更新速度,也用了区间查询(查询当前区间向右直至最右中以当前区间端点向右一段区间的和中最大的那一段的和),也用log的复杂度加快了查询速度。

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int x[],y[];
long long a[];
int cc[];
vector<pair<int,long long> >v[];
long long mx[],lz[],mxid[];
long long cmx,cid;
int n,cnt;
void up(int rt){
if(mx[rt<<]>=mx[rt<<|]){
mx[rt]=mx[rt<<];
mxid[rt]=mxid[rt<<];
}
else{
mx[rt]=mx[rt<<|];
mxid[rt]=mxid[rt<<|];
}
}
void down(int rt){
lz[rt<<]+=lz[rt];
lz[rt<<|]+=lz[rt];
mx[rt<<]+=lz[rt];
mx[rt<<|]+=lz[rt];
lz[rt]=;
}
void build(int rt,int l,int r){
if(l==r){
mx[rt]=-cc[l];
mxid[rt]=l;
return ;
}
build(rt<<,l,(l+r)>>);
build(rt<<|,((l+r)>>)+,r);
up(rt);
}
void update(int rt,int l,int r,int L,int R,long long k){
if(L<=l&&r<=R){
lz[rt]+=k;
mx[rt]+=k;
return ;
}
down(rt);
if(L<=((l+r)>>))
update(rt<<,l,(l+r)>>,L,R,k);
if(R>((l+r)>>))
update(rt<<|,((l+r)>>)+,r,L,R,k);
up(rt);
}
void query(int rt,int l,int r,int L,int R){
if(L<=l&&r<=R){
if(mx[rt]>cmx){
cmx=mx[rt];
cid=mxid[rt];
}
return ;
}
down(rt);
if(L<=((l+r)>>))
query(rt<<,l,(l+r)>>,L,R);
if(R>((l+r)>>))
query(rt<<|,((l+r)>>)+,r,L,R);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cnt=;
cin>>n;
for(int i=;i<=n;++i){
cin>>x[i]>>y[i]>>a[i];
cc[++cnt]=x[i];
cc[++cnt]=y[i];
}
sort(cc+,cc++cnt);
cnt=unique(cc+,cc++cnt)-(cc+);//去重
for(int i=;i<=n;++i){
x[i]=lower_bound(cc+,cc++cnt,x[i])-cc;//离散化后的区间标号,不是坐标标号
y[i]=lower_bound(cc+,cc++cnt,y[i])-cc;//离散化后的区间标号
if(x[i]>y[i])
swap(x[i],y[i]);
v[x[i]].push_back({y[i],a[i]});
}
long long ans=;
int cx=cc[cnt]+;
int cy=cx;
build(,,cnt);
//相当于把点坐标按照它们的纵坐标全部投影在y=x这条线上,同时为了使枚举复杂度降低,利用相对位置代替坐标,这里的数组下标指的是第几个区间,而不是第几个单位长度
for(int i=cnt;i;--i){//固定正方形的左下端点所在区间端点,每次选取从当前区间端点向右的连续的一段区间作为当前区间最大值的所在区间
for(int j=;j<v[i].size();++j){
int r=v[i][j].first;//纵轴上的区间端点
long long w=v[i][j].second;
update(,,cnt,r,cnt,w);//更新当前纵轴上区间端点右边区间的值,如果右边区间被选中的话,它左边区间里的点都会被选中,所以更新更右侧的区间最大值
}
cmx=-2e18;
cid=-;
query(,,cnt,i,cnt);//查询区间最大值,即查询是否有区间【i,j】这一段区间内的点的和减去j这个区间端点(相对位置)的纵坐标是至今为止最大的,有的话就更新最大值和此时的横纵坐标
if(ans<cmx+cc[i]){
ans=cmx+cc[i];//新的最大值
cx=cc[i];//此时的横坐标
cy=cc[cid];//此时的纵坐标
}
}
cout<<ans<<"\n";
cout<<cx<<" "<<cx<<" "<<cy<<" "<<cy;
return ;
}

Educational Codeforces Round 73 (Rated for Div. 2)F(线段树,扫描线)的更多相关文章

  1. Educational Codeforces Round 61 (Rated for Div. 2) G(线段树,单调栈)

    #include<bits/stdc++.h>using namespace std;int st[1000007];int top;int s[1000007],t[1000007];i ...

  2. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  3. Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块

    Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ​ ...

  4. Educational Codeforces Round 73 (Rated for Div. 2)

    传送门 A. 2048 Game 乱搞即可. Code #include <bits/stdc++.h> #define MP make_pair #define fi first #de ...

  5. Educational Codeforces Round 73 (Rated for Div. 2) D. Make The Fence Great Again(DP)

    链接: https://codeforces.com/contest/1221/problem/D 题意: You have a fence consisting of n vertical boar ...

  6. Educational Codeforces Round 73 (Rated for Div. 2) B. Knights(构造)

    链接: https://codeforces.com/contest/1221/problem/B 题意: You are given a chess board with n rows and n ...

  7. Educational Codeforces Round 73 (Rated for Div. 2) C. Perfect Team

    链接: https://codeforces.com/contest/1221/problem/C 题意: You may have already known that a standard ICP ...

  8. Educational Codeforces Round 73 (Rated for Div. 2) A. 2048 Game

    链接: https://codeforces.com/contest/1221/problem/A 题意: You are playing a variation of game 2048. Init ...

  9. Educational Codeforces Round 73 (Rated for Div. 2)E(思维,博弈)

    //这道题博弈的核心就是不能让后手有一段只能放b而长度不够放a的段,并且先手要放最后一次#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h> ...

随机推荐

  1. java中的try-catch-finally中的return的执行顺序

    在这里看到了try catch finally块中含有return语句时程序执行的几种情况,但其实总结的并不全,而且分析的比较含糊.但有一点是可以肯定的,finally块中的内容会先于try中的ret ...

  2. C# RichTextBox实现背景透明

    这几天在做一个文本编辑器,要将RichTextBox的背景透明,但是发现C#的RichTextBox是不支持将背景设置为Transparent(透明). 网上找了好多方法,但都不行. 后来自己想了个办 ...

  3. Vue 实现todolist的添加删除功能

    直接上代码 vm.$emit( eventName, [-args] ) 触发当前实例上的事件 可选附加参数 传给监听器回调. <style> #app{ margin: 100px; } ...

  4. 联想ideapad关闭Fn

    1.打开bios 开启/重启电脑的时候长按Fn+F2,就可以打开bios面板 2.切换到configuration菜单 使用键盘的右箭头将切换到configuration 3.关闭Fn 使用键盘下箭头 ...

  5. 抽象工厂模式(JAVA反射)

    实例代码(JAVA):模式动机     在工厂方法模式中具体工厂负责生产具体的产品,每一个具体工厂对应一种具体产品,工厂方法也具有唯一性,一般情况下,一个具体工厂中只有一个工厂方法或者一组重载的工厂方 ...

  6. Bugku-CTF之login2(SKCTF)(hint:union,命令执行)

    Day40   login2(SKCTF) http://123.206.31.85:49165/ SKCTF{xxxxxxxxxxxxxxxxxxxxx} hint:union,命令执行  

  7. linux下实现keepalived+nginx高可用

    1 nginx负载均衡高可用 1.1 什么是负载均衡高可用 nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机后端web服务将无法提供服务, ...

  8. Go包

    1. 导入包和init函数 init().main() 是 go 语言中的保留函数.我们可以在源码中,定义 init() 函数.此函数会在包被导入时执行,例如如果是在 main 中导入包,包中存在 i ...

  9. 请描述一下 cookies,sessionStorage 和 localStorage 的区别

    原文:http://blog.csdn.net/lxcao/article/details/52809939 相同点:都存储在客户端不同点: 1.存储大小 cookie数据大小不能超过4k. sess ...

  10. 排序算法之归并排序的python实现

    采用分治法: 分割:递归地把当前序列平均分割成两半. 集成:在保持元素顺序的同时将上一步得到的子序列集成到一起(归并). 归并操作(归并算法),指的是将两个已经排序的序列合并成一个序列的操作.归并排序 ...