Question: Give you an inserted sequence, and then output the final sequence. If you use the general method to query as o (n ), the line segment tree query can be optimized to log (n )....
AC code:
[Cpp]
# Include <iostream>
# Include <string. h>
# Include <algorithm>
# Include <cstdio>
# Define CLR (arr, val) memset (arr, val, sizeof (arr ))
# Define lson l, mid, rt <1
# Define rson mid + 1, r, rt <1 | 1
# Define N 200005
Using namespace std;
Typedef struct {
Int l;
Int r;
Int sum;
} Node;
Node node [N <2];
Struct Point
{
Int x;
Int y;
} S1 [N];
Int s [N];
Void push_up (int rt)
{
Node [rt]. sum = node [rt <1]. sum + node [rt <1 | 1]. sum;
}
Void build (int l, int r, int rt)
{
Node [rt]. l = l;
Node [rt]. r = r;
If (l = r ){
Node [rt]. sum = 1;
Return;
}
Int mid = (l + r)> 1;
Build (lson );
Build (rson );
Push_up (rt );
}
Void Quary (int l, int r, int rt, int val, int x)
{
If (node [rt]. l = node [rt]. r)
{
Node [rt]. sum = 0;
S [node [rt]. l] = x;
Return;
}
Int mid = (l + r)> 1;
If (node [rt <1]. sum> = val) Quary (lson, val, x );
Else Quary (rson, val-node [rt <1]. sum, x );
Push_up (rt );
}
Void in (int &)
{
Char ch;
While (ch = getchar () <'0' | ch> '9 ');
For (a = 0; ch> = '0' & ch <= '9'; ch = getchar () a = a * 10 + ch-'0 ';
}
Int main ()
{
Int n;
While (~ Scanf ("% d", & n ))
{
CLR (s,-1 );
CLR (node, 0 );
Build (1, n, 1 );
For (int I = 0; I! = N; ++ I) in (s1 [I]. x), s1 [I]. x ++, in (s1 [I]. y );
For (int I = n-1; I> = 0; -- I) Quary (1, n, 1, s1 [I]. x, s1 [I]. y );
For (int I = 1; I <n; ++ I)
Printf ("% d", s [I]);
Printf ("% d \ n", s [n]);
} Return 0;
}