59 Lazy Cows
the pasture where Bessie is located, littered with N heaps of pasture , where the first heap of Herbage is located in ( Xi, Yi ), the number is a. C11>i a unit.
when Bessie moves from home to a heap of pasture, it can only move along the axis in four directions, north, east, due west, and south.
To calculate the distance between Bessie and the pasture, use the distance between "Manhattan distance"-( x , y) and
| x ? x - | + | y ? y - |. For example the home of Bessie in (0. 5, 0. 3), there is a bunch of pasture in (3, 2), then the distance between them is 4. 2.
Becky was too lazy to walk around, and she wanted to ask you for it to find a best location for home, the number of grasses near this home not exceeding K
the sum of the quantities is the largest. Note that the coordinates of the home may not be integers, or they can be completely coincident with the coordinates of a pasture.
Input Format
? First line: Two integers n and K, 1 ≤ n ≤ 100000, 1 ≤ K ≤ 2000000
? The second line to the nth + 1 line: the first and 1 lines have three integers: ai, Xi and Yi, 1 ≤ A I ≤ 10000, 0 ≤ Xi, Yi ≤
1000000
output Format
A single integer: The sum of the number of grasses representing distances and best positions not exceeding K
Sample Input
4 3
7 8 6
3 0 0
4 6 0
1 4 2
Sample Output
8
explain
Select (3, 0) for home, position in (0, 0), (6, 0) and
(4, 2) The grass is not more than K
Source
the Lazy Cow, Mar
Analysis
The distance from Manhattan, that range, should be a square with a side length and an axis at a 45-degree angle.
This is a bit difficult, difficult to count.
We need to "spin" the graph.
The goal of rotation is to have the side length of the square parallel to the axis. 、
Then, the observation can be obtained, you can put nx=x-y ny=x+y so spin over (in fact, changed the size of the square, but there is no relationship, as long as it can be judged that the distance is not <=k in Manhattan is good)
Then it is very simple to maintain the Y-ordinate with the segment tree, and then the x-axis linear scan.
The code is as follows:
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <queue>7#include <cmath>8 using namespacestd;9 #defineMAXN 2000010Ten One structHP A { - intA,ax,ay; - }TT[MAXN]; the - structnode - { - intL,r,lc,rc,ans; + intlazy; -}t[2*MAXN];intLen; + A intMymin (intXintY) {returnX<y?x:y;} at intMymax (intXintY) {returnX>y?x:y;} - - BOOLCMP (HP x,hp y) {returnx.ax<y.ax;} - intn,k; - - intBuildintLintR) in { - intx=++Len; toT[x].l=l;t[x].r=R; +t[x].ans=0; t[x].lazy=0; - if(l!=R) the { * intMid= (l+r) >>1; $T[x].lc=build (L,mid);Panax NotoginsengT[x].rc=build (mid+1, R); - } the ElseT[x].lc=t[x].rc=0; + returnx; A } the + voidInit () - { $scanf"%d%d",&n,&k); $ intmx=0; - for(intI=1; i<=n;i++) - { thescanf"%d%d%d",&tt[i].a,&tt[i].ax,&Tt[i].ay); - intxx=tt[i].ax;Wuyitt[i].ax=tt[i].ax-tt[i].ay;tt[i].ay=xx+tt[i].ay+1; themx=Mymax (Mx,tt[i].ay); - } WuSort (tt+1, tt+1+n,cmp); -k=k*2; AboutBuild1, MX); $ } - - voidUpdintx) - { A if(t[x].lazy==0)return; +t[x].ans+=T[x].lazy; the intLc=t[x].lc,rc=t[x].rc; - if(t[x].l!=T[X].R) $ { thet[lc].lazy+=T[x].lazy; thet[rc].lazy+=T[x].lazy; the } thet[x].lazy=0; - } in the voidChangeintXintLintRinty) the { About if(t[x].l==l&&t[x].r==R) the { thet[x].lazy+=y; the return; + } - upd (x); the intMid= (T[X].L+T[X].R) >>1;Bayi if(r<=mid) Change (t[x].lc,l,r,y); the Else if(l>mid) Change (t[x].rc,l,r,y); the Else - { - Change (t[x].lc,l,mid,y); theChange (t[x].rc,mid+1, r,y); the } the upd (T[X].LC); upd (t[x].rc); thet[x].ans=Mymax (T[t[x].lc].ans,t[t[x].rc].ans); - } the the voidFfind () the {94 intj=0, ans=0; the for(intI=1; i<=n;i++) the { the while(j<n&&tt[j+1].ax-tt[i].ax<=k)98 { About intNx=mymax (1, tt[j+1].ay-k); -Change1, nx,tt[j+1].ay,tt[j+1].a);101J + +;102 }103Upd1);104Ans=mymax (ans,t[1].ans); theChange1, Mymax (1, tt[i].ay-k), tt[i].ay,-tt[i].a);106 }107printf"%d\n", ans);108 }109 the intMain ()111 { the init ();113 Ffind (); the return 0; the}Bzoj 3476
2016-10-31 11:25:57
"Bzoj 3476" segment tree = = =