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

#FT0000135. Mô phỏng bộ nhớ đệm (Cache Simulator)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Mô phỏng bộ nhớ đệm (Cache Simulator)

Cache Simulator

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

A processor accesses nonnegative integer memory addresses. There are NN independent caches with capacities CiC_i distinct addresses. Initially all caches are empty. Each cache follows LRU (Least Recently Used) replacement: accessing a resident address is a hit and makes that address most recently used; accessing an absent address is a miss, inserts that address, and evicts the least recently used one if necessary.

You are given commands:

  • ADDR x: access the single address xx.
  • RANGE b y k: access kk addresses b,b+y,b+2y,…,b+(k−1)yb,b+y,b+2y,\ldots,b+(k-1)y in exactly that order. The stride yy may be negative or zero.
  • STAT: print the number of misses since the preceding STAT for every cache in the initial capacity order, then reset all miss counters to zero. STAT does not clear or otherwise alter cache contents.
  • END: marks the end of input and is not a memory access.

The last command before END is guaranteed to be STAT. Every cache sees the same sequence of references, but cache capacities differ. All accessed addresses, including every address generated by RANGE, are valid 24-bit addresses.

Input

The first line contains the number of caches NN. The second line lists strictly increasing capacities C1,…,CNC_1,\ldots,C_N. Subsequent input contains ADDR x, RANGE b y k, and STAT commands. The argument of ADDR may appear on the next line because whitespace separates tokens. The final line is END.

Output

For every STAT, print one line of exactly NN integers: miss counts since the previous STAT, in the cache order C1,…,CNC_1,\ldots,C_N.

Subtasks

  • Subtask 1 (20 points): 1≤N≤301\le N\le 30; 2≤C1<C2<⋯<CN≤2202\le C_1<C_2<\cdots<C_N\le 2^{20}; every accessed address lies in [0,224−1][0,2^{24}-1]; 1≤k≤2001\le k\le 200 for every RANGE; 1≤R≤2001\le R\le 200 where RR counts all accesses generated by ADDR and RANGE; at most 2000020000 commands excluding END; at least one STAT, and the last command before END is STAT.
  • Subtask 2 (30 points): 1≤N≤301\le N\le 30; 2≤C1<C2<⋯<CN≤2202\le C_1<C_2<\cdots<C_N\le 2^{20}; every accessed address lies in [0,224−1][0,2^{24}-1]; 1≤k≤200001\le k\le 20000 for every RANGE; 1≤R≤200001\le R\le 20000 where RR counts all accesses generated by ADDR and RANGE; at most 2000020000 commands excluding END; at least one STAT, and the last command before END is STAT.
  • Subtask 3 (50 points): 1≤N≤301\le N\le 30; 2≤C1<C2<⋯<CN≤2202\le C_1<C_2<\cdots<C_N\le 2^{20}; every accessed address lies in [0,224−1][0,2^{24}-1]; 1≤k≤100000001\le k\le 10000000 for every RANGE; 1≤R≤100000001\le R\le 10000000 where RR counts all accesses generated by ADDR and RANGE; at most 2000020000 commands excluding END; at least one STAT, and the last command before END is STAT.

Examples

Example 1

Input

2
4 8
RANGE 1 1 5
RANGE 2 1 2
ADDR
99
STAT
ADDR
2
RANGE 5 -1 2
STAT
RANGE 0 10000 10
RANGE 0 20000 5
RANGE 0 30000 4
STAT
END

Output

6 6
1 0
18 13

Explanation

The first RANGE accesses five new addresses 1,2,3,4,51,2,3,4,5, causing five misses per cache. RANGE 2 1 2 revisits 2,32,3, both hits, and ADDR 99 adds one miss, so the first STAT prints 6 6. Caches are not cleared. Next, ADDR 2 hits in the capacity-8 cache and RANGE 5 -1 2 accesses 5,45,4; the capacity-4 cache incurs one miss while capacity-8 incurs none, giving 1 0. Across the final three RANGE commands, the capacity-4 cache incurs 1010, 55, and 33 misses, totalling 1818. The capacity-8 cache incurs 1010, 22, and 11 misses, totalling 1313. These are counts since the most recent STAT, so the third line is 18 13.

Example 2

Input

2
2 3
ADDR 1
ADDR 2
ADDR 3
ADDR 1
STAT
ADDR 2
STAT
END

Output

4 3
1 0

Explanation

The first four accesses are 1,2,3,11,2,3,1. The size-2 cache has only the two most recently used addresses, so the last access to 11 misses, for 44 misses total. The size-3 cache holds all three addresses, so that access hits, for 33 misses. After STAT, cache contents remain unchanged. The next access to 22 misses in the size-2 cache (which contains 3,13,1) but hits in size 3, hence 1 0.