Manioc

fila beauty

Aug 13th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int raiz = 1200;
  6.  
  7. struct bucket{
  8.     int maior; //change
  9.     vector<int> valores;
  10. };
  11.  
  12. vector<bucket> arr(1);
  13.  
  14. void print(){
  15.     for(int i = 0; i < arr.size(); i++){
  16.         cout << "bucket #" << (i+1) <<" (" << arr[i].maior << ") -> ";
  17.         for(int j = 0; j < arr[i].valores.size(); j++){
  18.             cout << arr[i].valores[j] << " \n"[j == arr[i].valores.size()-1];
  19.         }
  20.     }
  21. }
  22.  
  23. void change_max(bucket &actual){
  24.     actual.maior = 0;
  25.     for(int i = 0; i < actual.valores.size(); i++){
  26.         actual.maior = max(actual.maior, actual.valores[i]);
  27.     }    
  28. }
  29.  
  30. void split(bucket &actual, int idx){
  31.     bucket novo;
  32.     novo.valores.assign(actual.valores.begin()+ raiz, actual.valores.end());
  33.     actual.valores.resize(raiz);
  34.     change_max(novo);
  35.     change_max(actual);
  36.     arr.insert(arr.begin()+idx + 1, novo);
  37. }
  38.  
  39. void insert(int val, int idx, int bucket_idx = 0){
  40.     for(int i = bucket_idx; i < arr.size(); i++){
  41.         bucket &actual = arr[i];
  42.         if(idx <= actual.valores.size()){
  43.             actual.valores.insert(actual.valores.begin() + idx, val);
  44.             change_max(actual);
  45.             if(actual.valores.size() >= 2*raiz) split(actual, i);
  46.             //print();
  47.             return;
  48.         }
  49.         idx -= actual.valores.size();
  50.     }
  51. }
  52.  
  53. pair<int, int> at(int idx){
  54.     for(int i = 0; i < arr.size(); i++){
  55.         bucket &actual = arr[i];
  56.         //cout << idx << " " << actual.valores.size() << endl;
  57.         if(idx < actual.valores.size()){
  58.             return make_pair(i, idx);
  59.         }
  60.  
  61.         idx -= actual.valores.size();
  62.     }
  63. }
  64. void erase(int idx){
  65.     pair<int,int> pos = at(idx);
  66.     bucket &actual = arr[pos.first];
  67.  
  68.     actual.valores.erase(actual.valores.begin()+pos.second);
  69.     if(actual.valores.size() == 0) arr.erase(arr.begin()+pos.first);
  70.     else change_max(actual);
  71. }
  72.  
  73. int query(int idx, int val){
  74.     pair<int, int> pos = at(idx);
  75.  
  76.     //cout << "fase 1\n";
  77.     while(pos.second >= 0){
  78.         if(arr[pos.first].valores[pos.second] > val) return idx;
  79.         pos.second--;
  80.         idx--;
  81.     }
  82.  
  83.     //cout << "fase 2\n";
  84.     pos.first--;
  85.     while(pos.first >= 0){
  86.         if(arr[pos.first].maior > val) break;
  87.         idx -= arr[pos.first].valores.size();
  88.         pos.first--;
  89.     }
  90.  
  91.     //cout << "fase 3\n";
  92.     if(idx > 0) pos = at(idx);
  93.     while(pos.second >= 0){
  94.         if(arr[pos.first].valores[pos.second] > val) return idx;
  95.         pos.second--;
  96.         idx--;
  97.     }
  98.  
  99.     return -1;
  100. }
  101. int main(){
  102.     int n; scanf("%d", &n);
  103.     for(int i = 0; i < n; i++){
  104.         int val; scanf("%d", &val);
  105.         insert(val, i);
  106.     }
  107.     int q; scanf("%d", &q);
  108.     while(q--){
  109.         int type, pos, val; scanf("%d %d %d", &type, &pos, &val);
  110.         if(type == 0){
  111.             insert(val, pos);
  112.         }else{
  113.             pos--;
  114.             pair<int, int> idx = at(pos);
  115.             //cout << idx.first << " " << idx.second << endl;
  116.             printf("%d\n", query(pos-1, arr[idx.first].valores[idx.second] + val) + 1);
  117.         }
  118.     }
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment