mfgnik

Untitled

May 31st, 2020
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. struct FenwickTree {
  7.     vector<int> bit;// binary indexed tree
  8.     int n;
  9.  
  10.     FenwickTree(int n) {
  11.         this->n = n;
  12.         bit.assign(n, 0);
  13.     }
  14.  
  15.     FenwickTree(vector<int> a) : FenwickTree(a.size()) {
  16.         for (size_t i = 0; i < a.size(); i++)
  17.             add(i, a[i]);
  18.     }
  19.  
  20.     int sum(int r) {
  21.         int ret = 0;
  22.         for (; r >= 0; r = (r & (r + 1)) - 1)
  23.             ret += bit[r];
  24.         return ret;
  25.     }
  26.  
  27.     int sum(int l, int r) {
  28.         return sum(r) - sum(l - 1);
  29.     }
  30.  
  31.     void add(int idx, int delta) {
  32.         for (; idx < n; idx = idx | (idx + 1))
  33.             bit[idx] += delta;
  34.     }
  35. };
  36.  
  37.  
  38. int main() {
  39.     FenwickTree tree(43000);
  40.     int q;
  41.     std::cin >> q;
  42.     std::unordered_map<int, int> results;
  43.     int amount = 0;
  44.     while (q > 0) {
  45.         std::string command;
  46.         std::cin >> command;
  47.         if (command == "RUN") {
  48.             int who, result;
  49.             std::cin >> who >> result;
  50.             if (results.count(who) == 0) {
  51.                 ++amount;
  52.             } else {
  53.                 tree.add(results[who], -1);
  54.             }
  55.             results[who] = result;
  56.             tree.add(results[who], 1);
  57.         } else {
  58.             int who;
  59.             std::cin >> who;
  60.             if (results.count(who) == 0) {
  61.                 std::cout << 0 << "\n";
  62.             } else if (amount == 1) {
  63.                 std::cout << 1 << "\n";
  64.             } else {
  65.                 std::cout << tree.sum(results[who] - 1) / amount << "\n";
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment