A: Construct A group of data.
Obviously, the first number can only be compared once. The first two numbers are the largest and equal.
B: Calculate the sum. If it is a multiple of n, it means that all numbers are always equal. If it is not a multiple of n, you can use one number to construct the other n-1 numbers equal.
C: questions that have been stuck for a long time. Note that the range of K (n + 1) * n/2 is obviously 1 + 2 ...... N
Descending order of the series, a0, a1, a2, a3 (ai> ai + 1)
Consider taking only one number, there are n possibilities, and these n are different from each other
Take two numbers into consideration, where a0 is required, and take one from the remaining n-1 numbers. There are n-1 possibilities. These n-1 types are different from each other, and we also consider comparing them with the first n, the maximum value of the first n types is obviously a0, And the last n-1 types are a0 + ai> a0, so they are different.
Similarly, take three numbers, four numbers, and n numbers.
For the number of I, always the number of the first I-1 is required.
So the total case is n + n-1 + N-2 ...... + 2 + 1
D: directly violent. Just use set.
E: recursive offline, always RE, cry
You can also traverse the entire tree online, and the number. For node u, the number of all descendants is always within the range (left [u], right [r.
For a query, you can locate the interval of this layer by means of binary.
There may be repeated queries. Record the map. Otherwise, T
[Cpp]
Vector <int> e [N];
Map <pair <int, int>, int> ans;
Int n;
Char str [105];
String name [N];
Int Left [N], Right [N];
Int depth [N], idx = 0;
Int max_depth =-1;
Vector <int> dep [N];
Void dfs (int u, int h)
{
Max_depth = max (max_depth, h );
Depth [u] = h;
Dep [h]. pb (u );
Left [u] = idx ++;
For (int I = 0; I <e [u]. size (); I ++) dfs (e [u] [I], h + 1 );
Right [u] = idx ++;
}
Int query (int u, int k, int l, int r)
{
Int h = depth [u] + k;
If (h> max_depth) return 0;
If (dep [h]. empty () return 0;
If (ans. find (mp (u, k ))! = Ans. end () return ans [mp (u, k)];
Int low = 0, high = dep [h]. size ()-1, mid, L = N, R =-1;
While (low <= high ){
Mid = (low + high)> 1;
If (Left [dep [h] [mid]> l ){
High = mid-1;
L = mid;
}
Else low = mid + 1;
}
Low = 0, high = dep [h]. size ()-1;
While (low <= high ){
Mid = (low + high)> 1;
If (Left [dep [h] [mid] <r ){
Low = mid + 1;
R = mid;
}
Else high = mid-1;
}
Set <string> s;
While (L <= R) s. insert (name [dep [h] [L]), L ++;
Return ans [mp (u, k)] = s. size ();
}
Int main ()
{
Scanf ("% d", & n );
For (int I = 1; I <= n; I ++)
{
Int k;
Scanf ("% s % d", str, & k );
Name [I] = string (str, strlen (str ));
E [k]. pb (I );
}
Dfs (0, 0 );
Int q;
Scanf ("% d", & q );
While (q --)
{
Int u, k;
Scanf ("% d", & u, & k );
Printf ("% d \ n", query (u, k, Left [u], Right [u]);
}
Return 0;
}
Vector <int> e [N];
Map <pair <int, int>, int> ans;
Int n;
Char str [105];
String name [N];
Int Left [N], Right [N];
Int depth [N], idx = 0;
Int max_depth =-1;
Vector <int> dep [N];
Void dfs (int u, int h)
{
Max_depth = max (max_depth, h );
Depth [u] = h;
Dep [h]. pb (u );
Left [u] = idx ++;
For (int I = 0; I <e [u]. size (); I ++) dfs (e [u] [I], h + 1 );
Right [u] = idx ++;
}
Int query (int u, int k, int l, int r)
{
Int h = depth [u] + k;
If (h> max_depth) return 0;
If (dep [h]. empty () return 0;
If (ans. find (mp (u, k ))! = Ans. end () return ans [mp (u, k)];
Int low = 0, high = dep [h]. size ()-1, mid, L = N, R =-1;
While (low <= high ){
Mid = (low + high)> 1;
If (Left [dep [h] [mid]> l ){
High = mid-1;
L = mid;
}
Else low = mid + 1;
}
Low = 0, high = dep [h]. size ()-1;
While (low <= high ){
Mid = (low + high)> 1;
If (Left [dep [h] [mid] <r ){
Low = mid + 1;
R = mid;
}
Else high = mid-1;
}
Set <string> s;
While (L <= R) s. insert (name [dep [h] [L]), L ++;
Return ans [mp (u, k)] = s. size ();
}
Int main ()
{
Scanf ("% d", & n );
For (int I = 1; I <= n; I ++)
{
Int k;
Scanf ("% s % d", str, & k );
Name [I] = string (str, strlen (str ));
E [k]. pb (I );
}
Dfs (0, 0 );
Int q;
Scanf ("% d", & q );
While (q --)
{
Int u, k;
Scanf ("% d", & u, & k );
Printf ("% d \ n", query (u, k, Left [u], Right [u]);
}
Return 0;
}