Trace

There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xxx , yyy ) means the wave is a rectangle whose vertexes are ( 000 , 000 ), ( xxx , 000 ), ( 000 , yyy ), ( xxx , yyy ). Every time the wave will wash out the trace of former wave in its range and remain its own trace of ( xxx , 000 ) -> ( xxx , yyy ) and ( 000 , yyy ) -> ( xxx , yyy ). 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≤50000)n(n \le 50000)n(n≤50000).

The next nnn lines,each contains two numbers xxx yyy ,( 0<x0 < x0<x , y≤10000000y \le 10000000y≤10000000 ),the iii-th line means the iii-th second there comes a wave of ( xxx , yyy ), it's guaranteed that when 1≤i1 \le i1≤i , j≤nj \le nj≤n ,xi≤xjx_i \le x_jxi​≤xj​ and yi≤yjy_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=103+3+1+1+1+1=10

样例输入 复制

3
1 4
4 1
3 3

样例输出 复制

10

题目来源

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

比赛的时候想的是手撸一个平衡树排序,然后二分查找这个点左右两边的点,再比较他们的边值,结果数据结构写炸了

赛后看到网上题解才想起set和list 这些stl 太弱了Orz

用set这类的话 就把一个点的边值拆成两个处理,从后往前,(后面的点总是覆盖前面),看看当今的点时候比后面的点边值高出多少

set的二分还是要%一下,毕竟没这么写过

 #include<bits/stdc++.h>
using namespace std; typedef long long ll;
int n;
const int maxn = 5e4+;
const int maxnn = 1e7+; vector<int>vec1,vec2;
ll solve(vector<int>vec)
{
ll ans = ;
int sz = vec.size();
set<int>st;
for(int i=n-;i>=;i--)
{
set<int>::iterator it = st.lower_bound(vec[i]);
if(it == st.begin())ans += vec[i];
else
{
it--;
ans += vec[i] - *it;
}
st.insert(vec[i]);
}
return ans;
} int main()
{
scanf("%d",&n);
int x,y;
for(int i=;i<=n;i++)
{
scanf("%d%d",&x,&y);
vec1.push_back(x);
vec2.push_back(y);
}
printf("%lld\n",solve(vec1)+solve(vec2));
}

除了可以二分之外 还有树状数组的写法,能树状数组肯定可以线段树。这里先补上树状数组的做法。

trex表示是一个记录相应x位置的y的值的前缀和,trey也类似

  那么插入(4,1)的时候 更改trex,trey, y from 0 到 1 所遇到的横向遇到x都是3;x from 0 to 3 和 from 4 to 4 对应y值不同,这样就用到树状数组的区间更新

  把query 出来的上一个位置+1的位置更新加上现在的点和之前的差值 然后在现在位置+1的位置减去  例如:trex(x在各个位置对应的y值) 3+1位置加上差值 (1-3) 4+1位置加上差值的相反数

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e4+;
const int maxx = 1e7+;
int n;
struct Node
{
int x,y;
}node[maxn];
int trex[maxx];
int trey[maxx]; int maxxx = ;
int lowbit(int x)
{
return x &(-x);
}
void add(int x,int val,int *tre)
{
for(int i=x;i<=maxxx;i+=lowbit(i))
{
tre[i] += val;
}
} int query(int x,int *tre)
{
int ans = ;
for(int i=x;i>;i-=lowbit(i))
{
ans += tre[i];
}
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&node[i].x,&node[i].y);
maxxx = max(maxxx,max(node[i].x,node[i].y));
}
ll ans = ;
for(int i=n;i>;i--)
{
int x = node[i].x;
int y = node[i].y;
int lenx = query(y,trey);
int leny = query(x,trex);
ans += x - lenx;
ans += y - leny;
int valy = query(x,trex);
int valx = query(y,trey);
add(valy+,x-valx,trey);
add(y+,valx-x,trey);
add(valx+,y-valy,trex);
add(x+,valy-y,trex);
//printf("%d---%d %d---%d %d----%d\n",x,y,x-valx,y-valy,valx,valy);
}
printf("%lld\n",ans);
}

Trace 2018徐州icpc网络赛 (二分)(树状数组)的更多相关文章

  1. Trace 2018徐州icpc网络赛 思维+二分

    There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy) ...

  2. Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组

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

  3. Features Track 2018徐州icpc网络赛 思维

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

  4. query 2019徐州网络赛(树状数组)

    query \[ Time Limit: 2000 ms \quad Memory Limit: 262144 kB \] 题意 补题才发现比赛的时候读了一个假题意.... 给出长度为 \(n\) 的 ...

  5. 【BZOJ4538】[Hnoi2016]网络 整体二分+树状数组

    [BZOJ4538][Hnoi2016]网络 Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做一条树边.两个服务器进行数据的交互 ...

  6. HDU 5869 Different GCD Subarray Query(2016大连网络赛 B 树状数组+技巧)

    还是想不到,真的觉得难,思路太巧妙 题意:给你一串数和一些区间,对于每个区间求出区间内每段连续值的不同gcd个数(该区间任一点可做起点,此点及之后的点都可做终点) 首先我们可以知道每次添加一个值时gc ...

  7. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace-树状数组-区间修改,单点查询

    赛后和队友讨论了一波,感谢无敌的队友给我细心的讲题 先埋坑 #include<iostream> #include<string.h> #include<algorith ...

  8. ICPC 2018 徐州赛区网络赛

    ACM-ICPC 2018 徐州赛区网络赛  去年博客记录过这场比赛经历:该死的水题  一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进.     D. Easy Math 题意:   ...

  9. 【BZOJ-2527】Meteors 整体二分 + 树状数组

    2527: [Poi2011]Meteors Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 831  Solved: 306[Submit][Stat ...

随机推荐

  1. mysql 中实现多条数据同时更新

    有时间我们需要对一张表进行批量数据的更新.首先我们想的是update 语句. 比如对一张订单表order_info 多条数据更新, update order_inifo set   order_cod ...

  2. 机器学习之SVD分解

    一.SVD奇异值分解的定义 假设是一个的矩阵,如果存在一个分解: 其中为的酉矩阵,为的半正定对角矩阵,为的共轭转置矩阵,且为的酉矩阵.这样的分解称为的奇异值分解,对角线上的元素称为奇异值,称为左奇异矩 ...

  3. ORACLE in与exists语句的区别

    select * from A where id in(select id from B) 以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来.之后,检查A表的id是否与 ...

  4. Swift中 @objc 使用介绍

    在swift 中 如果一个按钮添加点击方法 如果定义为Private  或者 定义为 FilePrivate 那么会在Addtaget方法中找不到私有方法 但是又不想把方法暴露出来,避免外界访问 ,那 ...

  5. cordova AndroidStudio3.0 升级报错问题

    http://blog.csdn.net/z_Xiaozuo/article/details/78962701 ionic3 打包 安卓项目遇到的问题,当时比较冲忙升级了下android studio ...

  6. LeetCode(73):矩阵置零

    Medium! 题目描述: 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   [1,0,1], ...

  7. Linux基础实操五

    实操一:nginx服务 二进制安装nginx包1) 1)#yum clean all 2)#yum install epel-release -y 3)#yum install nginx -y 1) ...

  8. cf869C组合计数问题

    如果在两个区域里连点,两个区域内选的点数一定要相等 即a中选出i个点,必须与b中选出i个点相连 连接种类数为  然后我们再来看,如果ab中有两点相连,其中一点再与c相连会出事吗? 很显然不会对答案产生 ...

  9. Allegro PCB Design GXL (legacy) 将指定的层导出为DXF

    Allegro PCB Design GXL (legacy) version 16.6-2015 1.菜单:Display > Color/Visibility... 2.打开Color Di ...

  10. HDU 1541 STAR(树状数组)

    Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...