# zoj 3540 Adding New Machine(map+离散化)

PS：这题wa了一天多才发现错误，但是居然是个诡异的错误，当然大家也懒得知道这样的错误，为了测处这个错我交了近50次T_T

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define N 1000010
typedef long long LL;
int g[N][4];
struct que
{
int l,r,a,t;
que(int _a, int _l, int _r, int _t):l(_l),r(_r),a(_a),t(_t){}
};
bool cmp(const que &p, const que &q)
{
return p.a<q.a||(p.a==q.a&&p.t>q.t);
}
vector<que> q;
map<int, int> sgt;
map<int, int>::iterator it;
LL solve(int w, int h, int n, int len)
{
q.clear();
REP(i,n)
{
swap(g[i][0],g[i][1]),swap(g[i][2],g[i][3]);
q.push_back(que(g[i][0],g[i][1],g[i][3],1));
q.push_back(que(g[i][2],g[i][1],g[i][3],-1));
}
sort(q.begin(),q.end(),cmp);
sgt.clear();
sgt[0]=0,sgt[h+1]=h+1;
LL ret=0,now=max(h-len+1,0);
int i,l,r,low=0;
for(i=0;i<q.size();)
{
ret+=(q[i].a-1-low)*now;
low=q[i].a;
for(;i<q.size();++i)
if(q[i].a==low&&q[i].t>0)
{
sgt[q[i].l]=q[i].r;
it=sgt.find(q[i].l);
--it;
l=it->second;
++it,++it;
r=it->first;
if(r-l>len)
now+=max(q[i].l-l-len,0)+max(r-q[i].r-len,0)-(r-l-len);
}else break;
ret+=now;
for(;i<q.size();++i)
if(q[i].a==low)
{
it=sgt.find(q[i].l);
--it;
l=it->second;
++it,++it;
r=it->first;
--it;
sgt.erase(it);
if(r-l>len)
now-=max(q[i].l-l-len,0)+max(r-q[i].r-len,0)-(r-l-len);
}else break;
}
ret+=now*(w-low);
return ret;
}
int main()
{
int w,h,n,m;
LL ans;
while(~scanf("%d%d%d%d",&w,&h,&n,&m))
{
REP(i,n)REP(j,4)scanf("%d",&g[i][j]);
ans=solve(h,w,n,m);
if(m!=1)ans+=solve(w,h,n,m);
printf("%lld\n",ans);
}
return 0;
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客