加载中...

814 C Fighting Tournament

写的时候思路想到了,但是不怎么会维护。 这儿贴一个比较好理解的维护方式,用的双端队列。

#include<bits/stdc++.h>
using namespace std;
#define int long long 
#pragma GCC optimize(3)
typedef pair<int, int>PII;
#define pb push_back
const int N = 1e5 + 10;
PII a[N];
vector<int>v[N];
void cf(){
	int n, q;
	cin >> n >> q;
	for (int i = 1; i <= n; i++)v[i].clear();
	int ma = 0;
	deque<PII>q2;
	for (int i = 1; i <= n; i++){
		cin >> a[i].first;
		a[i].second = i;
		q2.push_back(a[i]);
		ma = max(ma, a[i].first);
	}
	int ti = 0;//回合数
	while (1){
		if (q2.front().first == ma)break;
		ti++;
		PII x = q2.front();
		q2.pop_front();
		PII y = q2.front();
		q2.pop_front();
		if (x.first>y.first){
			q2.push_front(x);
			q2.push_back(y);
			v[x.second].pb(ti);
		}
		else if (x.first<y.first){
			q2.push_back(x);
			q2.push_front(y);
			v[y.second].pb(ti);
		}
	}
	int id = q2.front().second;//最大元素的id
	while (q--){
		int x, y;
		cin >> x >> y;
		if (x == id){
			if (ti){//第二到第一的过程也胜利了一次
				if (y<ti)cout << 0 << endl;
				else cout << y - ti + 1 << endl;
			}
			else{//始终在第一位
				cout << y << endl;
			}
		}
		else if (v[x].empty()){
			cout << 0 << endl;
		}
		else{
			int idx = upper_bound(v[x].begin(), v[x].end(), y) - v[x].begin();
			cout << idx << endl;
		}
	}
}
signed main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int _ = 1;
	cin >> _;
	while (_--){
		cf();
	}
	return 0;
}
posted @ 2022-08-21 14:56  英雄不问出处c  阅读(54)  评论(0)    收藏  举报