5249 HDU: KPI

KPI

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem Description
After you work, KPI is your all and I developed a service, achieved great popularity. Postponed billions request to service a large pipe while pulling the request from the ferrule. Let's define each request has an important value. I KPI is an important intermediate value by the current value of the request in the pipeline is calculated. Now give your service record, sometimes I wonder important request in the current pipeline worth intermediate values.
 

 

Input
About 100 sets of data.

Each data row has a first n- ( . 1 n- 10000 ) , the number of records service representative.

Then there are n rows, each row has three forms
  "in x": representatives of key value X ( 0 X 10 . 9 ) requests the pipe is advanced.
  "out": pulling the Service on behalf of the head of the pipeline request.
  "query: I want to know a request on behalf of important intermediate value of the current value of the pipeline that is to say, if there are m number of requests, I want to know, on the ascending order of the current pipeline. F L O O R & lt ( m / 2 ) + . 1 T H  importance value requested article.

in order to make simple the subject, x are all different, and if there is no value in a conduit, there will be "out" and "query" operations.
 

 

Output
For each test, the first output line

Case #i:
Then every "query", the output value of the current pipeline important intermediate values.
 

 

Sample Input
6
in 874
query
out
in 24622
in 12194
query
 

 

Sample Output
Case #1:
874
24622
 

Algorithm: Weight segment tree

Solution: This problem needs to discretization, then segment tree according to the characteristics of the weights, the number of each of the sequentially updated section numbers, and according to the number of queries, questions in the pulling head pipe, direct use of analog queue just fine.

 

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

const int maxn = 1e5+7;

struct query {
    int op;
    int val;
}Q[maxn];

struct tree {
    int l, r, s;
}tree[maxn << 2];


vector<int> v;
int n;
int len;

int find(int x) {
    return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
}

void init() {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    len = v.size();
}

void build(int root, int l, int r) {
    tree[root].l = l;
    tree[root].r = r;
    tree[root].s = 0;
    if(l == r) {
        return;
    }
    int mid = (l + r) >> 1;
    build(root << 1, l, mid);
    build(root << 1 | 1, mid + 1, r);
}

void update(int root, int pos, int val) {
    int l = tree[root].l;
    int r = tree[root].r;
    tree[root].s += val;
    if(l == r) {
        return;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid) {
        update(root << 1, pos, val);
    } else {
        update(root << 1 | 1, pos, val);
    }
}

int query(int root, int k) {
    int l = tree[root].l;
    int r = tree[root].r;
    if(l == r) {
        return l;
    }
    int sum = tree[root << 1].s;
    if(sum >= k) {
        return query(root << 1, k);
    } else {
        return query(root << 1 | 1, k - sum);
    }
}

int main() {
    int cas = 0;
    while(~scanf("%d", &n)) {
        for(int i = 1; i <= n; i++) {
            char str[10];
            scanf("%s", str);
            if(str[0] == 'i') {
                Q[i].op = 1;
                scanf("%d", &Q[i].val);
                v.push_back(Q[i].val);
            } else if(str[0] == 'o') {
                Q[i].op = 2;
            } else {
                Q[i].op = 3;
            }
        }
        init();
        build(1, . 1 , len); 
        Queue < int > que;      // this queue analog data into and out 
        the printf ( " Case #% D: \ n- " , ++ CAS);
         for ( int I = . 1 ; I <= n-; I ++ ) {
             IF (Q [I] == .OP . 1 ) {
                 int POS = Find (Q [I] .val); 
                Update ( . 1 , POS, . 1 ); 
                que.push (POS);       
            } the else  IF (Q [I ] .OP == 2 ) {
                update(1, que.front(), -1);
                que.pop();
            } else {
                int k = (que.size() / 2) + 1;
                int pos = query(1, k);
                printf("%d\n", v[pos - 1]);
            }
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/buhuiflydepig/p/11287074.html