HDU 1828 & amp; POJ 1177 Picture (line segment tree + scanning line + discretization)
HDU question address: HDU 1828 POJ question address: POJ 1177
This question is the sum of perimeter, and the method I use may be a little troublesome .. It is the line that is located in the horizontal direction first, and then the vertical line. You only need to obtain the overwrite length of each total interval, and then accumulate the absolute value of the difference between the overwrite length of this total interval and the overwrite length of the previous total interval. Because a new perimeter is generated only when the length changes.
Try scanning once again later. This blog is to be updated.
The Code is as follows:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include using namespace std;#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1int sum[50000], c[50000], cnt, a[6000], b[5000], d1[6000], d2[6000], lazy[50000];struct node{ int l, r, h, f;} edge[100000];void add(int l, int r, int h, int f){ edge[cnt].l=l; edge[cnt].r=r; edge[cnt].h=h; edge[cnt++].f=f;}int cmp(node x, node y){ return x.h
=r) { lazy[rt]+=x; PushUp(rt,l,r); return ; } int mid=l+r>>1; if(ll<=mid) update(ll,rr,x,lson); if(rr>mid) update(ll,rr,x,rson); PushUp(rt,l,r);}int erfen(int x, int high){ int low=0, mid; while(low<=high) { mid=low+high>>1; if(c[mid]==x) return mid; else if(c[mid]>x) high=mid-1; else low=mid+1; }}int main(){ int n, i, j, last, ans, tmp, k; while(scanf("%d",&n)!=EOF) { cnt=0; k=0; memset(sum,0,sizeof(sum)); memset(lazy,0,sizeof(lazy)); for(i=0; i