Advertisement
999ms

Untitled

Apr 12th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define gs(_obj_) (int)(_obj_.size())
  3. using namespace std;
  4. using ll = long long;
  5.  
  6. vector<vector<int>> v;
  7. vector<int> ans;
  8. const int sz = 1024 + 256;
  9.  
  10. void insert_at_pos(vector<int> & a, size_t ind, int val){  
  11.   a.push_back(val);
  12.   if(ind < 0) {
  13.       while(true);
  14.   }
  15.   for(size_t i = a.size() - 1; i > ind; i--){
  16.     swap(a[i], a[i-1]);
  17.   }
  18. }
  19.  
  20. void get_sub_ans(vector<int> & a, int & answer, int f, int t){
  21.   for(int i = f; i <= t; i++){
  22.     answer = min(answer, a[i]);
  23.   }
  24. }
  25.  
  26. void rebuild(){
  27.   vector<int> tot;
  28.   for(size_t i = 0; i < v.size(); i++){
  29.     for(int val : v[i]){
  30.       tot.push_back(val);
  31.     }
  32.     v[i].clear();
  33.   }
  34.   v.clear();
  35.   ans.clear();
  36.   for(size_t i = 0; i < tot.size(); i += sz){
  37.     vector<int> nxt;
  38.     for(size_t j = i; j < min(tot.size(), i + sz); j++){
  39.       nxt.push_back(tot[j]);
  40.     }
  41.     v.push_back(nxt);
  42.     ans.push_back(*min_element(nxt.begin(), nxt.end()));
  43.   }
  44. }
  45.  
  46. int main() {
  47.   ios_base::sync_with_stdio(false);
  48.   cin.tie(nullptr);
  49.   int q;
  50.   cin>>q;
  51.   v = {{}};
  52.   ans = {2000'000'000};
  53.   for(int itr = 1; itr <= q; itr++){
  54.     if(itr % sz == 0) {
  55.       rebuild();
  56.     }
  57.     char type;
  58.     cin>>type;
  59.     if(type == '+') {
  60.       size_t ind;
  61.       int val;
  62.       cin>>ind>>val;
  63.       size_t cur = 0;
  64.       for(size_t i = 0; i < v.size(); i++){
  65.         if(cur + v[i].size() > ind || i == v.size() - 1){
  66.           insert_at_pos(v[i], ind - cur, val);
  67.           ans[i] = min(ans[i], val);
  68.           break;
  69.         } else {
  70.           cur += v[i].size();
  71.         }
  72.       }
  73.     } else {
  74.       int answer = 2e9;
  75.       int l, r;
  76.       cin>>l>>r;
  77.       l--,r--;
  78.       int cur_l = 0, cur_r;
  79.       for(size_t i = 0; i < v.size(); i++){
  80.         cur_r = cur_l + v[i].size() - 1;
  81.         if(l <= cur_l && r >= cur_r){
  82.           answer = min(answer, ans[i]);
  83.         } else {
  84.           int real_l = max(0, l - cur_l), real_r = min(gs(v[i]) - 1, r - cur_l);
  85.           get_sub_ans(v[i], answer, real_l, real_r);
  86.         }
  87.         cur_l = cur_r + 1;
  88.       }
  89.       cout<<answer<<"\n";
  90.     }
  91.   }
  92.  
  93.   return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement