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. Oracle11g 体系结构

    一:Oracle11g 体系结构 二:逻辑储存结构 二.1:数据块(data blocks) ----通过 v$parameter数据字典来查询oracle标准数据块的大小. SYS@orcl> ...

  2. Confluence 6 使用 Apache 和 mod_proxy 的基本配置

    在这些示例中,我们使用下面的信息: http://www.example.com/confluence - 你计划使用的 URL http://example:8090/ - Confluence 当 ...

  3. RefineDet算法笔记

    ---恢复内容开始--- 一.创新点 针对two-stage的速度慢以及one-stage精度不足提出的方法,refinedet 包括三个核心部分:使用TCB来转换ARM的特征,送入ODM中进行检测: ...

  4. Max Sum (dp)

    Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. F ...

  5. laravel 路由模型绑定

    我们在使用路由的时候一个很常见的使用场景就是根据资源 ID 查询资源信息: Route::get('task/{id}', function ($id) { $task = \App\Models\T ...

  6. Allegro PCB Design GXL (legacy) 从dxf文件中导入板框

    Allegro PCB Design GXL (legacy) version 16.6-2015 新建brd文件,并设置好相应的参数之后,点击菜单:File > Import > DXF ...

  7. ajax-简单参数方法实现阴影效果

    注: 简单参数 (按照参数的数量和位置传递参数) 使用时按照位置.数量传递 shadow.js函数 //简单参数实现方式/** slices:阴影* opacity:透明度* zIndex:层级* * ...

  8. Appium 九宫格 手势解锁

    分析九宫格定位 整个九宫格是一个 view   self.driver.find_element_by_id("com.elc:id/gesturepwd_create_lockview&q ...

  9. Unnamed namespaces

    Unnamed namespaces The unnamed-namespace-definition is a namespace definition of the form   inline(o ...

  10. linux基础练习题(1)

    Linux命令作业(关卡一) 练习题1 理解操作系统的作用,以及各种操作系统的不同 要求: 简述什么是OS 简述应用程序.硬件.OS的关系 列举出3种常见的操作系统 简述Ubuntu和Linux的关系 ...