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

   第一眼扫描线 想想突然感觉不太对,扫描线不会写

    这题仔细分析一下 这题倒推更加容易写

    这题倒推的话只要更新比这个矩形更大的区域就行了

    离散化一下

    注意两个点

       int ny = query( 1, L[i], tot, 1 );
       int nx = query( 2, R[i], tot, 1 );

    因为他给的是一个矩形所以我们看这条边能不能算就是看这个点的左边的最大值 所以query (L[I]->tot)

    因为你只要左边有一条边 ,这是一个矩形 所以右边也一定有

    第二个点

      update( 1, L[i], sum[R[i] - 1], 1 );
      update( 2, R[i], sum[L[i] - 1], 1 );

    L[i] 表示X轴上的点 他的值对应的是  sum[R[i] - 1]

    

 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("in.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const int mod = 1e9 + ;
const int maxn = 1e6 + ;
struct node {
int l, r, sumx, sumy;
int mid() {
return ( l + r ) >> ;
}
} tree[maxn << ];
void build( int l, int r, int rt ) {
tree[rt].l = l, tree[rt].r = r, tree[rt].sumx = tree[rt].sumy = ;
if ( l == r ) return ;
int m = ( l + r ) >> ;
build( l, m, rtl );
build( m + , r, rtr );
}
void update( int op, int pos, int key, int rt ) {
if ( tree[rt].l == tree[rt].r ) {
if ( op == ) tree[rt].sumx = max( tree[rt].sumx, key );
else tree[rt].sumy = max( tree[rt].sumy, key );
return ;
}
int m = tree[rt].mid();
if ( pos <= m ) update( op, pos, key, rtl );
else update( op, pos, key, rtr );
if ( op == ) tree[rt].sumx = max( tree[rtl].sumx, tree[rtr].sumx );
else tree[rt].sumy = max( tree[rtl].sumy, tree[rtr].sumy );
}
int query( int op, int L, int R, int rt ) {
if ( L <= tree[rt].l && tree[rt].r <= R ) {
if ( op == ) return tree[rt].sumx;
else return tree[rt].sumy;
}
int m = tree[rt].mid();
if ( L > m ) return query( op, L, R, rtr );
else if ( R <= m ) return query( op, L, R, rtl );
else return max( query( op, L, m, rtl ), query( op, m + , R, rtr ) );
}
int L[maxn], R[maxn], n, sum[maxn];
int main() {
sf( n );
int tot = ;
for ( int i = ; i <= n ; i++ ) {
sff( L[i], R[i] );
sum[tot++] = L[i], sum[tot++] = R[i];
}
sort( sum, sum + tot );
tot = unique( sum, sum + tot ) - sum;
build( , tot, );
LL ans = ;
for ( int i = n ; i >= ; i-- ) {
int a = L[i], b = R[i];
L[i] = lower_bound( sum, sum + tot, L[i] ) - sum + ;
R[i] = lower_bound( sum, sum + tot, R[i] ) - sum + ;
int ny = query( , L[i], tot, );
int nx = query( , R[i], tot, );
ans += b - ny;
ans += a - nx;
// fuck(nx),fuck(ny);
update( , L[i], sum[R[i] - ], );
update( , R[i], sum[L[i] - ], );
}
printf( "%lld\n", ans );
return ;
}

G. Trace ACM-ICPC 2018 徐州赛区网络预赛 线段树写法的更多相关文章

  1. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)

    ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...

  2. ACM-ICPC 2018 徐州赛区网络预赛(8/11)

    ACM-ICPC 2018 徐州赛区网络预赛 A.Hard to prepare 枚举第一个选的,接下来的那个不能取前一个的取反 \(DP[i][0]\)表示选和第一个相同的 \(DP[i][1]\) ...

  3. ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer (最大生成树+LCA求节点距离)

    ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer J. Maze Designer After the long vacation, the maze designer ...

  4. 计蒜客 1460.Ryuji doesn't want to study-树状数组 or 线段树 (ACM-ICPC 2018 徐州赛区网络预赛 H)

    H.Ryuji doesn't want to study 27.34% 1000ms 262144K   Ryuji is not a good student, and he doesn't wa ...

  5. ACM-ICPC 2018 徐州赛区网络预赛 B(dp || 博弈(未完成)

    传送门 题面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl n ...

  6. ACM-ICPC 2018 徐州赛区网络预赛 B. BE, GE or NE

    In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named &qu ...

  7. ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study

    262144K   Ryuji is not a good student, and he doesn't want to study. But there are n books he should ...

  8. ACM-ICPC 2018 徐州赛区网络预赛 F. Features Track

    262144K   Morgana is learning computer vision, and he likes cats, too. One day he wants to find the ...

  9. ACM-ICPC 2018 徐州赛区网络预赛 I. Characters with Hash

    Mur loves hash algorithm, and he sometimes encrypt another one's name, and call him with that encryp ...

随机推荐

  1. 关于@media不生效的问题和meta总结

    1:之前做的是两套页面.现在改成响应式布局.发现加上 @media only screen and (max-width: 500px) {    .gridmenu {        width:1 ...

  2. leetcode-回文链表

    请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶:你能否用 O(n) 时间复杂 ...

  3. tomcat下载、安装

    下载 官网地址:https://tomcat.apache.org/download-80.cgi 安装 直接安装即可.安装完毕后Tomcat的目录结构如下: bin:脚本目录 ​ 启动脚本:star ...

  4. 理解Python中的__builtin__和__builtins__

    以Python 2.7为例,__builtin__模块和__builtins__模块的作用在很多情况下是相同的. 但是,在Python 3+中,__builtin__模块被命名为builtins. 所 ...

  5. opencv-学习笔记(3)

    opencv-学习笔记(3) 这章讲了 图像加法 opencv测试效率 IPYTHON测试效率 图像加法 cv2.add() 要求,两图片必须大小类型相同 然后是图像混合cv2.addWeighted ...

  6. truffle框架快速开发合约步骤

    矩阵元区块链智能合约开发指南 1 适用范围 本规范描述了矩阵元区块链系统智能合约的开发约束与规范,用以指导DAPP开发者按照本规范开发基于矩阵元区块链运行的应用. 2 术语解释 术语 术语解释 DAP ...

  7. Linux内核设计笔记10——内核同步

    Linux内核同步笔记 几个基本概念 - 临界区(critical region):访问和操作共享数据的代码段: - 原子操作:操作在执行中不被打断,要么不执行,要么执行完: - 竞争条件: 两个线程 ...

  8. for和foreach的一点总结

    两者都是数组的循环遍历,但是区别还是有点: for循环,如果块内有事件操作,那么i是不会等事件操作时候在依次增加,而是一次性走完,,也就是依靠下标定位,下标已经走完了,而foreah是依次增加,它是依 ...

  9. fragment的介绍与使用

    稍稍摘录一段Fragment.java中的说明文档. /** * A Fragment is a piece of an application's user interface or behavio ...

  10. “Hello world!”团队第二周贡献分规则+贡献分数分配结果

    一.贡献规则制定: (1)基础分:9 , 9 , 8 , 7 , 7 , 7 , 6(按在本次编程中承担模块的重要度制定,某一模块重要度的认定通过组内开会讨论决定) (2)会议分:每人没出勤一次会议记 ...