Ultraviolet A 11423-Cache simulator (tree array)

Source: Internet
Author: User

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 <cstdio> # include <cstring> # include <algorithm> 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 S [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", & 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.