这道题里线段树用来区间更新(每次给更大的区间加上当前区间的权重),用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. 动态设置微信小程序 navigationBarTitle 的值

    wx.setNavigationBarTitle({ title:' 动态值 ' })

  2. 微信小程序 scroll-view 左右横向滑动没有效果(无法滑动)问题

    小程序组件 scroll-view 中分别有上下竖向滑动和左右横向滑动之分,在这次项目中刚好需要用到横向滑动,但在测试过程中发现横向滑动没有了效果(静止在那里没移动过),经调试发现: 1.scroll ...

  3. 30分钟编写一个抓取 Unsplash 图片的 Python爬虫

       我一直想用 Python and Selenium 创建一个网页爬虫,但从来没有实现它. 几天前, 我决定尝试一下,这听起来可能是挺复杂的, 然而编写代码从 Unsplash 抓取一些美丽的图片 ...

  4. HDU 3823 Prime Friend(线性欧拉筛+打表)

    Besides the ordinary Boy Friend and Girl Friend, here we define a more academic kind of friend: Prim ...

  5. 转:为什么说Java中只有值传递

    原文:https://www.cnblogs.com/wchxj/p/8729503.html 错误理解 在开始深入讲解之前,有必要纠正一下大家以前的那些错误看法了.如果你有以下想法,那么你有必要好好 ...

  6. HBase 启动后HMaster进程自动消失

    原因分析 1.hadoop 与 hbase 版本不兼容,导致的异常. 2.log日志信息显示 org.apache.hadoop.hbase.TableExistsException: hbase:n ...

  7. P1558 色板游戏 线段树(区间修改,区间查询)

    题意: 给n,m,k,n长度,k个操作,m种颜色 操作C:输入A,B,C,区间[A,B]变成C颜色,可能A>B,所以要确保A<B 操作P:输入A,B,区间[A,B]的颜色种类 思路: 因为 ...

  8. Atcoder Beginner Contest153F(模拟)

    应该也可以用线段树/树状数组区间更新怪兽的生命值来做 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespac ...

  9. Go错误

    1. error package main import ( "errors" "fmt" ) func main() { /* error:内置的数据类型,内 ...

  10. BFSDFS模板

    BFS模板: private static void bfs(HashMap<Character, LinkedList<Character>> graph,HashMap&l ...