任意门:https://nanti.jisuanke.com/t/31459

There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy ) means the wave is a rectangle whose vertexes are ( 00 , 00 ), ( xx , 00 ), ( 00 , yy ), ( xx , yy ). Every time the wave will wash out the trace of former wave in its range and remain its own trace of ( xx , 00 ) -> ( xx , yy ) and ( 00 , yy ) -> ( xx , yy ). Now the toad on the coast wants to know the total length of trace on the coast after n waves. It's guaranteed that a wave will not cover the other completely.

Input

The first line is the number of waves n(n \le 50000)n(n≤50000).

The next nn lines,each contains two numbers xx yy ,( 0 < x0<x , y \le 10000000y≤10000000 ),the ii-th line means the ii-th second there comes a wave of ( xx , yy ), it's guaranteed that when 1 \le i1≤i , j \le nj≤n ,x_i \le x_jxi​≤xj​ and y_i \le y_jyi​≤yj​ don't set up at the same time.

Output

An Integer stands for the answer.

Hint:

As for the sample input, the answer is 3+3+1+1+1+1=103+3+1+1+1+1=10

样例输入复制

3
1 4
4 1
3 3

样例输出复制

10

题意概括:

有 N 个矩形海浪(给出右上角坐标), 后一个海浪会覆盖前一个海浪,求可见的海浪轮廓长度和。

解题思路:

按照从后往前遍历海浪(因为后面的会把前面的海浪覆盖),每次当前海浪的坐标分别减去当前海浪区间的最大值(x轴记录y的最大值, y值记录x的最大值)。

举个上面的栗子:

注意顺序是从后往前扫。

所以第一个是 x3,y3

0~x3 最大的 y 是 0 所以 第一条可见轮廓为 y3 - 0;

0~y3 的最大 x 是 0 所以 第二条可见轮廓为 x3 - 0;

分别更新这两段的最大值(用两个树状数组维护即可, 只更新【0, X】 和 【0,Y】这两个区间!!!)

第二个是 x2, y3

0 ~ x2 最大的 y 是 0 !!!(因为前面只更新到 x3) 所以第三条可见轮廓为 y2 - 0;

0 ~ y2 最大的 x 是 x3  所以第四条可见轮廓为 x2 - x3;

更新区间;

第三个是 x1 y1

0~x1 最大的 y3 是 0 所以 第五条可见轮廓为 y1 - y3;

0~y1 的最大 x3 是  所以 第六条可见轮廓为 x1 - x3;

更新区间;

AC code:

 #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 5e4+; LL res_x, res_y, ans;
LL t[MAXN*];
LL y[MAXN*];
int N; struct date
{
LL x, y;
}wape[MAXN];
LL lowbit(LL x)
{
return x&(-x);
} void add(LL x ,LL val, int no)
{
for(LL i = x; i > ; i-=lowbit(i)){
if(no == ) t[i] = max(t[i], val);
else y[i] = max(y[i], val);
}
} LL query(LL x, int no)
{
LL res = ;
for(LL i = x; i < MAXN*; i+=lowbit(i)){
if(no == ) res = max(res, t[i]);
else res = max(res, y[i]);
}
return res;
} int main()
{
scanf("%d", &N);
for(int i = ; i <= N; i++){
scanf("%lld%lld", &wape[i].x, &wape[i].y);
}
for(int i = N; i > ; i--){
res_y = wape[i].y - query(wape[i].x, );
if(res_y > ) ans+=res_y;
res_x = wape[i].x - query(wape[i].y, );
if(res_x > ) ans+=res_x; add(wape[i].x, wape[i].y, );
add(wape[i].y, wape[i].x, );
}
printf("%lld\n", ans);
return ;
}

ACM-ICPC 2018 徐州赛区网络预赛 G. Trace【树状数组维护区间最大值】的更多相关文章

  1. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)

    ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...

  2. ACM-ICPC 2018 徐州赛区网络预赛 G Trace(思维+set)

    https://nanti.jisuanke.com/t/31459 题意 n个矩阵,不存在包含,矩阵左下角都在(0,0),给右上角坐标,后来的矩阵会覆盖前面的矩阵,求矩阵周长. 分析 set按照x或 ...

  3. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace

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

  4. ACM-ICPC 2018 徐州赛区网络预赛 G Trace(逆向,两颗线段树写法)

    https://nanti.jisuanke.com/t/31459 思路 凡是后面的轨迹对前面的轨迹有影响的,可以尝试从后往前扫 区间修改需要push_down,单点更新所以不需要push_up(用 ...

  5. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (set维护)

    注意题目保证不会有一个矩形完全包括另一个矩形的情况 时间序上从后往前看,一个坐标\((x,y)\)加进来之前,如果已经有\(x_i<x\),则对结果的贡献为\(x-x_i\);若不存在\(x_i ...

  6. ACM-ICPC 2018 徐州赛区网络预赛-G Trace(线段树的应用

    Problem:Portal传送门 Problem:Portal传送门  原题目描述在最下面.  我理解的题意大概是:有n次涨潮和退潮,每次的范围是个x×y的矩形,求n次涨退潮后,潮水痕迹的长度.   ...

  7. 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...

  8. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace-树状数组-区间修改,单点查询

    赛后和队友讨论了一波,感谢无敌的队友给我细心的讲题 先埋坑 #include<iostream> #include<string.h> #include<algorith ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 G题

    题目链接: https://nanti.jisuanke.com/t/31459 具体思路: 先顺序输入,然后回溯,假设已经加入了n个点,那么在加入的同时,首先看一下原先x轴上已经有过的点,找到第一个 ...

随机推荐

  1. oracle12C--DG 状态集

    一,物理备库 01,状态查询与状态详解 select switchover_status from v$database 02,状态转换到备用数据库 alter database commit to ...

  2. 更改CMD默认的初始路径

    一直用CMD开启本地服务,每一次都得切换路径,有点尴尬.记录一下,修改CMD默认路径 1.打开注册表编辑器(WIN+R打开运行.输入regedit,或者直接找到路径,双击打开C:\Windows\re ...

  3. JS之this那些事

    一直以来,对this的讨论都是热门话题.有人说掌握了this就掌握了JavaScript的80%,说法有点夸张,但可见this的重要性.至今记录了很多关于this的零碎笔记,今天就来个小结. 本人看过 ...

  4. 【linux】dpkg 命令使用说明

    dpkg是一个debian包管理工具.能够对包进行安装.卸载.获取信息等操作.用法:    安装(解包并配置):       dpkg -i package_file       dpkg --ins ...

  5. 在List中常用的linq表达式

    为了下面举例方便,先声明一个集合: public List<Model.Resume> GetResumeList() { var list = new List<Model.Res ...

  6. Linux的find命令实例详解和mtime ctime atime

    这次解释一下三个Linux文件显示的三个时间,然后展示一下find命令的各个功能 在linux操作系统中,每个文件都有很多的时间参数,其中有三个比较主要,分别是ctime,atime,mtime mo ...

  7. 【Android】1.0 安卓生猛上手

    1.Android操作系统,由安迪鲁宾发明,原发明用于照相机操作系统,市场反应惨淡,需求不足,后2005年被谷歌收购,用于智能手机操作系统. 2.logo来源:设计师上厕所看到男女厕所区别标志联想创作 ...

  8. .NET开源工作流RoadFlow-表单设计-数据表格

    数据表格即在表单中显示一个table,该table数据可以来自任意自定义的来源: 数据类型:指定表格的数据源类型 1.datatable,即.net中的System.Data.DataTable 2. ...

  9. 【起航计划ObjC 003】印第安老斑鸠ObjC的幻想 ---- ObjC经典问题

    1.Objective-C的类可以多重继承么?可以采用多个协议么? 答:不可以多重继承,可以采用多个协议. 2.#import和#include的区别是什么?#import<> 跟 #im ...

  10. 【起航计划 033】2015 起航计划 Android APIDemo的魔鬼步伐 32 App->Service->Foreground Service Controller service使用,共享service,前台服务,onStartCommand

    Android系统也提供了一种称为“Service”的组件通常在后台运行.Activity 可以用来启动一个Service,Service启动后可以保持在后台一直运行,即使启动它的Activity退出 ...