Nlogonia is fighting a ruthless war against the neighboring country of Cubiconia. The chief general of Nlogonia ' s Army decided to attack the enemy with a linear formation of soldiers, that would advance t Ogether until conquering the neighboring country. Before the battle, the chief general ordered, all soldier in the attack line, besides protecting himself and Attackin G, should also protect his (nearest) neighbors on the line, one to he left and one to him right. The chief general told the soldiers this for each of the them, he ' buddies ' would be these the neighbors, if such neighbors existed (because the leftmost soldier does not having a left neighbor and the rightmost soldier does is not having a right NEIGHB OR). The chief general also told the soldiers that protecting their buddies is very important to prevent the attack line from Being broken. So important this, if the left or right buddy of a soldier are killed, then the next living neighbor Right of theLdier, respectively, should become his buddy.
The battle is fierce, and many soldiers in the attack line is being killed by fire shots, grenades and bombs. But following the chief General's orders, immediately after knowing on losses in the attack line, the Army ' s informatio N Systems Division have to inform the soldiers who their new buddies is.
You is given the number of soldiers in the attack line, and a sequence of loss reports. Each loss report describes a group of contiguous soldiers on the attack line, were just killed in the battle. Write a program this, for each loss report, prints the new buddies formed.
Input
Each test case is described using several lines. The first input line contains, integers S and Brepresenting respectively the number of soldiers in th E attack line, and the number of loss reports (1BS105). Soldiers is identified by different integers from 1 to S, according to their positions on the attack line, being 1 The leftmost soldier and S the rightmost soldier. Each of the next B input lines describes a loss report using both integers L (left) and R (right ), meaning that soldiers from L to R were killed (1LrS). You may assume this until that moment those soldiers were alive and were just killed.
The last test was followed by a line containing the zeros.
Output
For each test case output B + 1 lines. In the i-th output line write the new buddies formed by removing from the attack line the soldiers that were just Killed according to the I-th loss report. That's, for the loss report 'l R', print the first surviving soldier to the left of L , and the first surviving soldier to the right of R. For each direction, print the character '*' (asterisk) if there are no surviving soldier in that direction. Print a line containing a, character '-' (hyphen) after each test case.
Sample Input
1 11 110 42 56 91 110 105 11 10 0
Sample Output
* *-1 61 10* 10* *-* 2-
This problem first wanted to use a line tree to do, and then changed to a tree array, and found that it should not be done with a tree array, that hangs on the timeout edge
Lessons learned: The person who cannot judge the K position by Dat[k]!=0 is not killed, and this value may be contributed by someone smaller than it.
Train of thought: Each position is 1, was killed to become 0, because each person kills only once, so the most is killed 10^5 times, this even according to the person update can bear, so simply separate from L to r each point of the closed interval is updated to 0; For a whole range, because it is 0, so sum (0,i) ( I belong to the interval of the subscript) are equal, and the left not killed person happens to be equal to the first ordinal number of this value, the person who is not killed on the right is equal to the first ordinal number of this value +1, to find sum (0,i) is of course a tree-like array
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;const int Maxn=1e5+2;int S,b;int dat[maxn<<1];int lowbit (int k) {return k& (-K);} void Update (int k,int a) {if (k<0| | DAT[K]+A<0) return; while (k<=s) {dat[k]+=a; K+=lowbit (k); }}int query (int k) {int ans=0; while (k>0) {ans+=dat[k]; K-=lowbit (k); }
return ans;} int getk (int k) {//Find and just the first point of K, the return value is less than or equal to n, greater than or equal to 1 if (k<0) return 0; int ans=0,cnt=0; for (int i=25;i>=0;i--) {ans+= (1<<i); if (ans>=s| | cnt+dat[ans]>=k) ans-= (1<<i); else Cnt+=dat[ans]; } return ans+1;} int main () {while (scanf ("%d%d", &s,&b) ==2&&s&&b) {//for (int i=1;i<=s;i++) update (I,-1); Not this one.
memset (dat,0,sizeof (int) *2*n); for (int i=1;i<=s;i++) update (i,1);//Initialize for (int i=0;i<b;i++) {int l,r; scanf ("%d%d", &l,&r); for (int i=l;i<=r;i++) update (I,-1);//decrement int tmp=query (r);//query this interval before the remainder of the number of int POS1=GETK (TMP);//Find left Point int POS2=GETK (tmp+1);//Find Right Endpoint
if (query (POS2)!=tmp+1) pos2++;//because the algorithm does not exceed N, in order to determine whether the last person exists if (query (POS1) ==0) putchar (' * '); else printf ("%d", POS1); Putchar ("); if (pos2>s) putchar (' * '); else printf ("%d", pos2); Putchar (' \ n '); } puts ("-");//Split case} return 0;}
UVA 12356 Army Buddies Tree Array solution tree array to add and just k the smallest item number difficulty: 1