uva11423 - Cache Simulator 树状数组

Problem G
Cache Simulator
Input:
Standard Input

Output: Standard Output

 

Cache is a special type of memoryproviding very small latency for accessing. The basic idea of using cache is tokeep recently used data close to processors. So, modern processors use smallcache along with large main memory. Program performance depends on the cacheperformance. If data is found in cache (Hit), execution can proceed normally.On the other hand if data is not found in cache, execution stalls until thatdata is fetched from main memory. Computer architects alwayslooks for miss rate of cache (#of misses per 100 accesses).

When a miss occurs, the newdata is kept in the cache and some existing data is removed from cache. Thereare several policies to select which one to evict. A common technique used inany computer systems is LRU (Least Recently Used). According to this scheme,the data that has not been used for longest time is evicted. Let us explain howthis scheme works –

Assume a cache with 4 entries.So, if the processor accesses data 1, 2, 3, 4, 5, 2, 3, 99, 2, 5, 4 thefollowing things will occur -

Cache

Hit/Miss

Data evicted

 

Miss (1)

 

1

Miss (2)

 

1 2

Miss (3)

 

1 2 3

Miss (4)

 

1 2 3 4

Miss (5)

1

2 3 4 5

Hit (2)

 

2 3 4 5

Hit (3)

 

2 3 4 5

Miss (99)

4

2 3 5 99

Hit (2)

 

2 3 5 99

Hit (5)

 

2 3 5 99

Miss (4)

3

2 4 5 99

 

 

 

Here for 11 data references, wehave 7 misses. In this problem you have to find the number of misses fordifferent cache sizes. You will be given data references in several ways –

a)     ADDR      x        : Processor will access data x

b)     RANGE  b y n:Processor will access all data references in the form b+y*k,where k is 0  to n-1  

Your will also be givencommands STAT to print the number of misses for each cache (excluding themisses occurred before last STAT command). The above example can be abstractedin the following way –

RANGE 1 1 5

     RANGE 2 1 2

     ADDR  99

     STAT               ---------------------------------- 6

     ADDR  2

     RANGE 5 –1 2

     STAT                ----------------------------------1

Input

Your program starts with apositive integer N (1<=N<=30), which is the number of caches. The nextline contains size of each cache (in increasing order). Cache size will be atleast 2 but not exceeding 220. The next few lines will contain anyof RANGE, ADDR, or STAT. The last line will contain STAT. Total number of datareferences (all data accessed by processor) will not exceed 107. Forthe sample input, total number of data references is 30 (5+2+1+1+2+10+5+4). Also,the processor uses 24-bit address, no data reference will exceed 224-1and will be non-negative. The value of b, y, n (in RANGE), and x (in ADDR) willbe consistent with all restrictions. Input is terminated by a line containingEND. The input file has around 20000 lines of inputs.

 
Output

For each STAT command just print thenumber of misses (as described above) in a line.

 

Sample Input                             Output for SampleInput

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

6 6

1 0

18 13

 

  有N个cash,工作方式就是找当前地址在不在cash中,如果在就把它放到最后面,否则MISS,把最前面那个从cash中删掉,把它放到最后面。每个cash容量不一样大,RANGE操作是查询b+y*k,where k is 0  to n-1,ADDR是查询x,STAT输出每个cash MISS量。

  用树状数组c[i]表示第i个查询的数。vis[x]表示x最近一次出现的位置,那么如果最近出现和这次出现中间的数小于等于cash数说明命中,否则未命中。如果再次出现就要把上次出现的删掉,add(vis[x],-1)。每次把当前出现的加上add(vis[x]=++now,1)。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXNODE 105
#define MOD 10000007
#define SIGMA_SIZE 4
typedef long long LL;
using namespace std;

const int MAXN=17000000;
const int MAXM=35;

int N,now;
int c[MAXN],vis[MAXN],cash[MAXM],ans[MAXM];

int lowbit(int x){
    return x&(-x);
}

int add(int x,int v){
    while(x<MAXN){
        c[x]+=v;
        x+=lowbit(x);
    }
}

int sum(int x){
    int ret=0;
    while(x>0){
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}

void solve(int x){
    if(vis[x]){
        int len=sum(now)-sum(vis[x]-1);
        for(int i=1;i<=N;i++){
            if(cash[i]<len) ans[i]++;
            else break;
        }
        add(vis[x],-1);
    }
    else{
        for(int i=1;i<=N;i++) ans[i]++;
    }
    add(vis[x]=++now,1);
}

int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d",&N)!=EOF){
        now=0;
        memset(vis,0,sizeof(vis));
        memset(ans,0,sizeof(ans));
        memset(c,0,sizeof(c));
        char str[20];
        for(int i=1;i<=N;i++) scanf("%d",&cash[i]);
        while(scanf("%s",str),str[0]!='E'){
            int b,y,n;
            if(str[0]=='A'){
                scanf("%d",&b);
                solve(b);
            }
            else if(str[0]=='R'){
                scanf("%d%d%d",&b,&y,&n);
                for(int i=0;i<n;i++) solve(b+i*y);
            }
            else{
                for(int i=1;i<N;i++) printf("%d ",ans[i]);
                printf("%d\n",ans[N]);
                memset(ans,0,sizeof(ans));
            }
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值