这道题里线段树用来区间更新(每次给更大的区间加上当前区间的权重),用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. Navicat图形更改表结构的时,设置外键时出现1452错误

    原文地址:http://www.mamicode.com/info-detail-1296600.html 提示1452错误,如下图所示. 然后百度了一下,得到了一个靠谱的答案: 这是因为表设置了外键 ...

  2. hive导出数据到本地文件报错解决方法

    报错信息: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.MoveTask. Unable to move so ...

  3. java 数据类型优先级

    由低到高:byte,short,char—> int —> long—> float —> double 1. 不能对boolean类型进行类型转换. 2. 不能把对象类型转换 ...

  4. Validation failed for one or more entities. See ‘EntityValidationErrors

    try{ context.SaveChanges(); } catch (DbEntityValidationException ex) { var errorMessages = ex.Entity ...

  5. AI人工智能之基于OpenCV+face_recognition实现人脸识别

    因近期公司项目需求,需要从监控视频里识别出人脸信息.OpenCV非常庞大,其中官方提供的人脸模型分类器也可以满足基本的人脸识别,当然我们也可以训练自己的人脸模型数据,但是从精确度和专业程度上讲Open ...

  6. MAC记住 git的用户名密码

    问题:第一次使用MAC的git垃取代码时,连续输错密码.以为垃取不下来,就让同事用它的git账号和密码垃取了一次拉去成功了.之后我再配置git的用户名和密码设置称自己的.往后每次拉去和提交都显示同事的 ...

  7. 关于原生,webapp,hybird(混合)

    链接:https://www.jianshu.com/p/839748d571b2 链接2:https://www.jianshu.com/p/6d5f32aa5dda

  8. 吴裕雄 python 人工智能——基于Mask_RCNN目标检测(3)

    import os import sys import random import math import re import time import numpy as np import cv2 i ...

  9. Smart License

    思科启动了通过构建思科智能软件管理器门户来简化客户许可管理的计划. 它可以帮助客户了解他们购买的许可证以及他们使用的许可证. 其他各种思科产品已经启用Smart Enabled,随着此版本(我这里学习 ...

  10. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) - D2. Optimal Subsequences (Hard Version)(主席树)

    题意:一共有$n$个数,$m$次询问,每次询问包括$k.pos$两个数,需要你从这$n$个数里面找出$k$个数,使得他们的总和最大,如果有多种情况,找出序号字典序最小的一组,然后输出这个序列中第$po ...