Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <stack>
- std::ifstream fin ("nrapp.in");
- std::ofstream fout ("nrapp.out");
- int n, q, x;
- char ch;
- std::vector <int> Hist, St, Dr;
- std::stack <int> Stiva;
- int main () {
- fin >> n;
- Hist.resize (n + 1), St.resize (n + 1), Dr.resize (n + 1);
- for (int i = 1; i <= n; ++ i)
- fin >> Hist[i];
- Stiva.push (0);
- for (int i = 1; i <= n; ++ i) {
- while ((int)Stiva.size () > 1 && Hist[i] <= Hist[Stiva.top ()]) Stiva.pop ();
- St[i] = Stiva.top (), Stiva.push (i);
- }
- Stiva = std::stack <int> ();
- Stiva.push (n + 1);
- for (int i = n; i >= 1; -- i) {
- while ((int)Stiva.size () > 1&& Hist[i] <= Hist[Stiva.top ()]) Stiva.pop ();
- Dr[i] = Stiva.top (), Stiva.push (i);
- }
- fin >> q;
- for (int i = 1; i <= q; ++ i)
- fin >> ch >> x, fout << (ch == 'S' ? St[x] : Dr[x]) << "\n";
- fin.close (), fout.close ();
- return 0;
- }
Add Comment
Please, Sign In to add comment