HoangMinhNguyen

https://codeforces.com/contest/1719/problem/C

Aug 16th, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define fi first
  4. #define se second
  5. #define pb push_back
  6. #define vi vector<int>
  7. #define ii pair<int,int>
  8. #define iii pair<int, ii>
  9. #define iiii pair<ii, ii>
  10. #define Task "test"
  11. #define all(x) x.begin(),x.end()
  12.  
  13. using namespace std;
  14. const int N = 3e5+5;
  15. const int inf = 1e9+9;
  16. const int mod = 1e9+7;
  17. const int base = 33;
  18.  
  19. void readfile(){
  20.     if (fopen(Task".inp","r")){
  21.         freopen(Task".inp","r",stdin);
  22.         //freopen(Task".out","w",stdout);
  23.     }
  24. }
  25.  
  26. int n, k;
  27. int a[N];
  28.  
  29. struct que{
  30.     int id, round, stt;
  31. };
  32.  
  33. bool cmp(que a, que b){
  34.     return a.round < b.round;
  35. }
  36.  
  37. que tv[N];
  38.  
  39. void solve(){
  40.     cin >> n >> k;
  41.     int id_Max = 0;
  42.     vector<int> ans(n+1,0);
  43.     vector<int> kq(k+1,0);
  44.     for(int i=1; i<=n; i++){
  45.         cin >> a[i];
  46.         if (a[i]==n){
  47.             id_Max = i;
  48.         }
  49.     }
  50.     for(int i=1; i<=k; i++){
  51.         cin >> tv[i].id >> tv[i].round;
  52.         tv[i].stt = i;
  53.     }
  54.     sort(tv+1,tv+1+k,cmp);
  55.     int cur = 1;
  56.     deque<int> qu;
  57.     for(int i=1; i<=n; i++) qu.push_back(i);
  58.     //preproc
  59.     for(int i=1; i<id_Max; i++){
  60.         int fir = qu.front(); qu.pop_front();
  61.         int sec = qu.front(); qu.pop_front();
  62.         if (a[fir] > a[sec]){
  63.             qu.push_front(fir);
  64.             qu.push_back(sec);
  65.             ans[fir]++;
  66.         }
  67.         else{
  68.             qu.push_front(sec);
  69.             qu.push_back(fir);
  70.             ans[sec]++;
  71.         }
  72.         while (cur <= k && tv[cur].round==i){
  73.             kq[tv[cur].stt] = ans[tv[cur].id];
  74.             cur++;
  75.         }
  76.     }
  77.     while (cur<=k){
  78.         if (tv[cur].id == id_Max){
  79.             kq[tv[cur].stt] = ans[tv[cur].id] + tv[cur].round - id_Max + 1;
  80.         }
  81.         else{
  82.             kq[tv[cur].stt] = ans[tv[cur].id];
  83.         }
  84.         cur++;
  85.     }
  86.     for(int i=1; i<=k; i++) cout << kq[i] << '\n';
  87. }
  88.  
  89. void proc(){
  90.     int q;
  91.     cin >> q;
  92.     //q=1;
  93.     while (q--){
  94.         solve();
  95.     }
  96. }
  97.  
  98. signed main()
  99. {
  100.     readfile();
  101.     proc();
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment