2018网络预选赛 徐州G 线段树
线段树,假设求(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 线段树的更多相关文章
- 2018网络预选赛 徐州H 线段树+树状数组
设读入的数组是a,树状数组用来维护a数组区间和sum,线段树用来维护一个另一个数组ssum的区间和,区间每个点a[i]*(n-i+1),那么l-r的答案是l-r的ssum-(n-r)*(sum[r]- ...
- 徐州网络赛G-Trace【线段树】
There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy ...
- 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 ...
- 2018.07.22 codeforces750E(线段树维护状态转移)
传送门 给出一个数字字串,给出若干个询问,询问在字串的一段区间保证出现2017" role="presentation" style="position: re ...
- HDU 6356 Glad You Came 2018 Multi-University Training Contest 5 (线段树)
题目中没有明说会爆int和longlong 的精度,但是在RNG函数中不用unsigned int 会报精度,导致队友debug了很久... 根据每次生成的l,r,v对区间更新m次,然后求 i*ai的 ...
- 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 ...
- 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/ ...
- HDU 4417 Super Mario(2012杭州网络赛 H 离线线段树)
突然想到的节约时间的方法,感觉6翻了 给你n个数字,接着m个询问.每次问你一段区间内不大于某个数字(不一定是给你的数字)的个数 直接线段树没法做,因为每次给你的数字不一样,父节点无法统计.但是离线一 ...
- 2019ccpc网络赛hdu6703 array(线段树)
array 题目传送门 解题思路 操作1是把第pos个位置上的数加上\(10^7\),操作2是找到区间[1,r]中没有且大于k的最小的数.注意到k的范围是小于等于n的,且n的范围是\(10^5\),远 ...
随机推荐
- hdu 5241 Friends(找规律?)
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...
- URL格式和urlencode
URL的格式:protocol :// hostname[:port] / path / [;parameters][?query]#fragment URL出现了有+,空格,/,?,%,#,& ...
- SCM-MANAGER 应用
什么是SCM-MANAGER 基于Web的,集成了 Git. Mercurial .Subversion 多种代码管理工具的源代码管理平台 它有什么优点 简易安装 不需要破解配置文件,完全可配置的 ...
- windows下安装 redis并开机自启动
1,redis官方下载地址:https://redis.io/download,redis 64位下载地址:https://github.com/ServiceStack/redis-windows, ...
- C# 高效过滤DataTable 中重复数据方法
使用DataView,然后设置ToTable,设置几个字段和一个布尔值,表示这些字段作为一个整体,在这个表内不允许重复,示例代码: namespace A { class Program { stat ...
- 要做Java程序员 需要知道那些技术 重点有那些
一.Java程序员 高级特性 反射.泛型.注释符.自动装箱和拆箱.枚举类.可变 参数.可变返回类型.增强循环.静态导入 ...
- codewar代码练习2——7级晋升6级
7级晋升到6级的过程中以做6级题以及以前未完成的题目为主,一般选择算法题或者基础题.相比之前从8级升级7级(参见此博客:http://blog.csdn.net/m0_37324740/article ...
- 3.4 常用的两种 layer 层 3.7 字体与文本
3.4 常用的两种 layer 层 //在cocos2d-x中,经常使用到的两种 layer 层 : CCLayer 和 CCLayerColor //CCLayer 的创建 CCLayer* la ...
- HDFS超租约异常总结(org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException)
HDFS超租约异常总结(org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException) 转载 2014年02月22日 14:40:58 96 ...
- LeetCode第二题:Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...