Advertisement
Zeinab_Hamdy

Untitled

Aug 26th, 2023 (edited)
1,083
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define nl "\n"
  4. #define fi first
  5. #define se second
  6. #define pb push_back
  7. #define ll long long
  8. #define ull unsigned ll
  9. #define RV  return void
  10. #define inf 2000000000
  11. #define sz(x) int(x.size())
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define Mini(x) *min_element(all(x))
  15. #define Maxi(x) *max_element(all(x))
  16. #define fixed(n) fixed << setprecision(n)
  17. // #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0))
  18. #define cin(v) for (auto&i:v) cin >> i;
  19. #define cout(v) for (auto&i:v) cout << i << " ";
  20. #define clr(memo, x) memset(memo, x, sizeof memo)
  21. #define updmin(a, b) a = min(a, b)
  22. #define updmax(a, b) a = max(a, b)
  23. #define vi vector < int >
  24. #define vl vector < ll >
  25. #define vc vector < char >
  26. #define vs vector < string >
  27. #define v2i vector < vector < int > >
  28. #define v2l vector < vector < int > >
  29. #define seti set < int >
  30. #define setl set < ll >
  31. #define mapii map < int , int >
  32. #define mapll map < ll , ll >
  33. #define mapli map < ll , int >
  34. #define mapci map < char , int >
  35. #define mapsi map < string , int >
  36. #define pll pair < ll , ll >
  37. #define pii pair < int , int >
  38. #define range(l,r,x) for(int i=l ; i < r ; i+=x)
  39. #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  40. vector < string > ternary= {"NO\n" , "YES\n"};
  41.  
  42. void  Zainab(){
  43.             #ifndef ONLINE_JUDGE
  44.               freopen("input.txt", "r", stdin);
  45.               freopen("output.txt", "w", stdout);
  46.             #endif
  47. }
  48.  
  49.  
  50. /*================================  Prblem solution  ================================ */
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. void myCode(){
  58.  
  59.     int n ;
  60.     cin >> n ;
  61.     vector < ll > v(n);
  62.     cin(v);
  63.    
  64.    
  65.     multiset < ll > vals , diff ;
  66.     for(auto& x : v ) vals.insert(x);
  67.    
  68.     ll pre = *vals.begin();
  69.     bool start = 0;
  70.    
  71.     for(auto& x : vals){
  72.         if(start) diff.insert(x - pre);
  73.         start =1;
  74.         pre = x;
  75.     }
  76.    
  77.     // for(auto& i : vals) cout << i << " ";
  78.     // cout << nl;
  79.    
  80.     // for(auto& i : diff) cout << i << " ";
  81.     // cout << nl;
  82.    
  83.     int q;       cin >> q;
  84.     while(q--){
  85.         int idx ,  val;
  86.         cin >> idx >> val ;
  87.         idx--;
  88.        
  89.         if( n == 1 ) {
  90.             cout << val << " " ;
  91.             continue;
  92.         }
  93.  
  94.         auto it = vals.find(v[idx]);
  95.         // vals.erase(it);
  96.         // cout << *next(it) << " ";
  97.        
  98.        
  99.         if( v[idx] == *vals.rbegin() ){  // last element  -> erase from right only
  100.             diff.erase( diff.find( v[idx] - *prev(it) ));
  101.         }
  102.        
  103.         else if(v[idx] == *vals.begin()){ // first element  -> erase from left only
  104.             diff.erase( diff.find( *next(it) - v[idx] ));
  105.         }
  106.        
  107.         else{  // left & right
  108.        
  109.             diff.erase( diff.find( v[idx] - *prev(it) ));
  110.             diff.erase( diff.find( *next(it) - v[idx] ));
  111.             diff.insert(*next(it) - *prev(it));
  112.         }
  113.        
  114.         vals.erase(it);
  115.         vals.insert(val);
  116.        
  117.         v[idx]= val;
  118.         it = vals.find(v[idx]);
  119.        
  120.        
  121.        
  122.         if( v[idx] == *vals.rbegin()){
  123.             diff.insert(  v[idx] - *prev(it) );
  124.         }
  125.         else if( v[idx] == *vals.begin()){
  126.             diff.insert ( *next(it) - v[idx] );
  127.         }
  128.        
  129.         else{
  130.             diff.insert( v[idx] - *prev(it) );
  131.             diff.insert( *next(it) - v[idx] );
  132.             diff.erase( diff.find(*next(it) - *prev(it)));
  133.         }
  134.      
  135.        
  136.         cout << *vals.rbegin() + *diff.rbegin() << " ";
  137.        
  138.        
  139.     }
  140.  
  141.     cout << nl;
  142.  
  143.    
  144. }
  145.  
  146.  
  147. int main(){
  148.                                    FastCode ;
  149.                               //      Zainab() ;
  150.  
  151.     int testCase=1;
  152.           cin >> testCase ;
  153.       for(int i=1 ; i<= testCase ; i++)
  154.         myCode();
  155.  
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement