徐州网络赛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 ...
随机推荐
- ubuntu/centos网络配置
UBUNTU网络配置 配置临时的Ip ifconfig eth0 其中24指的网络掩码24位. vim /etc/network/interfaces 添加下面内容 auto eth0 #开机自动连接 ...
- MySQL创建用户、授权、撤销权限、删除用户
一.创建用户 mysql -u root -p 1.mysql->create user 'test'@'localhost' identified by '123'; 2.mysql-> ...
- SSH远程登录其他机器
常用格式:ssh [-l login_name] [-p port] [user@]hostname更详细的可以用ssh -h查看. 不指定用户: ssh 192.168.0.11 指定用户: ssh ...
- react新手入门(序)
之前在软件园使用的是react,当时为了做个集光推送,自己去搭过react,这次项目中继续使用react,于是又重新操作了遍,恰巧公司买了本react的书籍,这本书写的非常好,看着并不觉得拗口,很容易 ...
- 使用Visual Studio将C#生成DLL文件的方法
1.命令方式 打开Visual Studio安装目录下的开发人员命令提示 译 File.cs 以产生 File.exe csc File.cs 编译 File.cs 以产生 File.dll csc ...
- MathType手写输入面板清空的方法
我们在文档中进行公式编辑的时候一般都会选用MathType数学公式编辑器来完成这个编辑过程,利用MathType里面的各种模板与符号可以编辑出很多我们需要的公式.最新版的word公式编辑器MathTy ...
- 我的javascript心跳机
li { list-style: none!important; padding:0; } .list_num{ list-style-type:decimal; } .list_inline{ ma ...
- Css中position、float和clear整理
Position: absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位. 元素的位置通过 "left", "top", ...
- IOS -执行时 (消息传递 )
一 函数调用概述 Objective-C不支持多重继承(同Java和Smalltalk),而C++语言支持多重继承. Objective-C是动态绑定,它的类库比C++要easy操作. Ob ...
- CreateEvent和SetEvent及WaitForSingleObject的使用方法
CreateEvent: 1.函数功能: 创建一个命名或匿名的事件对象 2.函数原型: HANDLE CreateEvent( LPSECURITY_ATTRIBUTES lpEventAttri ...