Cdoj 1335 learned and "What's up?" (Segment Tree & Scan line)-Xgtao-

Source: Internet
Author: User

learned and "What are you doing?" "

Test Instructions

The title gives a box of N (<=100000), which gives the coordinates x, y (1<=x,y<=1000000000), Z (1<=z<=3) of the lower-left and upper-right corners, to cover just the volume of K-Times, The answer is guaranteed within a long long

Exercises

1. According to the data range first of all think about discretization.

2. to cover the area or volume will think of line tree maintenance scan line, the volume is to use a two-dimensional line tree? This problem is not possible, because 1<=z<=3 so you can take the volume apart as an area to calculate, when 1<=z<= 2 is to seek the area of the front cover K times, when the 2<=z<=3 is to seek the area of the back cover K, is to seek two times area.

3. What about the K-coverage area? The scan line to the size of y from small to large sort, then each scan from the bottom of the sweep, there will be a scan line with the ordinate y of the difference so that the height of the ΔH, how to get the line with the last scan of the length of the line between the K? Tree[k].sum[x in the segment tree) indicates that this interval covers the length of the x-th segment, and the coverage k-th area between the previous scan line is δh*tree[1].sum[k].

Code

#include <cstdio> #include <iostream> #include <cstring> #include <algorithm>using namespace std;const int N = 1000010;struct tree{int l,r,cover,sum[20];} tree[n<<3]; #define CLR (A, B) memset (A,b,sizeof (a)) int hase[n],cnt,lucnt,lpcnt,n,k,pre;struct Line{int lx,rx,y, Flag;bool operator < (const line& RHS) Const{return y < Rhs.y;} void push (int lx,int rx,int y,int flag) {THIS-&GT;LX = Lx,this->rx = Rx,this->y = Y,this->flag = Flag;}} lineu[n<<2],linep[n<<2];void Build (int k,int l,int r) {tree[k].sum[0] = hase[r]-hase[l];for (int i = 1;i <= 15;++i) Tree[k].sum[i] = 0;TREE[K].L = L,TREE[K].R = R,tree[k].cover = 0;if (l+1 = = r) Return;int mid = (l+r) >>1;build ( K&LT;&LT;1,L,MID); build (k<<1|1,mid,r);} void Processup (int k) {int L = Tree[k].l,r = tree[k].r;if (l+1 = = r) {clr (tree[k].sum,0); Tree[k].sum[min (k+1,tree[k].cover )] = Hase[r]-hase[l];} ELSE{CLR (tree[k].sum,0); for (int i = 0;i <= k+1;++i) tree[k].sum[min (k+1,i+tree[k].cover)] = TreE[k<<1].sum[i]+tree[k<<1|1].sum[i];}} void update (int k,int l,int r,int flag) {if (L = = r) return;if (TREE[K].L >= l && tree[k].r <= r) {Tree[k].cover + = Flag;} Else{int mid = (TREE[K].L+TREE[K].R) >>1;if (r <= Mid) update (K<<1,l,r,flag), if (L > Mid) Update (k< <1|1,l,r,flag);} Processup (k);} int main () {scanf ("%d%d", &n,&k), for (int i = 1;i <= n;++i) {int x1,x2,y1,y2,z1,z2;scanf ("%d%d%d%d%d%d",& X1,&AMP;Y1,&AMP;Z1,&AMP;X2,&AMP;Y2,&AMP;Z2) hase[++cnt] = x1,hase[++cnt] = x2;if (z1 = = 1 && z2 = 2) {lineu[++ Lucnt].push (x1,x2,y1,1); Lineu[++lucnt].push (x1,x2,y2,-1);} if (z1 = = 1 && z2 = = 3) {Lineu[++lucnt].push (x1,x2,y1,1); Lineu[++lucnt].push (x1,x2,y2,-1); Linep[++lpcnt].push ( x1,x2,y1,1); Linep[++lpcnt].push (x1,x2,y2,-1);} if (z1 = = 2 && z2 = = 3) {Linep[++lpcnt].push (x1,x2,y1,1); Linep[++lpcnt].push (x1,x2,y2,-1);}} Sort (hase+1,hase+1+cnt), cnt = unique (hase+1,hase+1+cnt)-hase-1;sort (linep+1,linep+1+lpcnt), sort (lineu+1,lineu+1+ LUCNT); foR (int i = 1;i <= lucnt;++i) {lineu[i].lx = Lower_bound (hase+1,hase+cnt+1,lineu[i].lx)-hase;lineu[i].rx = Lower_bound ( HASE+1,HASE+CNT+1,LINEU[I].RX)-hase;} for (int i = 1;i <= lpcnt;++i) {linep[i].lx = Lower_bound (hase+1,hase+cnt+1,linep[i].lx)-hase;linep[i].rx = Lower_ Bound (Hase+1,hase+cnt+1,linep[i].rx)-hase;} Long Long res = 0;build (1,0,cnt);p re = lineu[1].y;for (int i = 1;i <= lucnt;++i) {res + = 1ll* (lineu[i].y-pre) *tree[1].sum [K];update (1,lineu[i].lx,lineu[i].rx,lineu[i].flag), pre = LINEU[I].Y;} Build (1,0,cnt);p re = linep[1].y;for (int i = 1;i <= lpcnt;++i) {res + = 1ll* (linep[i].y-pre) *tree[1].sum[k];update (1, Linep[i].lx,linep[i].rx,linep[i].flag), pre = LINEP[I].Y;} Cout<<res<<endl;/**/return 0;}

  

Cdoj 1335 learned and "What's up?" (Segment Tree & Scan line)-Xgtao-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.