warriorscats

Tsm

May 27th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. // TSM.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <set>
  7. #include <map>
  8. #include <iterator>
  9. #include <vector>
  10.  
  11. #define NMAX 1000001
  12.  
  13.  
  14. class InParser {
  15. public:
  16.     InParser (const char* file_name) {
  17.         input_file.open (file_name, std::ios::in);
  18.         input_file.sync_with_stdio (false);
  19.         index = 0;
  20.         input_file.read (buffer, SIZE);
  21.     }
  22.  
  23.     InParser& operator >> (int& n) {
  24.         for (; buffer[index] < '0' || buffer[index] > '9'; inc ());
  25.         n = 0;
  26.  
  27.         for (; '0' <= buffer[index] && buffer[index] <= '9'; inc ())
  28.             n = n * 10 + buffer[index] - '0';
  29.  
  30.         return *this;
  31.     }
  32.  
  33.     ~InParser () {
  34.         input_file.close ();
  35.     }
  36.  
  37. private:
  38.     std::fstream input_file;
  39.     static const int SIZE = 0x400000; ///4MB
  40.     char buffer[SIZE];
  41.     int index = 0;
  42.  
  43.     inline void inc () {
  44.         if (++index == SIZE)
  45.             index = 0, input_file.read (buffer, SIZE);
  46.     }
  47. };
  48.  
  49. class OutParser {
  50. public:
  51.     OutParser (const char* file_name) {
  52.         output_file.open (file_name, std::ios::out);
  53.         output_file.sync_with_stdio (false);
  54.         index = 0;
  55.     }
  56.  
  57.     OutParser& operator << (int target) {
  58.         aux = 0;
  59.         n = target;
  60.         if (!n)
  61.             nr[aux ++] = '0';
  62.         for (; n; n /= 10)
  63.             nr[aux ++] = n % 10 + '0';
  64.         for (; aux; inc ())
  65.             buffer[index] = nr[-- aux];
  66.  
  67.         return *this;
  68.     }
  69.  
  70.     OutParser& operator << (const char* target) {
  71.         aux = 0;
  72.         while (target[aux])
  73.             buffer[index] = target[aux ++], inc ();
  74.  
  75.         return *this;
  76.     }
  77.  
  78.     ~OutParser () {
  79.         output_file.write (buffer, index);
  80.         output_file.close ();
  81.     }
  82.  
  83. private:
  84.     std::fstream output_file;
  85.     static const int SIZE = 0x200000; ///2MB
  86.     int index = 0, aux = 0, n = 0;
  87.     char buffer[SIZE], nr[24];
  88.  
  89.     inline void inc () {
  90.         if (++index == SIZE)
  91.             index = 0, output_file.write (buffer, SIZE);
  92.     }
  93. };
  94.  
  95. InParser fin ("tsm.in");
  96. OutParser fout ("tsm.out");
  97.  
  98. int n, task, val, x;
  99. std::multiset <int> Heap;
  100. std::vector <int> BIT (NMAX);
  101. /*std::map <int, int> Hash;
  102. std::set <int>::iterator it;*/
  103.  
  104.  
  105. inline void Update (int index, int val) {
  106.     while (index < NMAX)
  107.         BIT[index] += val, index += index & (- index);
  108. }
  109.  
  110. inline int Query (int index) {
  111.     int sum = 0;
  112.     while (index > 0)
  113.         sum += BIT[index], index -= index & (- index);
  114.  
  115.     return sum;
  116. }
  117.  
  118. inline int Kth_smallest (int k) {
  119.     auto it = Heap.end(); -- it;
  120.     int st = 1, dr = *it; //fout << "\t\t" << st << " " << dr << ": ";
  121.     while (st < dr) {
  122.         int mid = (st + dr) / 2;
  123.         if (k <= Query (mid))
  124.             dr = mid;
  125.         else st = mid + 1;
  126.         //fout << st << " -> ";
  127.     }
  128.     //fout << "\n";
  129.  
  130.     return st;
  131. }
  132.  
  133.  
  134. int main () {
  135.     fin >> n;
  136.     for (int i = 1; i <= n; ++ i) {
  137.         fin >> task;
  138.         switch (task) {
  139.         case 1: {
  140.             fin >> val;
  141.             if (Heap.find (val) == Heap.end ())
  142.                 Update (val, 1);
  143.             Heap.insert (val);
  144.         }
  145.                 break;
  146.         case 2: {
  147.             fin >> val;
  148.             fout << (x = Kth_smallest (val)) << "\n";
  149.         }
  150.                 break;
  151.         case 3: {
  152.             fout << *(Heap.begin ()) << "\n";
  153.         }
  154.                 break;
  155.         case 4: {
  156.             Heap.erase (Heap.lower_bound (x));
  157.             if (Heap.find(x) == Heap.end())
  158.                 Update (x, - 1);
  159.         }
  160.                 break;
  161.         }
  162.         /*
  163.         fout << "\n\t";
  164.         for (auto it : Heap)
  165.             fout << it << " " << Query (it) << " | ";
  166.         fout << "\n";
  167.         */
  168.     }
  169.  
  170.     return 0;
  171. }
  172.  
  173. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  174. // Debug program: F5 or Debug > Start Debugging menu
  175.  
  176. // Tips for Getting Started:
  177. //   1. Use the Solution Explorer window to add/manage files
  178. //   2. Use the Team Explorer window to connect to source control
  179. //   3. Use the Output window to see build output and other messages
  180. //   4. Use the Error List window to view errors
  181. //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  182. //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Add Comment
Please, Sign In to add comment