PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#SRD0000092. Truy vấn nguyên thủy (Primitive Queries)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Truy vấn nguyên thủy (Primitive Queries)
Primitive Queries
Source: VNOJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given an undirected tree with vertices, vertex initially carries the integer . Process operations in input order.
There are two operation types:
1 u v: count the distinct vertex values on the unique simple path from to , including both endpoints.2 x val: assign the value to vertex , replacing its old value (not adding to it).
Each type-1 answer must use the state obtained after all preceding assignments.
Input
- The first line contains .
- The second line contains initial values .
- Each of the next lines contains endpoints of an undirected tree edge; the edges form a tree.
- Each of the last lines contains
1 u vor2 x val.
Output
Print one integer for each type 1 operation: the number of distinct values on its queried path. Print nothing for type 2 operations.
Subtasks
- Subtask 1 (50 points): Only type
1operations occur. - Subtask 2 (50 points): No additional restrictions.
Complete conditions for all subtasks, subject to the tighter limits above: , , .
Examples
Input
6 7
1 2 3 4 5 6
1 2
1 3
2 4
2 5
3 6
1 5 6
2 5 6
1 1 5
1 5 6
2 1 6
1 5 6
1 2 6
Output
5
3
4
3
3
Explanation
Initially the values at vertices are .
1 5 6follows path with values , so output5.2 5 6assigns value to vertex .1 1 5has values , giving3.1 5 6has values : four distinct values, giving4.2 1 6assigns value to vertex .1 5 6has values , giving3.1 2 6has values , giving3.