线段树,假设求(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. Dom节点操作常用方法

    1.访问/获取节点 document.getElementById(id); //返回对拥有指定id的第一个对象进行访问 document.getElementsByName(name); //返回带 ...

  2. hdu 1847 Good Luck in CET-4 Everybody!(sg)

    Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. java重置Timer执行频率

    public class BallUtil { public static Timer fisTimer ; public static void fisStartBall(){ long first ...

  4. Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)

    数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...

  5. VueJs路由跳转——vue-router的使用

    对于单页应用,官方提供了vue-router进行路由跳转的处理,本篇主要也是基于其官方文档写作而成. 安装 基于传统,我更喜欢采用npm包的形式进行安装. npm install vue-router ...

  6. 【整理】2-SAT

    2-satisfiability,我们一般将其缩写为 2-sat. 了解全名有助于我们对这个算法的理解.     百度翻译:‘satisfiability’---“可满足性,适定性”. “合取范式可满 ...

  7. Phone numbers

    Phone number in Berland is a sequence of n digits. Often, to make it easier to memorize the number, ...

  8. FastAdmin 推荐 Git 在线学习教程

    FastAdmin 推荐 Git 在线学习教程 因为 FastAdmin 推荐使用 Git 管理代码,有很多小伙伴对 Git 不是很熟悉. 也苦于找不到好的教程,我就分享一个 Git 在线学习教程. ...

  9. bzoj 4503 两个串——FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4503 翻转T,就变成卷积.要想想怎么判断. 因为卷积是乘积求和,又想到相等的话相减为0,所以 ...

  10. POJ1456:Supermarket(并查集版)

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:http://poj.org/problem?id=1456 堆作法:https:/ ...