Advertisement
mfgnik

Untitled

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