"bzoj-2892&1171" Strike Battle & Big SZ Game weight segment tree + monotone queue + tag permanent +DP

Source: Internet
Author: User

2892: Assault combat time limit:50 Sec Memory limit:512 MB
submit:45 solved:30
[Submit] [Status] [Discuss] Description in a world without winter horses, after the school festival, after the spring hope to see the beloved snow dishes. However, in line to see the snow dish fans too much, spring 1:30 will not make up to the snow vegetables before. As a Gao, how can such questions be stumped by spring? Chunch from Wu also took from the gold glittering treasure trove of multi-a dream of the microphone, and to each queue fans have sent a transmitter. So now there are n people lined up in a row, the first person is a snow dish, the back n person is a snow dish fans. The I fan can select a person not exceeding l on his left to emit a frequency at [Xi,yi], and the I fan can also accept the frequency in [Xi,yi] acoustic waves. In particular, the snow dish can accept sound waves of all frequencies. Spring, some words want to pass the microphone to tell the snow dish, he wants to choose a person as the starting point of the message, but do not know who choose better, as a friend of Chun-hee, you can tell him the answer? A specific explanation of the message: I can select a person whose left distance is not greater than L, as long as the corresponding intervals between the two sides have the intersection, then the first person can communicate the information to J. Then J can continue to find the next person to convey the message. It takes 1 units of time to convey a message. Input first line two positive integers n, L. Next N-1 line, line I contains three positive integer XI ,Yi ,Li, which Li said that the first person from the snow dish has Li distance, meet Li strict increment. Output Total N -1 lines, each line of a number for the 2 to N fan at least how many units of time, the snow dish to receive information, if it is not possible to upload to the snow dish output-1. Sample Input3 1
1 2 1
2 3 2
Sample Output1
2
"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. Hintsource 1171: The big SZ game time limit:50 Sec Memory limit:357 MB
submit:320 solved:98
[Submit] [Status] [Discuss] 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.

Sample InputINPUT1
3 1
1 2 1
2 3 2
Input 2
3 3
1 2 1
2 3 2Sample OutputOUTPUT1
1
2
Output2
1
1
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.
Hintsource

by Hu Hua Cheng

Solution

Segment Tree marker persistence + monotone queue +DP

First glance DP: $DP [I]=min (Dp[j]) +1$ where $\left | L[I]-L[J] \right |<l,\left [x[i],y[i] \right]\bigcap \left [x[j],y[j] \right]\neq \o $

Then you need to optimize the complexity, consider the use of data structures (can be used in many ways, here to choose the weight segment tree + monotone queue)

X[],y[] range is too large, single very sparse, discrete, built weight segment tree, support interval to modify the interval query; The answer in the maintenance interval requires a monotone queue to be added to the interval

The discovery tag is not suitable for the next pass, that is, the tag needs to be permanent, similar to the idea of maintaining the line before, so that the complexity is guaranteed to $o (NLOGN) $

If the current interval is completely covered, then no next pass is required, minimum maintenance, i.e. the interval's monotone queue head, and the smallest of the left and right intervals.

Query when the query has the intersection of all the interval, the node in the segment tree information is only maintained completely contained in the interval of the information, we also need to know and this interval has the intersection but does not contain the information of this interval, so to the sub-tree recursion when the information on the path is also counted.

Summarize:

1. The idea of permanent tagging is more deepened

2. For such a monotonous problem, the same can be used to maintain the line segment tree, it is said that similar problem line tree is more invincible than the CDQ??

3. For each segment tree interval nested on the monotonous queue, easy to explode, began to write monotone queue, fried compilation, changed to dynamic RE, so you can consider <list>

Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<list>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 250010#defineINF 0x7fffffffintn,l,tot,ls[maxn<<1],val[maxn<<4],DP[MAXN],QUE[MAXN],X[MAXN],Y[MAXN],L[MAXN];structdqueuenode{list<int>q;} tree[maxn<<4];intGet (intNow) {return! Tree[now].q.empty ()?Dp[tree[now].q.front ()]:inf;}voidUpdateintNowintLintR) {val[now]=min (now), (l==r)? Inf:min (val[now<<1],val[now<<1|1]));}voidBuild (intNowintLintR) {Val[now]=inf; if(L==R)return; intMid= (l+r) >>1; Build ( now<<1, L,mid); Build (now<<1|1, mid+1, R);}voidInsert (intNowintLintRintLintRintXintf) {    if(L<=l && r>=r) {if(f) while(! Tree[now].q.empty () && Tree[now].q.front () <=x) Tree[now].q.pop_front (); Else{ while(! Tree[now].q.empty () && dp[tree[now].q.back ()]>=Dp[x]) Tree[now].q.pop_back ();            Tree[now].q.push_back (x);}            Update (NOW,L,R); return; }    intMid= (l+r) >>1; if(L<=mid) Insert (now<<1, l,mid,l,r,x,f); if(R>mid) Insert (now<<1|1, mid+1, r,l,r,x,f); Update (NOW,L,R);}intQuery (intNowintLintRintLintR) {    if(L<=l && r>=r)returnVal[now]; intMid= (l+r) >>1, re=Get (now); if(L<=mid) Re=min (Re,query (now<<1, L,mid,l,r)); if(R>mid) Re=min (Re,query (now<<1|1, mid+1, R,l,r)); returnre;}intMain () {N=read (), l=read ();  for(intI=1; i<=n-1; i++) Ls[++tot]=x[i]=read (), Ls[++tot]=y[i]=read (), l[i]=read (); Sort (LS+1, ls+tot+1); inttot=1; for(intI=2; i<=tot; i++)if(ls[i]!=ls[i-1]) ls[++tot]=Ls[i];  for(intI=1; i<=n-1; i++) X[i]=lower_bound (ls+1, ls+tot+1, X[i])-ls,y[i]=lower_bound (ls+1, ls+tot+1, Y[i])-ls; Tot=unique (ls+1, ls+tot+1)-ls-1;//for (int i=1; i<=n-1; i++) printf ("%d%d\n", X[i],y[i]);Build (1,1, Tot); inthe=-1, ta=-1; dp[0]=0; x[0]=1, y[0]=tot; que[++ta]=0; Insert (1,1, tot,x[0],y[0],0,0);  for(intI=1; i<=n-1; i++)        {            inttmp;  while(He<ta && l[i]-l[que[he+1]]&GT;L) Tmp=que[++he],insert (1,1, Tot,x[tmp],y[tmp],tmp,1); Dp[i]=query (1,1, Tot,x[i],y[i]) +1; if(dp[i]!=inf+1) printf ("%d\n", Dp[i]), Que[++ta]=i,insert (1,1, Tot,x[i],y[i],i,0); ElsePuts"-1"); }    return 0;}

"bzoj-2892&1171" Strike Battle & Big SZ Game weight segment tree + monotone queue + tag permanent +DP

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.