题目链接:https://nanti.jisuanke.com/t/31459

样例输入

3
1 4
4 1
3 3

样例输出

10

题意:

二维平面上给出 $n$ 个点,每个点坐标 $\left( {x,y} \right)$ 代表一个从原点到 $\left( {x,y} \right)$ 的长方形,

其中标记红线为线段 $\left( {x,0} \right)\left( {x,y} \right)$ 和 $\left( {0,y} \right)\left( {x,y} \right)$,

每次往二维平面上放置长方形会覆盖住之前放置的长方形,求 $n$ 个长方形全部放置完之后,整个图形上剩余的红线的总长度。

题解:

$x$ 方向和 $y$ 方向可以分开来看,分成 $n$ 个平行于 $x$ 轴的线段和 $n$ 个平行于 $y$ 轴的线段,

不妨看 $n$ 个平行于 $x$ 轴的线段(平行 $x$ 轴的和平行 $y$ 轴的计算方式是一样的),

对于第 $i$ 条线段,其后面的第 $i+1$ 到 第$n$ 条线段,若他们的 $y$ 坐标大于等于当前线段,那么就会覆盖掉一部分(乃至全部)的当前线段,

我们只要找出:第 $i+1$ 到 第$n$ 条线段中,满足 $y$ 坐标大于等于当前线段的,最长的那一条($x$ 值最大的那一条),

这条线段的 $x$ 值,决定了当前第 $i$ 条线段被覆盖掉了多少,那么剩下的就是对答案的贡献,

所以我们从 $n$ 开始递减枚举,用线段树维护区间最大值(当然,坐标需要离散化,否则太大了),不断累加每条线段的贡献即可。

时间复杂度 $O\left( {n\log n} \right)$。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=5e4+; int n;
struct Point{
ll x,y;
}p[maxn]; vector<ll> x;
inline int getxid(int val){return lower_bound(x.begin(),x.end(),val)-x.begin();} vector<ll> y;
inline int getyid(int val){return lower_bound(y.begin(),y.end(),val)-y.begin();} /********************************* Segment Tree - st *********************************/
struct Node{
int l,r;
int val;
}node[*maxn];
void pushup(int root)
{
node[root].val=max(node[root*].val,node[root*+].val);
}
void build(int root,int l,int r)
{
if(l>r) return;
node[root].l=l; node[root].r=r;
node[root].val=;
if(l==r) return;
else
{
int mid=l+(r-l)/;
build(root*,l,mid);
build(root*+,mid+,r);
pushup(root);
}
}
void update(int root,int pos,int val)
{
if(node[root].l==node[root].r)
{
node[root].val=max(node[root].val,val);
return;
}
int mid=node[root].l+(node[root].r-node[root].l)/;
if(pos<=mid) update(root*,pos,val);
if(pos>mid) update(root*+,pos,val);
pushup(root);
}
int askmax(int root,int st,int ed)
{
if(st>node[root].r || ed<node[root].l) return -INF;
if(st<=node[root].l && node[root].r<=ed) return node[root].val;
else return max(askmax(root*,st,ed),askmax(root*+,st,ed));
}
/********************************* Segment Tree - ed *********************************/ int main()
{
scanf("%d",&n); x.clear(); x.push_back();
y.clear(); y.push_back();
for(int i=;i<=n;i++)
{
scanf("%lld%lld",&p[i].x,&p[i].y);
x.push_back(p[i].x);
y.push_back(p[i].y);
}
sort(x.begin(),x.end());
x.erase(unique(x.begin(),x.end()),x.end());
sort(y.begin(),y.end());
y.erase(unique(y.begin(),y.end()),y.end()); build(,,y.size());
ll X=;
for(int i=n;i>=;i--)
{
int yid=getyid(p[i].y);
int xid=getxid(p[i].x);
int mx=askmax(,yid,y.size());
if(xid>mx) X+=(ll)(p[i].x-x[mx]);
update(,yid,xid);
} build(,,x.size());
ll Y=;
for(int i=n;i>=;i--)
{
int yid=getyid(p[i].y);
int xid=getxid(p[i].x);
int my=askmax(,xid,x.size());
if(yid>my) Y+=(ll)(p[i].y-y[my]);
update(,xid,yid);
}
cout<<X+Y<<endl;
}

计蒜客 31459 - Trace - [线段树][2018ICPC徐州网络预赛G题]的更多相关文章

  1. 计蒜客 31460 - Ryuji doesn't want to study - [线段树][2018ICPC徐州网络预赛H题]

    题目链接:https://nanti.jisuanke.com/t/31460 Ryuji is not a good student, and he doesn't want to study. B ...

  2. 计蒜客 30996 - Lpl and Energy-saving Lamps - [线段树][2018ICPC南京网络预赛G题]

    题目链接:https://nanti.jisuanke.com/t/30996 During tea-drinking, princess, amongst other things, asked w ...

  3. 计蒜客 Zoning Houses(线段树区间最大次大)

    Given a registry of all houses in your state or province, you would like to know the minimum size of ...

  4. 计蒜客 31453 - Hard to prepare - [递归][2018ICPC徐州网络预赛A题]

    题目链接:https://nanti.jisuanke.com/t/31453 After Incident, a feast is usually held in Hakurei Shrine. T ...

  5. 计蒜客 31451 - Ka Chang - [DFS序+树状数组][2018ICPC沈阳网络预赛J题]

    题目链接:https://nanti.jisuanke.com/t/31451 Given a rooted tree ( the root is node $1$ ) of $N$ nodes. I ...

  6. 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]

    题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...

  7. 计蒜客 31452 - Supreme Number - [简单数学][2018ICPC沈阳网络预赛K题]

    题目链接:https://nanti.jisuanke.com/t/31452 A prime number (or a prime) is a natural number greater than ...

  8. 计蒜客 31001 - Magical Girl Haze - [最短路][2018ICPC南京网络预赛L题]

    题目链接:https://nanti.jisuanke.com/t/31001 题意: 一带权有向图,有 n 个节点编号1~n,m条有向边,现在一人从节点 1 出发,他有最多 k 次机会施展魔法使得某 ...

  9. 计蒜客 30999 - Sum - [找规律+线性筛][2018ICPC南京网络预赛J题]

    题目链接:https://nanti.jisuanke.com/t/30999 样例输入258 样例输出814 题意: squarefree数是指不含有完全平方数( 1 除外)因子的数, 现在一个数字 ...

随机推荐

  1. SQLServer2008/2005 生成数据字典语句

    SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号=a.colorder, 字段名=a.n ...

  2. iOS js与objective-c的交互(转)

    在写 JavaScript 的时候,可以使用一个叫做 window 的对象,像是我们想要从现在的网页跳到另外一个网页的时候,就会去修改 window.location.href 的位置:在我们的 Ob ...

  3. linux nginx svn 更新前端代码

    1.进入项目前端代码目录中 root@TServer:~# cd /home/p/web/gongti/ 2.更新svn上最新的代码版本 root@TServer:/home/p/web/gongti ...

  4. Git Step by Step – (7) Git远程仓库(续)

    上一篇文章介绍了Git远程仓库的一些使用,但是还是有些东西需要补充一下,所以有了这个续篇. .gitignore 前一篇中,我们介绍了Git的patch功能,当我们生成patch之后,"gi ...

  5. Windows系统下运行某些程序时缺少“Msflxgrd.ocx”的解决方法

    出现这样的错误就是系统缺少相应的库文件,我们安装即可. 下载Msflxgrd.ocx,这里提供一个下载网址:https://www.ocxme.com/files/msflxgrd_ocx 64位系统 ...

  6. 配置Django框架为生产环境的注意事项(DEBUG=False)

    问题描述: Django1.10版本中框架中settings.py配置文件 配置文件settings.py配置了下面两项: DEBUG= False ALLOWED_HOSTS = ['*'] #这样 ...

  7. 初步总结javascript中学习DOM之前的知识

    嘿嘿,又到了周末时间,周六其实就是总结这周的学习的,记得周二周三刚开始接触javascript时间,还是不知道怎么学习的,就感觉找不到方向,那时间学习的只是总结了一些简单的定义或者是学习结构,今天就把 ...

  8. python框架---->pymysql的使用

    这里面学习一下python中操作mysql的第三方库pymysql的使用.很多我们以为一辈子都不会忘掉的事情,就在我们念念不忘的日子里.被我们遗忘了. pymysql的简单使用 我们创建一张表来进行下 ...

  9. 【css系列】创建网页加载进度条

    一.最简单或者明显的方式是使用定时器 1.在网页中加入布局覆盖真实网页内容 2.使用定时器确定加载所用时间的长短,其实并不是真正的加载进度实现 <!DOCTYPE html> <ht ...

  10. Python Tkinter Entry(文本框)

    Python学习记录--关于Tkinter Entry(文本框)的选项.方法说明,以及一些示例. 属性(Options) background(bg) borderwidth(bd) cursor e ...