[BZOJ4822] [CQOI2017] Old C's Task (Scan line + Tree array)

Source: Internet
Author: User

4822: [Cqoi2017] task of old C time limit:20 Sec Memory limit:512 MB
submit:379 solved:203
[Submit] [Status] [Discuss] Description Old C is a programmer. Old C recently got a job from the boss--write a management system for the cell phone base station in the city. As an experienced programmer, old C easily completes most of the system's functions and gives you one of the features to implement. Since the area of a base station is very small relative to the entire city area, each base station can be considered as a point in the coordinate system, and its position can be represented by coordinates (x, y). In addition, each base station has many properties, such as height, power, and so on. Operators often delimit an area and query the information for all the base stations in the area. Now the function you need to implement is to answer the sum of the power of all the base stations in the region (including the area boundaries) for a given rectangular region. If there are no base stations in the zone, answer 0.    Input the first row of two integers n, m, indicating that there are altogether n base stations and M-Times query. Then there are n rows, each line consisting of integers separated by x_i, y_i, p_i three spaces, representing the coordinates of a base station (X_i, y_i) and power p_i.    There will not be two base stations in the same coordinates. Then there is a total of M lines, each consisting of integers separated by X1_j, Y1_j, X2_j, y2_j four spaces, representing the rectangular area of a query. The rectangle has diagonal coordinates (X1_J, Y1_j) and (X2_j, Y2_j), and 4 sides are parallel to the axis.   2^31≤x_i, Y_i, P_i, X1_j, Y1_j, X2_j, Y2_j < 2^31, X1_j≤x2_j, Y1_j≤y2_j. Output outputs m rows, one integer per line, corresponding to the results of each query. Sample Input 4 2
0 0 1
0 1 2
2 2 4
1 0 8
0 0 1 1
1 1 5 6Sample Output 11
4Hintsource [Submit] [Status] [Discuss]

First-eye discretization + two-dimensional tree array, but obviously space is not open.

Then directly split each rectangle into two edges, offline after the scan line + tree-like array is good.

Feel for the interval problem more commonly used several: Chairman tree, scan line, CDQ division, MO team.

Not a half-day of the order.

1#include <cstdio>2#include <algorithm>3 #defineRep (i,l,r) for (int i=l; i<=r; i++)4 #defineFind (x) lower_bound (b+1,b+tot+1,x)-B5typedefLong Longll;6 using namespacestd;7 8 Const intn=100010;9ll p[n],c[n<<3],ans[n];Ten intn,m,tot,cnt,x[n],y[n],x1[n],y1[n],x2[n],y2[n],b[n<<3]; One structp{intx, y; ll P; }pt[n<<1]; A structq{intX,l,r,pos; }q[n<<1]; - BOOL operator< (P a,p b) {returna.x<b.x;} - BOOL operator< (Q a,q b) {returna.x<b.x;} the  - voidAddintX,ll k) { for(; x<=tot; x+=x&-x) c[x]+=K;} -ll que (intx) {ll res=0; for(; x; x-=x&-x) res+=c[x];returnRes;} -  + intMain () { -Freopen ("task.in","R", stdin); +Freopen ("Task.out","W", stdout); Ascanf"%d%d",&n,&m); atRep (I,1, N) scanf ("%d%d%lld", &x[i],&y[i],&p[i]), b[++tot]=x[i],b[++tot]=Y[i]; -Rep (I,1, M) { -scanf"%d%d%d%d",&x1[i],&y1[i],&x2[i],&y2[i]); -b[++tot]=x1[i]-1; b[++tot]=y1[i]-1; B[++tot]=x2[i]; b[++tot]=Y2[i]; -     } -Sort (b +1, b+tot+1); Tot=unique (b +1, b+tot+1)-b-1; inRep (I,1, N) x[i]=find (X[i]), Y[i]=find (Y[i]), pt[i]=(P) {x[i],y[i],p[i]}; -Rep (I,1, M) { toX1[i]=find (x1[i]-1); Y1[i]=find (y1[i]-1); +X2[i]=find (X2[i]); y2[i]=find (Y2[i]); -Q[++cnt]= (Q) {x1[i],y1[i],y2[i],i}; q[++cnt]=(Q) {x2[i],y1[i],y2[i],i}; the     } *Sort (pt+1, pt+n+1); Sort (q+1, q+cnt+1);intnow=1; $Rep (I,1, CNT) {Panax Notoginseng          while(pt[now].x<=q[i].x && now<=n) Add (PT[NOW].Y,PT[NOW].P), now++; -         if(!ans[q[i].pos]) Ans[q[i].pos]=que (Q[I].R)-que (Q[I].L); the             ElseAns[q[i].pos]=que (Q[I].R)-que (Q[I].L)-Ans[q[i].pos]; +     } ARep (I,1, m) printf ("%lld\n", Ans[i]); the     return 0; +}

[BZOJ4822] [CQOI2017] Old C's Task (Scan line + Tree array)

Related Article

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.