0
点赞
收藏
分享

微信扫一扫

ACM-ICPC 2018 徐州赛区网络预赛 - G Trace - 思维


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 xxyy ,( 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

ACM-ICPC 2018 徐州赛区网络预赛 - G Trace - 思维_#include

 

样例输入复制


3 1 4 4 1 3 3


样例输出复制


10


题目来源

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

 

 

题意:

有一个沙滩,每个波浪都是一个矩形的左下角为(0,0),右上角为(x,y)。每一次波浪都会冲刷掉之前留下来的痕迹,问最后留下来的痕迹是多长。

分析:

逆着找,因为最后一块痕迹是不会被冲刷掉的且可以判断他是否可以冲刷掉上一次波浪的痕迹。

对于当前(x,y) ,假设要计算x的贡献,我们知道后来的波浪且在它y坐标上面的会覆盖掉它,我们现在逆着来,所以只需要找(y,INF)的最大值即可。

计算y值贡献同理。


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long LL;
#define lson i<<1
#define rson i<<1|1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define ll long long
#define N 100005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

ll b[N];
ll ans_sum,ans_max,ans_min;
//注意一定要找一个临时变量记录下ans_***的答案,不然会覆盖
struct node
{
int l,r;
ll maxx;
ll tag;
} ;
struct segTree
{
node tree[N<<2];
void pushdown(int i)//标记下传
{
if(tree[i].tag!=-1)
{
tree[lson].tag = tree[rson].tag = tree[i].tag;

tree[lson].maxx = tree[rson].maxx = tree[i].tag;

tree[i].tag = -1;
}


}

void pushup(int i)
{
tree[i].maxx=max(tree[lson].maxx,tree[rson].maxx);
}
//建立线段树
void build(int l,int r,int i)
{
tree[i].l = l;
tree[i].r = r;
tree[i].tag = -1;
if(l == r)
{
tree[i].maxx =0;

return;
}
int mid = (l+r)>>1;
build(LS);
build(RS);
pushup(i);
}
//tree[l,r]都变为val
void set_data(int l,int r,int i,ll val)
{
if(tree[i].l>=l&&tree[i].r<=r)
{
tree[i].maxx = val;
tree[i].tag = val;
return;
}
pushdown(i); //标记下传
int mid = (tree[i].l+tree[i].r)>>1;
if(l<=mid)
set_data(l,r,lson,val);
if(r>mid)
set_data(l,r,rson,val);
pushup(i);
}
void query(int l,int r,int i)
{
//cout<<l<<" "<<r<<" "<<i<<endl;
if(l <= tree[i].l && tree[i].r <= r)
{
ans_max = max(ans_max,tree[i].maxx);
return ;
}
pushdown(i);
int mid = (tree[i].l+tree[i].r)>>1;
if(l<=mid)
query(l,r,lson);
if(r>mid)
query(l,r,rson);
pushup(i);
}

} T[3];
struct Point
{
LL x,y;
} p[N];
vector<LL> vx,vy;
inline int getIDx(LL x)
{
return lower_bound(vx.begin(), vx.end(), x) - vx.begin() ;
}
inline int getIDy(LL x)
{
return lower_bound(vy.begin(), vy.end(), x) - vy.begin() ;
}


int main()
{

int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%lld%lld",&p[i].x,&p[i].y);
vx.push_back(p[i].x);
vy.push_back(p[i].y);
}
vx.push_back(0);
vy.push_back(0);//保证查询位置从1开始
sort(vx.begin(), vx.end());
vx.erase(unique(vx.begin(), vx.end()), vx.end());

sort(vy.begin(), vy.end());
vy.erase(unique(vy.begin(), vy.end()), vy.end());

T[0].build(1,n,1);
T[1].build(1,n,1);
ll ansx = 0, ansy = 0;

for(int i=n; i>=1; i--)
{
int y=getIDy(p[i].y);
int x=getIDx(p[i].x);


ans_max=0;
T[1].query(y,n,1);
if(ans_max<p[i].x)
{
ansx+=p[i].x-vx[ans_max];
T[1].set_data(y,y,1,x);
}


ans_max=0;
T[0].query(x,n,1);
if(ans_max<p[i].y)
{
ansy+=p[i].y-vy[ans_max];
T[0].set_data(x,x,1,y);
}
}
printf("%lld\n",ansx+ansy);






return 0;
}


举报

相关推荐

0 条评论