After discretization, [1,10]=[1,3]+[6,10] lost [4,5] this paragraph.
Because [3,6] is updated, it is only updated to [3,3],[6,6].
Either add a value between two points with a difference greater than 1, or let the segment tree nodes with left and right endpoints l,r to the x[l] to x[r+1] interval.
So Tree[l,r]=tree[l,m]+tree[m+1,r]=[x[l],x[m+1]]+[x[m+1],x[r+1]]
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #define LL Long Long#define N 2005using namespace std;struct p{ll s,e,h,f;} P[n];struct tree{ll Sum,c;} T[n<<5];int x[n],n,m,ans;int cmp (const p &A,CONST p &b) {return a.h<b.h;} void Init () {n=m=ans=0; memset (p,0,sizeof p); memset (T,0,sizeof t); X[0]=-1;} void Pushup (ll rt,ll l,ll R) {if (t[rt].c) t[rt].sum=x[r+1]-x[l]; else if (l==r) t[rt].sum=0; else T[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum;} void Update (LL s,ll e,ll rt,ll l,ll r,ll v) {if (s<=l&&r<=e) t[rt].c+=v; else {if (l>e| | R<s) return; ll m=l+r>>1; Update (S,E,RT<<1,L,M,V); Update (S,E,RT<<1|1,M+1,R,V); } pushup (Rt,l,r);} int main () {int a,b,c,d; Init (); while (cin>>a>>b>>c>>d) {if (a<0) break; while (a>=0) {p[++n].s=a,p[n].e=c,p[n].h=b,p[n].f=1;x[n]=a; P[++n].s=a,p[n].e=c,p[n].h=d,p[n].f=-1;x[n]=c; cin>>a>>b>>c>>d; } sort (x+1,x+1+n); for (int i=1;i<=n;i++) if (x[i]!=x[i-1]) x[++m]=x[i]; Sort (p+1,p+1+n,cmp); for (int i=1;i<n;i++) {int L=lower_bound (X,X+M,P[I].S)-X; int R=lower_bound (X,X+M,P[I].E)-x-1; Update (L,R,1,1,M,P[I].F); ans+=t[1].sum* (p[i+1].h-p[i].h); } cout<<ans<<endl; Init (); } return 0;}
"POJ 1389" area of simple polygons (segment tree + scan line, rectangular and square)