首页 技术 正文
技术 2022年11月8日
0 收藏 718 点赞 1,304 浏览 3096 个字

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

徐州网络赛G-Trace【线段树】

样例输入复制

3
1 4
4 1
3 3

样例输出复制

10

题目来源

ACM-ICPC 2018 徐州赛区网络预赛

题意:

每次给一个点 与原点画一个矩形 后面的矩形会覆盖前面的矩形

求所有露在外面的矩形的边的长度之和

思路:

线段树 将横线和竖线分开

以横线为例

从后往前 当前这条横线对结果的贡献 = 本身的长度 – 被之前的(也就是后来的矩形)遮掉的长度

后来的矩形的横线这有在这条横线之上才会对这条横线有影响

树存放当前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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905