fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=1e6+10;
  4. stack<int> a;
  5. int arr[N],ans[N],brr[N];
  6. int main() {
  7. int n;
  8. cin>>n;
  9. for(int i=1;i<=n;i++){
  10. cin>>arr[i]>>brr[i];
  11. }
  12. for(int i=n;i>=1;i--){
  13. while(!a.empty()&&arr[a.top()]<=arr[i]){
  14. a.pop();
  15. }
  16. if(a.empty()){
  17. ans[i]=0;
  18. }else{
  19. ans[i]=a.top();
  20. }
  21. a.push(i);
  22. }
  23. int maxx=0;
  24. for(int i=1;i<=n;i++){
  25. cout<<ans[i];
  26. }
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0.01s 7636KB
stdin
5
1 2
1 3
2 2
2 5
1 4
stdout
33000