Monsters are magical creatures endowed with special powers, and they like to battle each other. Each Monster is of one of three types: Fire, Water or Grass. When two Monsters battle each other, one of them will win according to the following rules:
There are $$$N$$$ Monsters numbered $$$1 \ldots N$$$. You are given the type of each Monster $$$i$$$. You wish to perform $$$Q$$$ operations, where the $$$j$$$-th operation is the following:
Eventually, after some number of battles, there will only be one Monster remaining. You wish to find the number of Monster who could be the last remaining Monster. That is, you wish to count the number of Monster $$$i$$$ such that there is at least one valid sequence of battles and outcomes of battles by which $$$i$$$ is the last remaining Monster.
The first line contains a single integer $$$N$$$, the number of Monster.
The second line contains a string $$$A$$$ of $$$N$$$ characters without spaces, where the $$$i$$$-th character is F, W, or G, depending on whether the $$$i$$$-th Monster is Fire type, Water type, or Grass type, respectively.
The third line contains a single integer $$$Q$$$, the number of operations to be performed.
The next $$$Q$$$ lines describe these operations. The $$$j$$$-th of these lines contains two space-separated integers $$$L_j$$$ and $$$R_j$$$, which have the meaning explained above.
You should print $$$Q$$$ lines of output. The $$$j$$$-th of these lines should consist of a single integer, which is the number of Monster that could be the last remaining Monster in operation $$$j$$$.
The test data for this problem is divided into multiple subtasks. In order to pass a subtask, your submitted program must solve every test case within that subtask correctly and within the time and memory limits.
You will be awarded the points allocated to a subtask if at least one submission you make during the contest passes that subtask. You do not need to combine your solutions for multiple subtasks into a single submission.
Please keep in mind that the subtasks are not necessarily arranged in increasing order of difficulty. We encourage you to try as many subtasks as possible.
Constraints
In all test data, it is guaranteed that:
Subtasks
Constraint for Subtasks 1-9
You only have to perform one operation, and this operation covers all the Monster. In other words, $$$Q = 1$$$, $$$L_1=1$$$, $$$R_1=N$$$.
Constraint for Subtasks 10-11
There may be multiple operations.
7FFWGWFG21 22 4
2 2
5FWGFW11 5
4
35FWFWFWWWFWWFWWFFWFWWFWWWWFWFWWWFWWW11 35
23
Example 1
In this example, there are two operations.
Here is one possible sequence of battles by which Monster $$$2$$$ can be the last remaining Monster:
Here is one possible sequence of battles by which Monster $$$4$$$ can be the last remaining Monster:
There is no such sequence of battles by which Monster $$$3$$$ can be the last remaining Monster.
This example is valid for subtask 11.
Example 2
In this example, there is only one operation which covers all the Monster. It is possible for the Monster $$$1$$$, $$$2$$$, $$$3$$$ and $$$5$$$ to be the last remaining Monster, so the answer is $$$4$$$.
Here is one possible sequence of battles by which Monster $$$1$$$ is the last remaining Monster:
Similar to this, for each Monster $$$2$$$, $$$3$$$ or $$$5$$$ it is possible to find such a sequence of battles and outcomes of battles such that that Monster will be the last remaining Monster. For Monster $$$4$$$, it is impossible to find such a sequence.
This example is valid for subtasks 5, 6, 7, 8, 9 and 11.
Example 3
This example is valid for subtasks 2, 5, 6, 7, 8, 9, 10 and 11.
You are a farmer. You own $$$F$$$ fields of land, which are arranged in a straight line. They are numbered $$$1 \ldots F$$$ from left to right.
You have recently acquired a crop duster, which is an airplane that can quickly spray fertilizer over a large amount of land. The crop duster is preprogrammed with $$$N$$$ possible trips, numbered $$$1 \ldots N$$$. In trip $$$i$$$, the crop duster flies over the fields $$$L_i \ldots R_i$$$, where $$$1 \leq L_i \leq R_i \leq F$$$, and sprays fertilizer over all these fields.
You now plan to use a consecutive set of preprogrammed trips. You would like to answer $$$Q$$$ questions, where the $$$j$$$-th question is the following:
The first line contains a single integer $$$F$$$, the number of fields.
The second line contains a single integer $$$N$$$, the number of preprogrammed trips.
The next $$$N$$$ lines describe the trips. The $$$i$$$-th of these lines contains two integers $$$L_i$$$ and $$$R_i$$$.
The next line contains a single integer $$$Q$$$, the number of questions you wish to answer.
The next $$$Q$$$ lines describe the questions. The $$$j$$$-th of these lines contains two integers $$$X_j$$$ and $$$Y_j$$$.
You should print $$$Q$$$ lines of output. The $$$j$$$-th line should consist of a single integer, the answer to the $$$j$$$-th question.
The test data for this problem is divided into multiple subtasks. In order to pass a subtask, your submitted program must solve every test case within that subtask correctly and within the time and memory limits.
You will be awarded the points allocated to a subtask if at least one submission you make during the contest passes that subtask. You do not need to combine your solutions for multiple subtasks into a single submission.
Please keep in mind that the subtasks are not necessarily arranged in increasing order of difficulty. We encourage you to try as many subtasks as possible.
Constraints
In all test data, it is guaranteed that:
Subtasks
531 42 34 541 22 31 31 1
4 4 5 4
1041 42 57 88 1041 41 24 43 4
9 5 3 4
1054 82 21 35 79 1051 11 21 31 41 5
5 6 8 8 10
1051 42 103 65 82 651 23 43 54 55 5
10 6 7 7 5
1071 42 34 75 103 56 81 251 72 53 46 71 3
10 9 7 5 7
In the first sample, there are $$$3$$$ trips : Trip $$$1$$$ being from field $$$1$$$ to $$$4$$$, trip $$$2$$$ being from field $$$2$$$ to $$$3$$$ and trip $$$3$$$ being from field $$$4$$$ to $$$5$$$.
In the first query, trips $$$1$$$ and $$$2$$$ are in consideration, and we can see fields $$$1$$$ and $$$4$$$ will be fertilized once, fields $$$2$$$ and $$$3$$$ twice, and field $$$5$$$ none. Thus, the answer is $$$4$$$.
Sample $$$1$$$ is valid for Subtasks $$$1$$$, $$$3$$$, $$$6$$$, $$$9$$$ and $$$10$$$.
Sample $$$2$$$ is valid for Subtasks $$$1$$$, $$$3$$$, $$$5$$$, $$$6$$$, $$$9$$$ and $$$10$$$.
Sample $$$3$$$ is valid for Subtasks $$$1$$$, $$$2$$$, $$$3$$$, $$$6$$$, $$$9$$$ and $$$10$$$.
Sample $$$4$$$ is valid for Subtasks $$$1$$$, $$$3$$$, $$$6$$$, $$$8$$$, $$$9$$$ and $$$10$$$.
Sample $$$5$$$ is valid for Subtasks $$$1$$$, $$$3$$$, $$$6$$$, $$$9$$$ and $$$10$$$.
A tree is a set of $$$N$$$ nodes and $$$N-1$$$ undirected edges. Each edge connects two distinct nodes, and between any two nodes there is a unique path that doesn't repeat edges. Further, each edge has an integer edge weight.
You are given two trees, tree X and tree Y. In both these trees, there are $$$N$$$ nodes numbered from $$$1$$$ to $$$N$$$ and $$$N-1$$$ edges numbered from $$$1$$$ to $$$N-1$$$, but the specific edge structures and edge weights of tree X and tree Y may differ.
In tree X, the $$$i$$$-th edge connects nodes $$$A_i$$$ and $$$B_i$$$ and has weight $$$C_i$$$.
In tree Y, the $$$j$$$-th edge connects nodes $$$U_j$$$ and $$$V_j$$$ and has weight $$$W_j$$$.
For some two nodes $$$p$$$ and $$$q$$$ such that $$$1 \leq p \lt q \leq N$$$, $$$\text{cost}_X(p, q)$$$ is defined as the largest edge weight that occurs on the unique path between $$$p$$$ and $$$q$$$ in tree X. Similarly, $$$\text{cost}_Y(p, q)$$$ is defined as the largest edge weight that occurs on the unique path between $$$p$$$ and $$$q$$$ in tree Y.
You are asked to find the number of pairs of nodes $$$(p, q)$$$ such that $$$1 \leq p \lt q \leq N$$$ and $$$\text{cost}_X(p, q) \leq \text{cost}_Y(p, q)$$$. (In total, there are $$$\frac{N(N-1)}{2}$$$ pairs of nodes $$$(p, q)$$$ such that $$$1 \leq p \lt q \leq N$$$.)
The first line of input contains a single integer $$$N$$$, the number of nodes.
The next $$$N-1$$$ lines describe tree X. The $$$i$$$-th of these lines contains three integers, $$$A_i$$$, $$$B_i$$$, and $$$C_i$$$.
The next $$$N-1$$$ lines describe tree Y. The $$$j$$$-th of these lines contains three integers, $$$U_j$$$, $$$V_j$$$, and $$$W_j$$$.
You should print a single integer, the number of pairs of nodes $$$(p, q)$$$ such that $$$1 \leq p \lt q \leq N$$$ and $$$\text{cost}_X(p, q) \leq \text{cost}_Y(p, q)$$$.
The test data for this problem is divided into multiple subtasks. In order to pass a subtask, your submitted program must solve every test case within that subtask correctly and within the time and memory limits.
You will be awarded the points allocated to a subtask if at least one submission you make during the contest passes that subtask. You do not need to combine your solutions for multiple subtasks into a single submission.
Please keep in mind that the subtasks are not necessarily arranged in increasing order of difficulty. We encourage you to try as many subtasks as possible.
Constraints
In all test data, it is guaranteed that:
Please be aware that the output for this problem may not fit in 32-bit integers. You may need to use 64-bit integers in your computations.
Notation
The following notation is used to describe the subtasks:
Subtasks
41 2 22 3 33 4 91 2 22 3 63 4 8
3
51 2 82 3 63 4 24 5 71 2 102 3 43 4 74 5 5
8
51 4 62 3 81 2 92 5 101 4 82 3 81 2 82 5 8
2
61 2 22 4 63 2 84 5 81 6 101 2 32 4 53 2 54 5 91 6 11
10
81 2 62 3 61 4 102 5 74 6 73 7 15 8 41 2 21 3 61 4 24 5 91 6 25 7 17 8 1
11
In the first sample :
We can show that the pairs $$$(p, q) = (1, 2), (2, 3), (1, 3)$$$ all satisfy $$$\text{cost}_X(p, q) \leq \text{cost}_Y(p, q)$$$ while the other $$$3$$$ pairs do not. Hence, the answer is $$$3$$$.
Sample $$$1$$$ is valid for Subtasks 1, 2, 3, 4, 5, 7, 8.
Sample $$$2$$$ is valid for Subtasks 2, 3, 5, 8.
Sample $$$3$$$ is valid for Subtasks 2, 3, 6, 7, 8.
Sample $$$4$$$ is valid for Subtasks 2, 3, 7, 8.
Sample $$$5$$$ is valid for Subtasks 2, 3, 8.