Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- using namespace __gnu_pbds;
- using namespace std;
- #define ff first
- #define ss second
- #define int long long
- #define pb push_back
- #define mp make_pair
- #define pii pair<int,int>
- #define vi vector<int>
- #define mii map<int,int>
- #define pqb priority_queue<int>
- #define pqs priority_queue<int,vi,greater<int> >
- #define setbits(x) __builtin_popcountll(x)
- #define zrobits(x) __builtin_ctzll(x)
- #define mod 1000000007
- #define inf 1e18
- #define ps(x,y) fixed<<setprecision(y)<<x
- #define mk(arr,n,type) type *arr=new type[n];
- #define w(x) int x; cin>>x; while(x--)
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- #define fastio ios_base::sync_with_stdio(0); cin.tie(0);
- void update(stack<pii> &st,set<int> &pos,vi &a,int idx)
- {
- stack<pii> st1;
- while(!st.empty() && a[st.top().ff]<a[idx]){
- auto temp=st.top(); st.pop();
- temp.ss++;
- pos.erase(temp.ff);
- if(temp.ss<2){
- st1.push(temp);
- }
- }
- st.push({idx,0});
- pos.insert(idx);
- while(!st1.empty()){
- auto temp=st1.top();st1.pop();
- pos.insert(temp.ff);
- st.push(temp);
- }
- }
- int32_t main()
- {
- vi a= {1,2,3,4,5,6,7};
- int x=1;
- set<int> pos;
- stack<pii> st,st1;
- int n=a.size();
- vi ans(n,-1);
- for(int i=n-1;i>=0;i--){
- while(!st.empty() && a[st.top().ff]<a[i]+x){
- auto temp=st.top();
- pos.erase(temp.ff);
- st1.push(temp);
- st.pop();
- }
- if(st.size()>=2){
- auto it=pos.begin();it++;
- ans[i]=a[*(it)];
- }
- while(!st1.empty()){
- auto temp=st1.top();
- st.push(temp);
- pos.insert(temp.ff);
- st1.pop();
- }
- update(st,pos,a,i);
- }
- for(auto x:ans) cout<<x<<" ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment