这道题里线段树用来区间更新(每次给更大的区间加上当前区间的权重),用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. FLV文件格式分析(附源码)

    FLV文件主要由两部分组成:Header和Body. 1. Header header部分记录了flv的类型.版本等信息,是flv的开头,一般都差不多,占9bytes.具体格式如下: 文件类型 3 b ...

  2. 知识图谱学习与实践(6)——从结构化数据进行知识抽取(D2RQ介绍)

    1 概述 D2RQ,含义是把关系型数据库当作虚拟的RDF图数据库进行访问.D2RQ平台是一个将关系型数据库当作虚拟的.只读的RDF图数据库进行访问的系统.提供了基于RDF访问关系数据库的内容,而无需复 ...

  3. Git-配置SSH公钥

    前言:Git是分布式的代码管理工具,远程的代码管理是基于SSH的,所以要使用远程的Git则需要SSH的配置. 以下操作都在git-bash命令行中进行. 查看所有配置项: git config --l ...

  4. 牛客腾讯2020校园招聘-后台&综合-第一次笔试

    第一题 Q: 小Q想要给他的朋友发送一个神秘字符串,但是他发现字符串的过于长了,于是小Q发明了一种压缩算法对字符串中重复的部分进行了压缩,对于字符串中连续的m个相同字符串S将会压缩为m|S,例如字符串 ...

  5. 虚拟机安装的ubuntu不能联网解决

    安装双系统从没遇到的问题,再虚拟机上遇到了不能联网的问题: 下面给出我的解决方法(win10系统.ubuntu 16.04) 我的电脑-管理-设备管理器 看是否虚拟机的虚拟网卡在: 在去设置-控制面板 ...

  6. weinre 真机调试

    1.在任意文件夹 安装weinre cnpm -g install weinre 2.启动 weinre --httpPort 8009 --boundHost -all- 3.在你的页面中加入 本机 ...

  7. Unity 鼠标旋转物体360展示

    PC端 using UnityEngine; using System.Collections; public class DragRound : MonoBehaviour { public Tra ...

  8. 瀑布流无限加载infinitescroll插件与masonry插件使用

    masonry官网地址http://masonry.desandro.com/,infinitescroll官网地址http://www.infinite-scroll.com/ 无限滚动原理:无限滚 ...

  9. Python学习之函数篇

    python查看对象,函数帮助文档:.__doc__,例:str.split.__doc__ 查看详细对象,函数文档:help(),例:help(str.split) 函数参数可设置默认值 如果不能提 ...

  10. Postman的使用和测试

    1.输入认证的IP,获取headers 2.输入用户名及密码 3.带着headers去访问网址 4.传参