Ultraviolet A 11423-Cache Simulator (tree array)

Source: Internet
Author: User

Ultraviolet A 11423-Cache Simulator (tree array)
Ultraviolet A 11423-Cache Simulator

Question Link

The question is about a few cash. Each operation can add one or more data. If there is hit before the data, the hit data will not disappear. If there is no hit data, it will be missed, when the capacity exceeds the cash capacity, it will discard one of the earliest dead ones. Each time the START command is executed, the miss count is calculated and the output is

Idea: Since there can be a maximum of 2 ^ 24 data, you can open a tree array, each location indicates the data added to the I, and record the location of each data, the next time you add existing data to a previous one, you can use a tree array to query the total number of data records from the previous one to this one. If the data exceeds the cash size, then it indicates that the previous data has been discarded, and the previous data location is-1, set this data to the currently added location + 1. Note that you need to re-calculate each START time.

Code:

#include 
 
  #include 
  
   #include using namespace std;#define lowbit(x) (x&(-x))const int N = 35;const int MAXN = (1<<24) + 5;int n, cach[N], bit[MAXN];void add(int x, int v) {    while (x < MAXN) {bit[x] += v;x += lowbit(x);    }}int get(int x) {    int ans = 0;    while (x) {ans += bit[x];x -= lowbit(x);    }    return ans;}int get(int l, int r) {    return get(r) - get(l - 1);}char op[10];int ans[N], vis[MAXN], now;void init() {    now = 0;    memset(bit, 0, sizeof(bit));    memset(vis, 0, sizeof(vis));    for (int i = 0; i < n; i++)scanf("%d", &cach[i]);}void tra(int num) {    if (vis[num]) {int len = get(vis[num], now);for (int i = 0; i < n; i++) {    if (cach[i] >= len) break;    ans[i]++;}add(vis[num], -1);    }    else {for (int i = 0; i < n; i++)    ans[i]++;    }    add(vis[num] = ++now, 1);}void solve() {    int x, b, y, nn;    while (scanf("%s", op)) {if (op[0] == 'E') break;if (op[0] == 'S') {    for (int i = 0; i < n; i++)printf("%d%c", ans[i], i == n - 1 ? '\n' : ' ');    memset(ans, 0, sizeof(ans));}if (op[0] == 'A') {    scanf("%d", &x);    tra(x);}if (op[0] == 'R') {    scanf("%d%d%d", &b, &y, &nn);    for (int i = 0; i < nn; i++)tra(b + y * i);}    }}int main() {    while (~scanf("%d", &n)) {init();solve();    }    return 0;}
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.