hdu1828
题意 就是线段树扫描线 但是 poj 1177 那个题的数据也太弱了吧 所以重新写了一发hdoj的
感觉自己的线段树好丑
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
const int N = 20025;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
namespace sgt
{
#define mid ((l+r)>>1)
int cnt[N<<2],s[N<<2];
void up(int rt,int l,int r)
{
if(cnt[rt]) s[rt] = r - l + 1;
else s[rt] = s[rt<<1] + s[rt<<1|1];
}
void build(int rt,int l,int r)
{
s[rt] = cnt[rt] = 0;
if(l==r)
{
return ;
}
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
}
void update(int rt,int l,int r,int x,int y,int v)
{
if(x<=l&&r<=y)
{
cnt[rt]+=v;
up(rt,l,r);
return;
}
if(x>mid) update(rt<<1|1,mid+1,r,x,y,v);
else if(y<=mid) update(rt<<1,l,mid,x,y,v);
else update(rt<<1,l,mid,x,y,v),update(rt<<1|1,mid+1,r,x,y,v);
up(rt,l,r);
}
#undef mid
}
const int MAX_N = 5025;
struct Node
{
int l,r,z,id;
bool operator < (const Node other) const
{
if(z==other.z) return id <other.id;
return z < other.z;
}
Node(){}
Node(int _l,int _r,int _z,int _id):
l(_l),r(_r),z(_z),id(_id){}
}arr[MAX_N*2],brr[MAX_N*2];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,a,b,c,d,cnt = 0;
while(scanf("%d",&n)==1)
{
ll ans = 0;
cnt = 0;
for(int i = 1;i<=n;++i)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
a+=10001,b+=10001,c+=10001,d+=10001;
if(a>c) swap(a,c);
if(b>d) swap(b,d);
arr[++cnt] = Node(a,c,d,1);
brr[cnt] = Node(b,d,a,0);
arr[++cnt] = Node(a,c,b,0);
brr[cnt] = Node(b,d,c,1);
}
int last = 0;
sort(arr+1,arr+1+cnt);
sort(brr+1,brr+1+cnt);
brr[cnt+1] = arr[cnt+1] = Node(1,1,100000,1);
sgt::build(1,1,20025);
for(int i = 1;i<=cnt;++i)
{
if(arr[i].id==0)
{
sgt::update(1,1,N,arr[i].l,arr[i].r-1,1);
ans += abs(sgt::s[1]-last);
last = sgt::s[1]; }
}
else
{
sgt::update(1,1,N,arr[i].l,arr[i].r-1,-1);
ans += abs(last - sgt::s[1]);
last = sgt::s[1];
}
}
last = 0;
sgt::build(1,1,20025);
for(int i = 1;i<=cnt;++i)
{
if(brr[i].id==0)
{
sgt::update(1,1,N,brr[i].l,brr[i].r-1,1);
ans += abs(sgt::s[1]-last);
last = sgt::s[1];
}
else
{
sgt::update(1,1,N,brr[i].l,brr[i].r-1,-1);
ans += abs(last - sgt::s[1]);
last = sgt::s[1];
}
}
printf("%lld\n",ans);
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}