线段树,假设求(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. vs 2005/2008/2010 ATL ActiveX控件显示XP风格

    vs 2005/2008/2010 ATL ActiveX控件在IE浏览器中控件显示的是原始的风格,要显示XP风格只要进行以下操作 一.在Stdafx.h中增加下以内容 #if defined _M_ ...

  2. 条款50:使用自定义的new以及delete的时机会

    几种最常见的这么做的理由:     1.用来检测运行上的错误:可以在分配的内存空间的起始以及结束分别放置单独的签名     2.为了强化性能     3.为了收集使用上的统计数据 按照第一点就可以举一 ...

  3. Mac 系统安装redis服务

    1.首先去http://www.redis.io/下载最新的redis文件,现在最新的是redis-2.8.19 2.进行解压缩 tar -zxvf redis-2.8.19.tar.gz 3.移动重 ...

  4. json与DataTable相互转换

    首先我们看看 Newtonsoft.Json.JsonConvert 是怎么完成的: DataTable table = new DataTable(); table.Columns.Add(&quo ...

  5. 剑指offer--22.反转链表

    时间限制:1秒 空间限制:32768K 热度指数:440624 本题知识点: 链表 题目描述 输入一个链表,反转链表后,输出新链表的表头. /* struct ListNode { int val; ...

  6. Linux-CentOS7 安装VMware Workstation 12

    转自:http://blog.csdn.net/aoshilang2249/article/details/48656107 1.下载VMware 衔接地址 http://www.vmware.com ...

  7. nodejs 安装 cnpm 命令

    npm install -g cnpm --registry=https://registry.npm.taobao.org

  8. hdu 4336 Card Collector —— Min-Max 容斥

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 bzoj 4036 的简单版,Min-Max 容斥即可. 代码如下: #include<cst ...

  9. 解决docker 下来镜像出现 error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net的问题

    http://f2d6cb40.m.daocloud.io [root@node2 ~]# docker --version                                       ...

  10. hihoCoder#1139(二分+bfs)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回和上上回里我们知道Nettle在玩<艦これ>,Nettle在整理好舰队之后终于准备出海捞船和敌军交战了 ...