徐州网络赛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 ...
随机推荐
- android jni aotf 错误
在jni中希望将字符串转成浮点型数据,使用了atof函数.出现错误: failed: Cannot load library: soinfo_relocate(linker.cpp:975): can ...
- Javascript 严格模式 strict mode(转)
一.概述 除了正常运行模式,ECMAscript 5添加了第二种运行模式:"严格模式"(strict mode).顾名思义,这种模式使得Javascript在更严格的条件下运行. ...
- Java 的JSON、XML转换方法——目录索引(转)
JSON及XML的Java序列化.反序列化(转换)在WebService.Ajax数据传递中,用得比较多.如:在用ExtJS.jQuery.mootools以及一些WebService时,你可以需要用 ...
- 谷哥的小弟学后台(04)——MySQL(4)
探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 具体解释Android主流框架不可或缺的基石 站在源代码的肩膀上全解Scroller工作机制 Android多分辨率适 ...
- hadoop 安装笔记
http://www.powerxing.com/install-hadoop/ 查询相关链接~!
- 插入数据insert语句中出错:没有与这些操作数匹配的“+”运算符,操作数类型为:const char[ ]+CString
UpdateData(); CString n; n.Format(L"%d", m_n); _bstr_t strCmd_n = "insert into n(材料,折 ...
- IPV6设置
C:\Windows\System32\drivers\etc 目录下修改hosts文件. 网上有更新的ipv6 hosts文件,复制下来~ 别人不断更新的: https://raw.githubus ...
- Java精选笔记_JDBC
JDBC 概述 什么是JDBC JDBC全称是Java数据库连接(Java Database Connectivity),应用程序可通过这套API连接到关系数据库,并使用SQL语句来完成对数据库中数据 ...
- Java精选笔记_面向对象(包、访问控制、内存机制、垃圾回收机制)
包 包的定义与使用 专门用来存放类的,通常功能相同的类存放在相同的包中. 包的声明只能位于Java源文件的第一行 Java语言中的常用包 java.lang:包含Java语言的核心类,如String. ...
- Axis2开发实例
1.下载①axis2-1.7.4-bin.zip.②axis2-1.7.4-war.zip.③axis2-eclipse-service-plugin-1.7.4.zip.④axis2-eclipse ...