ACM-ICPC 2018 徐州赛区网络预赛 G. Trace【树状数组维护区间最大值】
任意门: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【树状数组维护区间最大值】的更多相关文章
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)
ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...
- ACM-ICPC 2018 徐州赛区网络预赛 G Trace(思维+set)
https://nanti.jisuanke.com/t/31459 题意 n个矩阵,不存在包含,矩阵左下角都在(0,0),给右上角坐标,后来的矩阵会覆盖前面的矩阵,求矩阵周长. 分析 set按照x或 ...
- 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 ...
- ACM-ICPC 2018 徐州赛区网络预赛 G Trace(逆向,两颗线段树写法)
https://nanti.jisuanke.com/t/31459 思路 凡是后面的轨迹对前面的轨迹有影响的,可以尝试从后往前扫 区间修改需要push_down,单点更新所以不需要push_up(用 ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (set维护)
注意题目保证不会有一个矩形完全包括另一个矩形的情况 时间序上从后往前看,一个坐标\((x,y)\)加进来之前,如果已经有\(x_i<x\),则对结果的贡献为\(x-x_i\);若不存在\(x_i ...
- ACM-ICPC 2018 徐州赛区网络预赛-G Trace(线段树的应用
Problem:Portal传送门 Problem:Portal传送门 原题目描述在最下面. 我理解的题意大概是:有n次涨潮和退潮,每次的范围是个x×y的矩形,求n次涨退潮后,潮水痕迹的长度. ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ's Salesman 【离散化+树状数组维护区间最大值】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/O ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace-树状数组-区间修改,单点查询
赛后和队友讨论了一波,感谢无敌的队友给我细心的讲题 先埋坑 #include<iostream> #include<string.h> #include<algorith ...
- ACM-ICPC 2018 徐州赛区网络预赛 G题
题目链接: https://nanti.jisuanke.com/t/31459 具体思路: 先顺序输入,然后回溯,假设已经加入了n个点,那么在加入的同时,首先看一下原先x轴上已经有过的点,找到第一个 ...
随机推荐
- Java - 打印质数(使用控制嵌套循环跳转)
使用控制嵌套循环跳转,打印输出10 ~ 150之间的质数 代码: public class Testcotinue { public static void main(String[] args) { ...
- Android 中判断网络状态
首先在AndroidManifest.xml添加权限 <uses-permission android:name="android.permission.ACCESS_NETWORK_ ...
- How to Create a Cron Job (Scheduled Task) for Your Website or Blog
How to Create a Cron Job (Scheduled Task) for Your Website or Blog by Christopher Heng, thesitewizar ...
- angular的基本要点
<body ng-app="Myapp"> <div ng-controller="firstcon"> <h1>hello ...
- JVM 类加载全过程
类加载机制 - JVM把class文件加载到内存中 并对数据进行 校验,解析,初始化,最终形成JVM可以直接使用的java类型的过程 详细过程 加载→ 验证→ 准备→ 解析 → 初始化→ 使用 → ...
- VS 连接数据库报错:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误
VS报错:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider ...
- PHP通过映射类查找类所在文件
$reflectObj = new ReflectionClass(类名); $file_name = $reflectObj->getFileName(); var_dump($file_na ...
- maven module
通过将一个maven项目拆分成多个module,会引入一定的项目复杂度,但随着后期项目代码的逐渐增多,最直观的感受是,每次build代码,不必build整个项目,可节省很多时间. 如果各个module ...
- .NET开源工作流RoadFlow-表单设计-保存与发布
表单的过程中可以随时保存,以便下次继续设计. 表单设计完成后即可点击发布按钮,发布表单,发布表单后即可在流程设计时选定该表单.
- 跨平台移动开发_PhoneGap 再次点击返回键切换到桌面效果
PhoneGap 再次点击返回键切换到桌面效果 相关代码 <!DOCTYPE html> <html> <head> <title> PhoneGap ...