Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fill(x,y) memset(x,y,sizeof(x))
- #define fi first
- #define se second
- #define sz(x) ((int) (x).size())
- #define all(x) (x).begin(), (x).end()
- #define sqr(x) ((x) * (x))
- #define sqrt(x) sqrt(abs(x))
- #define mp make_pair
- #define pb push_back
- typedef long long ll;
- typedef string s;
- typedef pair<int,int> pii;
- typedef pair<int,string> pis;
- typedef pair<string,int> psi;
- typedef pair<string,string> pss;
- typedef vector<int> vi;
- typedef vector<vi> vvi;
- typedef vector<pii> vpii;
- typedef vector<vector<ll> > matrix;
- const ll oo = 1e6;
- const double EPS = (1e-7);
- int dcmp(double x, double y) { return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1; }
- int tarr[400001];
- int res[100001];
- void build (int index ,int l , int r){
- if(l==r){
- tarr[index]= l;
- return ;
- }
- int mid = (l+r)/2;
- build(2*index,l,mid);
- build(2*index+1,mid+1,r);
- if(res[tarr[2*index]] >= res[tarr[2*index+1]]){
- tarr[index] = tarr[2*index];
- }
- else{
- tarr[index] = tarr[2*index+1];
- }
- }
- void update (int index , int start , int endd , int ind){
- if(start == ind && endd == ind ){
- return;
- }
- int mid=(start+endd)/2;
- if(ind <= mid) update(2*index,start,mid,ind);
- else update(2*index+1,mid+1,endd,ind);
- if(res[tarr[2*index]] >= res[tarr[2*index+1]]){
- tarr[index] = tarr[2*index];
- }
- else{
- tarr[index] = tarr[2*index+1];
- }
- }
- int main(){
- ios::sync_with_stdio(0);
- int inputs;
- cin >> inputs ;
- for(int i=0 ; i<inputs ; i++){
- memset(res, 0 ,sizeof res);
- int maxy=0,maxin=0,n,q,moment,tries=0;
- cin >> n >> q ;
- build(1, 0, n-1);
- for(int j=0 ; j<q ; j++){
- int x,p ; cin >> x >> p;
- x--;
- res[x] += p;
- update(1, 0, n-1, x);
- int comp = tarr[1];
- int maxi2 = res[comp];
- if(maxi2 > maxy){
- if(comp != maxin){
- tries++;
- moment=j;
- }
- maxin=comp;
- maxy = maxi2;
- }
- else if(maxi2 == maxy){
- if(comp < maxin){
- tries++;
- moment=j;
- maxin=comp;
- maxy = maxi2;
- }
- }
- }
- if(tries==1){
- cout<<0<<endl;
- }
- else{
- cout<<(moment+1)<<endl;
- }
- }
- }
- /*
- 1
- 5 6
- 1 -2
- 2 -2
- 3 -1
- 2 -1
- 3 0
- 4 0*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement