ash_sinha

Untitled

Oct 13th, 2022
771
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. #include <ext/pb_ds/assoc_container.hpp>
  3. using namespace __gnu_pbds;
  4. using namespace std;
  5.  
  6. #define ff              first
  7. #define ss              second
  8. #define int             long long
  9. #define pb              push_back
  10. #define mp              make_pair
  11. #define pii             pair<int,int>
  12. #define vi              vector<int>
  13. #define mii             map<int,int>
  14. #define pqb             priority_queue<int>
  15. #define pqs             priority_queue<int,vi,greater<int> >
  16. #define setbits(x)      __builtin_popcountll(x)
  17. #define zrobits(x)      __builtin_ctzll(x)
  18. #define mod             1000000007
  19. #define inf             1e18
  20. #define ps(x,y)         fixed<<setprecision(y)<<x
  21. #define mk(arr,n,type)  type *arr=new type[n];
  22. #define w(x)            int x; cin>>x; while(x--)
  23. mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());
  24. #define fastio          ios_base::sync_with_stdio(0); cin.tie(0);
  25.  
  26. void update(stack<pii> &st,set<int> &pos,vi &a,int idx)
  27. {
  28.     stack<pii> st1;
  29.     while(!st.empty() && a[st.top().ff]<a[idx]){
  30.         auto temp=st.top(); st.pop();
  31.         temp.ss++;
  32.         pos.erase(temp.ff);
  33.         if(temp.ss<2){
  34.             st1.push(temp);
  35.         }
  36.     }
  37.     st.push({idx,0});
  38.     pos.insert(idx);
  39.     while(!st1.empty()){
  40.         auto temp=st1.top();st1.pop();
  41.         pos.insert(temp.ff);
  42.         st.push(temp);
  43.     }
  44. }
  45. int32_t main()
  46. {
  47.     vi a= {1,2,3,4,5,6,7};
  48.     int x=1;
  49.     set<int> pos;
  50.     stack<pii> st,st1;
  51.     int n=a.size();
  52.     vi ans(n,-1);
  53.     for(int i=n-1;i>=0;i--){
  54.         while(!st.empty() && a[st.top().ff]<a[i]+x){
  55.             auto  temp=st.top();
  56.             pos.erase(temp.ff);
  57.             st1.push(temp);
  58.             st.pop();
  59.         }
  60.         if(st.size()>=2){
  61.             auto it=pos.begin();it++;
  62.             ans[i]=a[*(it)];
  63.         }
  64.      
  65.         while(!st1.empty()){
  66.             auto temp=st1.top();
  67.             st.push(temp);
  68.             pos.insert(temp.ff);
  69.  
  70.             st1.pop();
  71.         }
  72.         update(st,pos,a,i);
  73.     }
  74.     for(auto x:ans) cout<<x<<" ";  
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment