[BZOJ1171] [BZOJ2892] The big SZ game

Source: Internet
Author: User

[BZOJ1171] [BZOJ2892] The big SZ game

Question Description

Big Sz is playing a game adapted from Star Wars recently. The Jedi have a total control of n planets. But the Sith are quietly preparing their plans for revenge in the dark. The Jedi Council felt the same thing. The Jedi were then prepared to be deployed to the planets to prevent the Sith from raiding. A planet is under attack and will be notified to the base as soon as possible. The longer it takes, the more the Jedi will need to defend the planet. In order to properly allocate limited samurai, big SZ needs you to help him find out how much time each planet needs to be able to inform the base. For some reason, n planets are lined up in a straight line, numbered 1 to N. The main base was built on planet 1th. Each planet is controlled by a Jedi, but the living creatures are not necessarily the same, and the level of technology is different. The planet I can receive and analyze the signal between [XI, Yi] at a wavelength, and can also signal in this interval, but it cannot emit signals of any other wavelength. For technical reasons, each planet can only signal to a planet less than its own number of miles. In particular, a powerful general base can receive signals of any wavelength. Each planet needs 1 units of time to process the received data, and the transmission time can be negligible.

Input

The first line is two positive integers n, L. Next N-1, total line I contains three positive integers xi, Yi, Li, of which Li represents the first planet from Planet 1th Li, which satisfies Li's strict increment.

Output

A total of N-1 lines, each line of a number of planets 2 to N is at least how much unit time, the base can handle the good data, if unable to reach the base of the output-1.

Input Example 1

3 1 1 2 1 2 3 2

Output Example 1

1 2

Input Example 2

3 3 1 2 1 2 3 2

Output Example 2

1 1

Data size and conventions

30% of data meets N <=20000;
100% of the data meet 2 <=n<= 2.5*10^5, 0<=xi,yi,li<=2*10^9,1<=l<=2*10^9,xi<=yi.

Exercises

First of all, this is a bare dp,f. (i) for the answer required by planet I, f (i) = min{f (j) | [Xi, Yi] and [XJ, YJ] have intersection & L[i]-l[j] <= L & 0 < J < i} + 1.

The tricky thing is that there is no overlap between the two intervals. Can consider the discrete after the use of line segment tree, but to add, delete, query operation of the porcelain section, note that in the above transfer equation, with the growth of I, J is located in the interval is always moving to the right (because L[i]-l[j] <= L), or a sliding window, you can set a monotonous queue.

In addition, the line segment tree tag is not good to merge, so simply do not mark the next pass, only in the segment of the tree node is exactly the operation interval is fully contained, marking on this node, and then up to update the minimum value, ask the node to the root of the tag to take a min. (Note why I want to mark the "tag" red)

#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < Queue> #include <list>using namespace Std;const int buffersize = 1 << 16;char buffer[buffersize], *head, *ta Il;inline Char Getchar () {if (head = = tail) {int L = fread (buffer, 1, buffersize, stdin); tail = (head = buffer) + L;} return *head++;} int read () {int x = 0, F = 1, char c = Getchar (), while (!isdigit (c)) {if (c = = '-') f =-1; c = Getchar ();} while (IsDigit (c)) {x = x * + C-' 0 '; c = Getchar ();} return x * f;} #define MAXN 250005#define oo 2147483646int N, Len, num[maxn<<1], CNT, X[MAXN], Y[MAXN], L[MAXN], F[MAXN], QUE[MAXN] , HD, tl;list <int> q[maxn<<3];int res[maxn<<3];void Update (int L, int R, int o, int ql, int qr, int p, BOOL tp) {int M = L + R >> 1, LC = O << 1, rc = LC | 1;IF (QL <= L && R <= qr) {if (!TP) {while (!q [O].empty () && q[o].front () <= p) Q[o].pop_front (); else {while (! Q[o].empty () && F[q[o].back ()] >= f[p]) q[o].pop_back (); Q[o].push_back (P);} Res[o] = oo;if (! Q[o].empty ()) {if (L < R) Res[o] = min (RES[LC], RES[RC]); Res[o] = min (Res[o], F[q[o].front ()]);} else if (L < R) Res[o] = min (RES[LC], RES[RC]); return;} if (QL <= m) update (L, M, LC, QL, QR, p, TP), if (QR > M) update (m+1, R, RC, QL, QR, p, TP); Res[o] = min (RES[LC], res[r c]); if (! Q[o].empty ()) Res[o] = min (Res[o], F[q[o].front ()]); return;} int query (int L, int R, int o, int ql, int qr) {if (QL <= L && r <= qr) return res[o];int M = L + R >> 1, LC = O << 1, rc = LC | 1, ans = oo;if (ql <= m) ans = min (ans, query (L, M, LC, QL, QR)); if (qr > m) ans = min (ans, query (m+1, R, RC, QL, QR)) ; if (! Q[o].empty ()) ans = min (ans, f[q[o].front ())); return ans; int main () {n = read (); Len = Read (), for (int i = 2; I <= n; i++) {num[++cnt] = x[i] = read (); num[++cnt] = y[i] = read (); L[i] = Read ();} Sort (num + 1, num + cnt + 1), for (int i = 2; I <= n; i++) {X[i] = lower_bound (num + 1,num + cnt + 1, x[i])-num;y[i] = lower_bound (num + 1, num + cnt + 1, y[i])-num; for (int i = 0; i < (maxn<<3); i++) res[i] = Oo;que[++tl] = 1;f[1] = 0; X[1] = 1; Y[1] = cnt; L[1] = 0;update (1, CNT, 1, 1, CNT, 1, 1); for (int i = 2; I <= n; i++) {while (HD < TL && L[que[hd+1]] < l[i ]-Len) {hd++;update (1, CNT, 1, X[QUE[HD]], Y[QUE[HD]], QUE[HD], 0);}  F[i] = query (1, CNT, 1, x[i], y[i]) + 1;if (F[i] < oo) {printf ("%d\n", F[i]), que[++tl] = i;update (1, CNT, 1, x[i], Y[i], I, 1);} Else puts ("1");} return 0;} /*3 31 2 12 3 28 1000002 8192813 1000005 23131346 11321323131346 83131346 12321323131346 83131346 199213231346 103131346 2 13213213 1038 213214854 5432153 214214*/

[BZOJ1171] [BZOJ2892] The big SZ game

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.