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. STM32L476应用开发之二:模拟量数据采集

    采集模拟量数据在一台一起中是必不可少的功能.在本次实验中我们要采集的模拟量值主要包括氧气传感器的输出以及压力变送器的输出. 1硬件设计 我们需要采集数据对精度有一定的要求,而STM32L476自带AD ...

  2. Net 4.5 WebSocket 在 Windows 7, Windows 8 and Server 2012上的比较

    .Net 4.5 WebSocket Server Running on Windows 7? Net 4.5 WebSocket Server 可以运行在 Windows 7,但是Net 4.5的 ...

  3. java.lang.NumberFormatException 错误及解决办法

    package com.geelou.test; public class ErrTest { public static void main(String[] args) { String numS ...

  4. Confluence 6 创建站点的导出文件

    希望为你的站点创建一个 XML 导出文件: 进入  > 基本配置(General Configuration) > 备份和恢复(Backup & Restore). 选择 归档到备 ...

  5. Confluence 6 修改默认空间标识图片

    空间标识图片在边栏上的站点目录(Sites Directory)中作为图标进行显示.默认的空间标识图片将会应用到所有的空间中,如果你没有自定义的空间标识被定义的话,请查看 Configure the ...

  6. Java的动手动脑(五)

    日期:2018.11.1 星期四 博客期:021 Part1: 运行代码 class Grandparent { public Grandparent() { System.out.println(& ...

  7. Java 8 中的 Lambda 表达式

    Lambda 表达式是 Java 8 最受欢迎的功能.人们将函数式编程的概念引入了 Java 这门完全面向对象的命令式编程语言. 关于函数式编程是如何运作的,这个话题超出了本文的范围,不过我们会提炼出 ...

  8. list的add()方法与addAll()方法简介

    简单描述:月读别人的代码,发现了一个有意思的东西,list的一个方法,addAll(),然后就去度娘了一下,发现这个还挺有用的. 吐槽一下:为什么自己没发现这个方法呢?因为平时自己写list的时候,基 ...

  9. 【rabbitmq】安装卸载

    安装: 1. 在http://www.rabbitmq.com/install-rpm.html下载对应系统的rpm包  我下载的是rabbitmq-server-3.6.6-1.el6.noarch ...

  10. php 常用字符函数学习

    1.addcslashes 要向字符串中的特定字符添加反斜杠 <?php header('Content-type:text/html;charset=utf8'); $str='are you ...