线段树,假设求(x1,y1)点的贡献,就找所有比该点出现时间晚,且x坐标大于x1的点中y最大的,贡献就是Y-y1,由于题目条件限制,不可能有x坐标大于(x1,y1)且y坐标大于y1的点,所以贡献肯定为正。

思路参考了这篇博客:https://blog.csdn.net/qq_39599067/article/details/82560005#accode。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node{
int x,y;
};
node xx[maxn],yy[maxn],re[maxn];
int x[maxn],y[maxn],maxv[][maxn*];
int ql,qr,v,p;
bool cmpx(node a,node b){
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
bool cmpy(node a,node b){
if(a.y==b.y)return a.x<b.x;
return a.y<b.y;
}
void update(int o,int L,int R,int pos){
if(L==R){
maxv[pos][o]=v;
return;
}
int M=L+(R-L)/;
if(p<=M)update(o*,L,M,pos);
else update(o*+,M+,R,pos);
maxv[pos][o]=max(maxv[pos][o*],maxv[pos][o*+]);
}
int query(int o,int L,int R,int pos){
int ans=;
if(ql<=L&&qr>=R){
return maxv[pos][o];
}
int M=L+(R-L)/;
if(ql<=M)ans=max(ans,query(o*,L,M,pos));
if(qr>M)ans=max(ans,query(o*+,M+,R,pos));
return ans;
}
int main(){
int n;
long long ans=;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
xx[i].x=yy[i].x=re[i].x=x[i];
xx[i].y=yy[i].y=re[i].y=y[i];
}
sort(xx+,xx++n,cmpx);
sort(yy+,yy++n,cmpy);
sort(x+,x++n);
sort(y+,y++n);
for(int i=n;i>=;i--){
int pos=lower_bound(x+,x++n,re[i].x)-x;
if(pos==n)ans+=1ll*re[i].y;
else{
ql=pos+,qr=n;
ans+=1ll*(re[i].y-query(,,n,));
}
p=pos;
v=re[i].y;
update(,,n,);
pos=lower_bound(y+,y++n,re[i].y)-y;
if(pos==n)ans+=1ll*re[i].x;
else{
ql=pos+,qr=n;
ans+=1ll*(re[i].x-query(,,n,));
}
p=pos;
v=re[i].x;
update(,,n,);
}
printf("%lld\n",ans);
}

2018网络预选赛 徐州G 线段树的更多相关文章

  1. 2018网络预选赛 徐州H 线段树+树状数组

    设读入的数组是a,树状数组用来维护a数组区间和sum,线段树用来维护一个另一个数组ssum的区间和,区间每个点a[i]*(n-i+1),那么l-r的答案是l-r的ssum-(n-r)*(sum[r]- ...

  2. 徐州网络赛G-Trace【线段树】

    There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy  ...

  3. Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵

    Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries ...

  4. 2018.07.22 codeforces750E(线段树维护状态转移)

    传送门 给出一个数字字串,给出若干个询问,询问在字串的一段区间保证出现2017" role="presentation" style="position: re ...

  5. HDU 6356 Glad You Came 2018 Multi-University Training Contest 5 (线段树)

    题目中没有明说会爆int和longlong 的精度,但是在RNG函数中不用unsigned int 会报精度,导致队友debug了很久... 根据每次生成的l,r,v对区间更新m次,然后求 i*ai的 ...

  6. 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 ...

  7. HDU-6315 Naive Operations//2018 Multi-University Training Contest 2___1007 (线段树,区间除法)

    原题地址 Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/ ...

  8. HDU 4417 Super Mario(2012杭州网络赛 H 离线线段树)

    突然想到的节约时间的方法,感觉6翻了  给你n个数字,接着m个询问.每次问你一段区间内不大于某个数字(不一定是给你的数字)的个数 直接线段树没法做,因为每次给你的数字不一样,父节点无法统计.但是离线一 ...

  9. 2019ccpc网络赛hdu6703 array(线段树)

    array 题目传送门 解题思路 操作1是把第pos个位置上的数加上\(10^7\),操作2是找到区间[1,r]中没有且大于k的最小的数.注意到k的范围是小于等于n的,且n的范围是\(10^5\),远 ...

随机推荐

  1. QBZT Day3(zhx ak IOI)

    动态规划 DP和前几天学的东西不大一样,动态规划和数据结构相比是一个非常抽象的东西 先来看看斐波那契数列 定义是F0=0,F1=1,Fn=F(n-1)+F(n-2) 0,1,1,2,3,5,8,13, ...

  2. mac实用脚本

    安全清倒废纸篓(AppleScript) on run {input, parameters} set title to "安全清倒废纸篓" set trashIsEmptyMes ...

  3. ural 2022 Riding a Toad

    2022. Riding a Toad Time limit: 1.0 secondMemory limit: 64 MB A tribe of leafmen live in the old for ...

  4. 51nod 1686 二分+离散化

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 1686 第K大区间 基准时间限制:1 秒 空间限制:131072 ...

  5. mac 上 mamp 配置虚拟主机 具体过程 ?

    https://www.zhihu.com/question/32320396 mac 上 mamp 配置虚拟主机 具体过程 ? 按照哪些配置什么的 都搞好了 但是还不好 配置的主机 制定的目录 还是 ...

  6. python在字符串中查找字符

    两类函数: find(),rfind() index(),rindex() 找到了都返回下标. find找不到返回-1,index找不到抛出ValueError. 带r的表示从右向左找. 都可以使用第 ...

  7. Python 2.7_利用xpath语法爬取豆瓣图书top250信息_20170129

    大年初二,忙完家里一些事,顺带有人交流爬取豆瓣图书top250 1.构造urls列表 urls=['https://book.douban.com/top250?start={}'.format(st ...

  8. asp.net 打印控件使用方法

    打印的效果及控制性虽然不是很好,但是也能勉强使用,应付一般的打印还是 可以的了.代码如下所示: 代码 复制代码 代码如下: //调用PrintControl.ExecWB(?,?)实现直接打印和打印预 ...

  9. WebSocket --为什么引入WebSocket协议

    Browser已经支持http协议,为什么还要开发一种新的WebSocket协议呢?我们知道http协议是一种单向的网络协议,在建立连接后,它只允许Browser/UA(UserAgent)向WebS ...

  10. 反射ORM 三层(for sql server/mysql)

    sql server and oracle 不同之处只在于: 1·参数@和: 2·自增和序列 RPROM.cs //(写错愕,应该是RPORM) RPROM.Insert(p1); //需求:DBS中 ...