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. swift 实践- 08 -- UISegmentedControl

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  2. 分布式Dubbo快速入门

    目录 Dubbo入门 背景 zookeeper安装 发布Dubbo服务 Dubbo Admin管理 消费Dubbo服务 抽取与依赖版本管理 Dubbo入门 Editor:SimpleWu Dubbo是 ...

  3. java概念基础笔记整理

    1.构造方法没有类型,有类型的不是不叫构造方法. 2.一个类的的成员变量可以是java允许的任何数据类型,一个类可以把某个对象作为自己的一个成员变量,如果用这样的类创建对象,那么该对象中就会其他对象, ...

  4. CentOS7+mysql(yum)

    1.现在centos上默认是没有yum源的,yum安装的是 MariaDB.所以我们需要自己先配置yum源.配置yum源步骤如下: 下载yum源:wget 'https://dev.mysql.com ...

  5. git无法pull仓库refusing to merge unrelated histories (拒绝合并不相关仓库)

    原文地址 https://blog.csdn.net/lindexi_gd/article/details/52554159 本文讲的是把git在最新2.9.2,合并pull两个不同的项目,出现的问题 ...

  6. typeof操作符--undefined与null

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>ty ...

  7. Appium 常用方法总结 (python 版)

    1.app后台运行 driver.background_app(5) 2.锁屏 driver.lock(5) 3.隐藏键盘 driver.hide_keyboard() 4.启动一个app或者在当前a ...

  8. Ajax爬虫必用到的字典转换器

    1.使用情景 在我们Ajax爬虫时需要用到以下这样的数据的时候我们会一个一个地复制粘贴,这样会很麻烦 def dictionary_converter(key_value): '''主要用于爬虫时复制 ...

  9. 牛客寒假算法基础训练集中营4 E题 Applese 涂颜色

    链接:https://ac.nowcoder.com/acm/contest/330/E 来源:牛客网 题目描述 精通程序设计的 Applese 叕写了一个游戏. 在这个游戏中,有一个 n 行 m 列 ...

  10. UTC时间戳转为时间

    /// <summary> /// 将UTC时间转化DateTime时间 /// </summary> /// <returns></returns> ...