PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000108. Đếm giống bò (Breed Counting)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm giống bò (Breed Counting)

Breed Counting

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

John has NN cows in a row, numbered 11 through NN. Breed IDs are 11 for Holstein, 22 for Guernsey and 33 for Jersey. For each query (a,b)(a,b), count cows of each breed at positions aa through bb, inclusive.

Input

The first line contains integers N,QN,Q, the numbers of cows and queries.

The next NN lines each contain the breed ID cic_i of cow ii.

Each of the last QQ lines contains integers a,ba,b.

Output

Print QQ lines in query order. Each line contains three integers: the counts of breeds 1,2,31,2,3, in that order.

Subtasks

  • Subtask 1 (20 points): 1≤N≤10001\le N\le 1000; 1≤Q≤10001\le Q\le 1000; ci∈{1,2,3}, 1≤i≤Nc_i\in\{1,2,3\},\ 1\le i\le N; 1≤a≤b≤N1\le a\le b\le N.
  • Subtask 2 (30 points): 1≤N≤1000001\le N\le 100000; 1≤Q≤1000001\le Q\le 100000; ci∈{1,2,3}, 1≤i≤Nc_i\in\{1,2,3\},\ 1\le i\le N; 1≤a≤b≤N1\le a\le b\le N; b−a+1≤10b-a+1\le 10.
  • Subtask 3 (50 points): 1≤N≤1000001\le N\le 100000; 1≤Q≤1000001\le Q\le 100000; ci∈{1,2,3}, 1≤i≤Nc_i\in\{1,2,3\},\ 1\le i\le N; 1≤a≤b≤N1\le a\le b\le N.

Examples 1

Input

6 3
2
1
1
3
2
1
1 6
3 3
2 4

Output

3 2 1
1 0 0
2 0 1

Explanation

The breed IDs are 2,1,1,3,2,12,1,1,3,2,1. In [1,6][1,6], breed 1 occurs at 2, 3, 6; breed 2 at 1, 5; breed 3 at 4. Interval [3,3][3,3] contains one breed-1 cow. Interval [2,4][2,4] contains 1,1,31,1,3, hence counts 2,0,12,0,1.