Little Q is learning about a page replacement algorithm in operating systems memory management~-- the LRU (Least Recently Used) algorithm.
To help Little Q understand this algorithm, you need to answer some questions regarding LRU in this problem. First, we briefly introduce the principle of this algorithm:
-
Set up a cache container of capacity kkk to store loaded pages and related information. Initially, no pages are in the container.
-
The system will sequentially process requests to access pages, handling one request at a time.
-
For accessing a page that is already in the cache (i.e., a cache hit), use the cache directly as the data source and update the page's most recent access time.
-
For accessing a page that is not in the cache (i.e., a cache miss), if the number of pages in the cache has reached kkk, first evict a page from the cache to ensure the number of pages in the cache is less than kkk, and then load the accessed page from its real address into the cache, using the cache as the data source and updating the page's most recent access time.
-
Whenever a page needs to be evicted, choose the one with the earliest most recent access time to release from the cache.
Next, we use pages numbered from
111 to
nnn and provide an array of length
nnn,
[a1,a2,…,an][a_1, a_2, \ldots, a_n][a1,a2,…,an], along with
qqq queries, and you are asked to answer these queries. Each query is one of the following two types:
-
1 l r k1\ l\ r\ k1 l r k: Access the pages numbered al,al+1,…,ara_l, a_{l + 1}, \ldots, a_ral,al+1,…,ar in order, using an LRU cache with a maximum capacity of kkk, and report the number of cache hits.
-
2 l r k2\ l\ r\ k2 l r k: Access the pages numbered al,al+1,…,ara_l, a_{l + 1}, \ldots, a_ral,al+1,…,ar in order, using an LRU cache, requiring that the number of cache hits is at least kkk, and report the minimum required LRU capacity. If the goal of cache hits cannot be achieved, regard the capacity as −1-1−1.