徐州网络赛G-Trace【线段树】
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
题目来源
题意:
每次给一个点 与原点画一个矩形 后面的矩形会覆盖前面的矩形
求所有露在外面的矩形的边的长度之和
思路:
线段树 将横线和竖线分开
以横线为例
从后往前 当前这条横线对结果的贡献 = 本身的长度 - 被之前的(也就是后来的矩形)遮掉的长度
后来的矩形的横线这有在这条横线之上才会对这条横线有影响
树存放当前y坐标到无穷大之间 横线的最大长度
首先离散化所有的xy坐标
从后往前处理点 查询当前y坐标之上的横线最大长度
结果加上当前横线长度-查询所得
更新y节点的值为当前横线长度
初始化树的节点都是0 做一个更新一个 build都不需要
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<set>
//#include<bits/stdc++.h>
#define inf 0x7f7f7f7f7f7f7f7f
using namespace std;
typedef long long LL;
const int maxn = 50005;
int n;
struct node {
int x, y;
}nodes[maxn];
map<int, int> newx, newy;//离散化xy坐标
set<int>xx, yy;
set<int>::iterator it;
int treex[maxn << 2], treey[maxn << 2];
void pushupx(int rt)
{
treex[rt] = max(treex[rt << 1], treex[rt << 1 | 1]);
}
void pushupy(int rt)
{
treey[rt] = max(treey[rt << 1], treey[rt << 1 | 1]);
}
void updatex(int x, int val, int l, int r, int rt)
{
if (l == r) {
treex[rt] = val;
return;
}
int m = (l + r) / 2;
if (x <= m) {
updatex(x, val, l, m, rt << 1);
}
else {
updatex(x, val, m + 1, r, rt << 1 | 1);
}
pushupx(rt);
}
void updatey(int x, int val, int l, int r, int rt)
{
if (l == r) {
treey[rt] = val;
return;
}
int m = (l + r) / 2;
if (x <= m) {
updatey(x, val, l, m, rt << 1);
}
else {
updatey(x, val, m + 1, r, rt << 1 | 1);
}
pushupy(rt);
}
LL queryx(int L, int R, int l, int r, int rt)
{
if (L <= l && R >= r) {
return treex[rt];
}
int m = (l + r) / 2;
LL ans = 0;
if (L <= m) {
ans = max(ans, queryx(L, R, l, m, rt << 1));
}
if (R > m) {
ans = max(ans, queryx(L, R, m + 1, r, rt << 1 | 1));
}
return ans;
}
LL queryy(int L, int R, int l, int r, int rt)
{
if (L <= l && R >= r) {
return treey[rt];
}
int m = (l + r) / 2;
LL ans = 0;
if (L <= m) {
ans = max(ans, queryy(L, R, l, m, rt << 1));
}
if (R > m) {
ans = max(ans, queryy(L, R, m + 1, r, rt << 1 | 1));
}
return ans;
}
void init()
{
memset(treex, 0, sizeof(treex));
memset(treey, 0, sizeof(treey));
newx.clear();
newy.clear();
xx.clear();
yy.clear();
}
int main()
{
while (scanf("%d", &n) != EOF) {
init();
for (int i = 0; i < n; i++) {
scanf("%d%d", &nodes[i].x, &nodes[i].y);
xx.insert(nodes[i].x);
yy.insert(nodes[i].y);
}
int cntx = 1, cnty = 1;
for (it = xx.begin(); it != xx.end(); it++) {
newx[*it] = cntx++;
}
cntx--;
for (it = yy.begin(); it != yy.end(); it++) {
newy[*it] = cnty++;
}
cnty--;
LL ans = 0;
//buildx(1, cnty, 1);
//buildy(1, cntx, 1);
for (int i = n - 1; i >= 0; i--) {
int nowy = newy[nodes[i].y], nowx = newx[nodes[i].x];
LL resx = queryx(nowy + 1, cnty, 1, cnty, 1), resy = queryy(nowx + 1, cntx, 1, cntx, 1);
ans += nodes[i].x - resx + nodes[i].y - resy;
updatex(nowy, nodes[i].x, 1, cnty, 1);
updatey(nowx, nodes[i].y, 1, cntx, 1);
}
printf("%lld\n", ans);
}
return 0;
}
徐州网络赛G-Trace【线段树】的更多相关文章
- 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 ...
- 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\),远 ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace【树状数组维护区间最大值】
任意门:https://nanti.jisuanke.com/t/31459 There's a beach in the first quadrant. And from time to time, ...
- 2019 徐州网络赛 G Colorful String 回文树
题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...
- 徐州网络赛F-Feature Trace【暴力】
Morgana is learning computer vision, and he likes cats, too. One day he wants to find the cat moveme ...
- hdu 4031 2011成都赛区网络赛A题 线段树 ***
就是不知道时间该怎么处理,想了好久,看了别人的题解发现原来是暴力,暴力也很巧妙啊,想不出来的那种 -_-! #include<cstdio> #include<iostream&g ...
- 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)
query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)
ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...
随机推荐
- 【MySQL】MySQL 常用语法之锁表与解锁表
mysql 锁表语句: Lock锁整张表: 写锁定: LOCK TABLES products WRITE: 写锁,锁定之后,只有当前线程可以进行读操作和写操作,其他线程读操作和写操作均被堵塞.... ...
- 利用Spring AOP和自定义注解实现日志功能
Spring AOP的主要功能相信大家都知道,日志记录.权限校验等等. 用法就是定义一个切入点(Pointcut),定义一个通知(Advice),然后设置通知在该切入点上执行的方式(前置.后置.环绕等 ...
- -lpthread和-pthread的区别
编译程序包括 预编译, 编译,汇编,链接,包含头文件了,仅能说明有了线程函数的声明, 但是还没有实现, 加上-lpthread是在链接阶段,链接这个库.<stdio.h>等都是静态库,不需 ...
- 截取scrollview的滑动事件,传递给子控件
重写一个ScrollView public class MyScrollView extends ScrollView{ public MyScrollView(Context context, At ...
- console.log()注意事项。
console.log常因不明原因在IE9出现SCRIPT5009: 'console' is undefined (console未被定义) 错误! IE9说console变量未定义? 但F12打开 ...
- chrome 版本升级到56,报错Your connection is not private NET::ERR_CERT_WEAK_SIGNATURE_ALGORITHM
ubuntu14.04 解决方法: $ sudo apt-get install libnss3-1d ref: http://stackoverflow.com/questions/42085055 ...
- JavaScript 事件参考手册
事件通常与函数配合使用,这样就可以通过发生的事件来驱动函数执行. 事件句柄 HTML 4.0 的新特性之一是有能力使 HTML 事件触发浏览器中的动作(action),比如当用户点击某个 HTML 元 ...
- 如何使用ChemDraw改变说明文本
作为一款全球领先的化学绘图工具,ChemDraw能够绘制各种复杂的结构方程式.ChemDraw软件还增加了新的绘图工具,能够方便化学领域的图形绘制.本教程将向大家讲解如何在ChemDraw中改变说明文 ...
- VC++ : GetIDsOfNames 调用失败,获取的dispid为-1
今天调试自己的程序,需要调用一个COM组件中的方法,利用GetIDsOfNames获取调用函数对象的DISPID. GetIDsOfNames: 把COM接口的方法名字和参数(可选)映射成一组DISP ...
- WebService远程调用(代码调用)
在做多个系统集成的时候,由于各系统厂商采用不同的架构,在项目实施前期,各业务对业务理解不够深入,系统接口可能会有较多变化, 在此背景下,动态调用webserivce就变得灵活了,降低了系统集成的耦合度 ...