warriorscats

dfdf

Mar 28th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <stack>
  4.  
  5.  
  6. std::ifstream fin ("nrapp.in");
  7. std::ofstream fout ("nrapp.out");
  8.  
  9. int n, q, x;
  10. char ch;
  11. std::vector <int> Hist, St, Dr;
  12. std::stack <int> Stiva;
  13.  
  14.  
  15.  
  16. int main () {
  17. fin >> n;
  18. Hist.resize (n + 1), St.resize (n + 1), Dr.resize (n + 1);
  19. for (int i = 1; i <= n; ++ i)
  20. fin >> Hist[i];
  21.  
  22. Stiva.push (0);
  23. for (int i = 1; i <= n; ++ i) {
  24. while ((int)Stiva.size () > 1 && Hist[i] <= Hist[Stiva.top ()]) Stiva.pop ();
  25. St[i] = Stiva.top (), Stiva.push (i);
  26. }
  27.  
  28. Stiva = std::stack <int> ();
  29.  
  30. Stiva.push (n + 1);
  31. for (int i = n; i >= 1; -- i) {
  32. while ((int)Stiva.size () > 1&& Hist[i] <= Hist[Stiva.top ()]) Stiva.pop ();
  33. Dr[i] = Stiva.top (), Stiva.push (i);
  34. }
  35.  
  36. fin >> q;
  37. for (int i = 1; i <= q; ++ i)
  38. fin >> ch >> x, fout << (ch == 'S' ? St[x] : Dr[x]) << "\n";
  39.  
  40. fin.close (), fout.close ();
  41. return 0;
  42. }
Add Comment
Please, Sign In to add comment