# Untitled

unknown
plain_text
7 days ago
2.4 kB
2
Indexable
Never
```#include <bits/stdc++.h>
using namespace std ;
#define ll long long
#define For(n) for(long long i = 0; i < n; i++)
#define endl '\n'
#define int long long
struct node {
int id;
int score;
};
class cmp{
public :
bool operator() (node n1,node n2){
if(n1.score<n2.score)return 1;
else if(n1.score>n2.score)return 0;
else return n1.id>n2.id;
}
};
void solve(){
map<int,int> mp;
int n,q,x,y;;cin>>n>>q;
std:: priority_queue<node ,std::vector<node>,cmp>prevscores;
cin>>x>>y;mp[x]+=y;
int ans=0,mxscore = y,winnerteam=x;
for(int i=1;i<q;i++){
cin>>x>>y;
mp[x]+= y;
if(x==winnerteam && mp[x]<mxscore){// if winner team got fuckedup
mxscore = mp[x];// update mxscore
if(prevscores.empty())continue;
if(mxscore<prevscores.top().score){
ans = i;
mxscore = prevscores.top().score;
winnerteam = prevscores.top().id;
prevscores.pop();
}
else if(mxscore==prevscores.top().score){
if(x>prevscores.top().id){
ans = i;
mxscore = prevscores.top().score;
winnerteam = prevscores.top().id;
prevscores.pop();
}
}
}
else if(mp[x]>mxscore){
if(winnerteam!=x)ans = i;
node n; n.id=x,n.score=mxscore;
prevscores.push(n);
winnerteam = x;
mxscore = mp[x];
}
else if(mp[x] == mxscore){
if(x<winnerteam){
ans = i;
winnerteam = x;
}
else {
node n;n.id=x,n.score=mxscore;
prevscores.push(n);
}
}
else {
node n;n.id=x,n.score=mxscore;
prevscores.push(n);
}
// cout << winnerteam<<' '<<mxscore<<' '<<' '<<prevscores.top().first<<' '<<prevscores.top().second<<endl;
}
if(ans)ans++;
// if(ans == n) ans --;
cout << ans << endl;
}
signed main ()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t =1;
cin>>t;
while (t--){
solve();
}
}```