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轴上已经有过的点,找到第一个 ...
随机推荐
- pg存储过程和sql语句块
展E宝项目使用的是postgresql数据库,批量发送红包需求,需要采用存储过程来初始化红包记录数据. 创建存储过程语句有固定的架子,如下 CREATE OR REPLACE FUNCTION pub ...
- (转)开发监控Linux 内存 Shell 脚本
原文:http://blog.csdn.net/timchen525/article/details/76474017 题场景: 开发Shell 脚本判断系统剩余内存的大小,如果低于100MB,就邮件 ...
- linux_api之信号
本片索引: 1.引言 2.信号 3.程序启动 4.signal函数 5.系统调用的中断和系统调用的重启(了解) 6.可再入与不可再入函数(了解) 7.kill函数和raise函数 8.alarm函数和 ...
- git pull冲突报错
修改代码后在git pull报错: error: Your local changes to the following files would be overwritten by merge: xx ...
- 【学习笔记】Java实用类:枚举(Enum)
Java API(Java Application Programming Interface)Java应用程序编程接口... 实用类:Java API提供了几个常用包: 1.java.lang:编写 ...
- 成长之路--Docker入门
简介 Docker是一种新兴的虚拟化技术,能够一定程度上的代替传统虚拟机. Docker版本 Docker 划分为CE 和EE.CE 即社区版(免费,支持周期三个月),EE 即企业版,强调安全,付费使 ...
- Bash 终端快捷键
在终端下输大量的命令,有些快捷键还是挺有帮助的. 跳跃 跳到最后 ctrl + e (end) 跳到最前 ctrl + a (abcde....) 向后跳一个单词 alt + 右方向键 向前跳一个单词 ...
- python占位符%s,%d,%r,%f
input接收的所有输入默认都是字符串格式 1.%s代表字符串占位符 conn, client_addr = phone.accept() print(conn) print(client_addr) ...
- Linux在Tomcat下部署JavaWeb项目
一.Linux快速部署War包操作 1.先关闭Tomcat /home/java/tomcat8/bin/shutdown.sh 注意:进入tomcat bin目录下操作 2.进入War包存放目录(可 ...
- setExecuteExistingDelayedTasksAfterShutdownPolicy方法与setContinueExistingPeriodicTasksAfterShutdownPolicy方法的比较
一.setExecuteExistingDelayedTasksAfterShutdownPolicy方法 这个方法大多是与schedule方法和shutdown方法搭配使用的. public voi ...