IMohammedNasr

Untitled

Jul 5th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define nl '\n'
  4. #define cin(v)        \
  5.     for (auto &i : v) \
  6.         cin >> i;
  7. #define cout(v)       \
  8.     for (auto &i : v) \
  9.         cout << i << " "; \
  10.         cout<<nl;
  11. #define br cout << '\n';
  12. #define ll long long
  13. #define ull unsigned long long
  14. #define all(v) v.begin(), v.end()
  15. #define rall(v) v.rbegin(), v.rend()
  16. #define MOD 1000000007
  17. #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" \
  18.                   << "\n";
  19.  
  20. void Warding()
  21. {
  22.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  23. #ifndef ONLINE_JUDGE
  24.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  25. #endif
  26. }
  27.  
  28.  
  29. void solve()
  30. {
  31.     ll n, q; cin>>n>>q;
  32.     deque<ll> deq;
  33.     map<ll, pair<ll,ll>> m;
  34.     for(int i=0; i<n; i++){
  35.         ll x; cin>>x;
  36.         deq.push_back(x);
  37.     }
  38.     for(int i=1; i<=n*2; i++){
  39.         ll f = deq.front();
  40.         deq.pop_front();
  41.         ll s = deq.front();
  42.         deq.pop_front();
  43.         m[i] = {f,s};
  44.         if(f>s){
  45.             deq.push_front(f);
  46.             deq.push_back(s);
  47.         }else{
  48.             deq.push_front(s);
  49.             deq.push_back(f);
  50.         }
  51.     }
  52.     while(q--){
  53.         ll target; cin>>target;
  54.         if(target<=n)
  55.         cout<<m[target].first<<' '<<m[target].second<<'\n';
  56.         else
  57.         cout<<m[(target - n) % (n-1)  + n].first<<" "<<m[(target - n) % (n-1)  + n].second<<'\n';
  58.     }
  59. }
  60.  
  61. int main()
  62. {
  63.     Warding();
  64.     int t = 1;
  65.     // cin >> t;
  66.     while (t--)
  67.     {
  68.         solve();
  69.     }
  70.     Time
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment