Judge Bahosain was bored at ACM AmrahCPC 2016 as the winner of the contest had the first rank from the second hour until the end of the contest.
Bahosain is studying the results of the past contests to improve the problem sets he writes and make sure this won’t happen again.
Bahosain will provide you with the log file of each contest, your task is to find the first moment after which the winner of the contest doesn’t change.
The winner of the contest is the team with the highest points. If there’s more than one team with the same points, then the winner is the team with smallest team ID number.
Input
The first line of input contains a single integer T, the number of test cases.
The first line of each test case contains two space-separated integers N and Q (1 ≤ N, Q ≤ 105), the number of teams and the number of events in the log file. Teams are numbered from 1 to N.
Each of the following Q lines represents an event in the form: X P, which means team number X (1 ≤ X ≤ N) got P ( - 100 ≤ P ≤ 100, P ≠ 0) points. Note that P can be negative, in this case it represents an unsuccessful hacking attempt.
Log events are given in the chronological order.
Initially, the score of each team is zero.
Output
For each test case, if the winner of the contest never changes during the contest, print 0. Otherwise, print the number of the first event after which the winner of the contest didn’t change. Log events are numbered from 1 to Q in the given order.
Example
1
5 7
4 5
3 4
2 1
1 10
4 8
3 -5
4 2
5
题意:计分,每个队刚开始为0,找最后一直分最大的那一个事件
题解:线段树维护,区间更新,最上层的id就是最大的那一个,不相同就更换。
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define pi acos(-1) #define ll long long #define mod 1000000007 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 using namespace std; const double eps=1e-8; const int N=100000+5,maxn=305,inf=0x3f3f3f3f; struct { int v; int id; }e[N<<2]; void pushup(int rt) { e[rt].v=max(e[rt<<1].v,e[rt<<1|1].v); if(e[rt<<1].v>=e[rt<<1|1].v)e[rt].id=e[rt<<1].id; else e[rt].id=e[rt<<1|1].id; } void btree(int l,int r,int rt) { if(l==r) { e[rt].id=l; e[rt].v=0; return ; } int m=(l+r)>>1; btree(ls); btree(rs); pushup(rt); } void update(int x,int u,int l,int r,int rt) { if(l==r) { e[rt].v+=u; return ; } int m=(l+r)>>1; if(x<=m)update(x,u,ls); else update(x,u,rs); pushup(rt); } int main() { ios::sync_with_stdio(false); cin.tie(0); int t,n,k; cin>>t; while(t--){ cin>>n>>k; btree(1,n,1); int ans=0; int now=e[1].id; for(int i=1;i<=k;i++) { int a,b; cin>>a>>b; update(a,b,1,n,1); if(now!=e[1].id)ans=i,now=e[1].id; } cout<<ans<<endl; } return 0; }