Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int long long
- #define fi first
- #define se second
- #define pb push_back
- #define vi vector<int>
- #define ii pair<int,int>
- #define iii pair<int, ii>
- #define iiii pair<ii, ii>
- #define Task "test"
- #define all(x) x.begin(),x.end()
- using namespace std;
- const int N = 3e5+5;
- const int inf = 1e9+9;
- const int mod = 1e9+7;
- const int base = 33;
- void readfile(){
- if (fopen(Task".inp","r")){
- freopen(Task".inp","r",stdin);
- //freopen(Task".out","w",stdout);
- }
- }
- int n, k;
- int a[N];
- struct que{
- int id, round, stt;
- };
- bool cmp(que a, que b){
- return a.round < b.round;
- }
- que tv[N];
- void solve(){
- cin >> n >> k;
- int id_Max = 0;
- vector<int> ans(n+1,0);
- vector<int> kq(k+1,0);
- for(int i=1; i<=n; i++){
- cin >> a[i];
- if (a[i]==n){
- id_Max = i;
- }
- }
- for(int i=1; i<=k; i++){
- cin >> tv[i].id >> tv[i].round;
- tv[i].stt = i;
- }
- sort(tv+1,tv+1+k,cmp);
- int cur = 1;
- deque<int> qu;
- for(int i=1; i<=n; i++) qu.push_back(i);
- //preproc
- for(int i=1; i<id_Max; i++){
- int fir = qu.front(); qu.pop_front();
- int sec = qu.front(); qu.pop_front();
- if (a[fir] > a[sec]){
- qu.push_front(fir);
- qu.push_back(sec);
- ans[fir]++;
- }
- else{
- qu.push_front(sec);
- qu.push_back(fir);
- ans[sec]++;
- }
- while (cur <= k && tv[cur].round==i){
- kq[tv[cur].stt] = ans[tv[cur].id];
- cur++;
- }
- }
- while (cur<=k){
- if (tv[cur].id == id_Max){
- kq[tv[cur].stt] = ans[tv[cur].id] + tv[cur].round - id_Max + 1;
- }
- else{
- kq[tv[cur].stt] = ans[tv[cur].id];
- }
- cur++;
- }
- for(int i=1; i<=k; i++) cout << kq[i] << '\n';
- }
- void proc(){
- int q;
- cin >> q;
- //q=1;
- while (q--){
- solve();
- }
- }
- signed main()
- {
- readfile();
- proc();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment