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

Input
1
5 7
4 5
3 4
2 1
1 10
4 8
3 -5
4 2
Output
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-;
const int N=+,maxn=,inf=0x3f3f3f3f; struct {
int v;
int id;
}e[N<<]; void pushup(int rt)
{
e[rt].v=max(e[rt<<].v,e[rt<<|].v);
if(e[rt<<].v>=e[rt<<|].v)e[rt].id=e[rt<<].id;
else e[rt].id=e[rt<<|].id;
}
void btree(int l,int r,int rt)
{
if(l==r)
{
e[rt].id=l;
e[rt].v=;
return ;
}
int m=(l+r)>>;
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)>>;
if(x<=m)update(x,u,ls);
else update(x,u,rs);
pushup(rt);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int t,n,k;
cin>>t;
while(t--){
cin>>n>>k;
btree(,n,);
int ans=;
int now=e[].id;
for(int i=;i<=k;i++)
{
int a,b;
cin>>a>>b;
update(a,b,,n,);
if(now!=e[].id)ans=i,now=e[].id;
}
cout<<ans<<endl;
}
return ;
}

Gym - 101102C线段树的更多相关文章

  1. K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

    题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...

  2. 【金色】种瓜得瓜,种豆得豆 Gym - 102072H (线段树)

    题目链接:https://cn.vjudge.net/problem/Gym-102072H 题目大意:中文题目 具体思路:通过两棵线段树来维护,第一棵线段树来维护当前坐标的点的日增长速度(默认每一年 ...

  3. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  4. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  5. Codeforces Gym 100513F F. Ilya Muromets 线段树

    F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...

  6. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  7. 【线段树】BAPC2014 E Excellent Engineers (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  8. Codeforces Gym 100733J Summer Wars 线段树,区间更新,区间求最大值,离散化,区间求并

    Summer WarsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...

  9. 【拓扑排序】【线段树】Gym - 101102K - Topological Sort

    Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...

随机推荐

  1. 关于Trie KMP AC自动机

    个人认为trie,KMP,AC自动机是思想非常明确的,AC自动机的性质是与KMP算法的思想类似的(失配后跳转) 而KMP是线性的,AC自动机是在tire树上跑KMP,为方便那些不会用指针的小朋友(我也 ...

  2. Chrome 下动画卡顿问题的另一种可能

    [现象] 动画出现了明显的卡顿,且仅仅出现在 chrome 中. [原因排查] 一开始使用了css动画的时候已经出现了卡顿.找到如下的文章:CSS3 动画卡顿解决方案.深入浏览器理解CSS anima ...

  3. 1.WP8.1开发_去除闪动效果,直接进入首页

    有时候希望打开软件的时候,不出现闪屏,而直接进入到第一个页面. 把第一个页面作为闪屏,可以制作一些进度条  和动画等... 很简单,有两种方法去除闪屏. 1.删除项目下 Assets 文件夹里面的Sp ...

  4. Layoutinlater 转

    http://blog.csdn.net/guolin_blog/article/details/12921889

  5. Spring的IoC容器

    Spring是一个轻量级的Java开发框架,其提供的两大基础功能为IoC和AOP,其中IoC为依赖反转(Inversion of Control).IOC容器的基本理念就是"为别人服务&qu ...

  6. XJOI1657&Codevs1255搭积木【树状动规】

    搭积木 一种积木搭建方式,高为H的积木,最底层有M个积木,每一层的积木数是他的低一层的积木数+1或-1.总共有N个积木.(且每行积木数不超过10)比如上图N=13 H=6 M=2. 输入格式: 第一行 ...

  7. nodejs oj在线笔试应对方案(讲几种输入处理方法)

    最近参加了一些线上笔试.但是...我不是学计算机的,只会js不会c++,java,c(好吧都学过,不过忘了).可怕的是我也没学过nodejs,怎么 办,怎么办.node不就是用的js吗?所以只用学会标 ...

  8. Asp.NetCore之组件写法

    本章内容和大家分享的是Asp.NetCore组件写法,在netcore中很多东西都以提供组件的方式来使用,比如MVC架构,Session,Cache,数据库引用等: 这里我也通过调用验证码接口来自定义 ...

  9. 转接口IC GM7150BN/ GM7150BC:CVBS转BT656芯片 低功耗NTSC/PAL 视频解码器

    1 概述    GM7150 是一款9 位视频输入预处理芯片,该芯片采用CMOS 工艺,通过I2C 总线与PC 或DSP 相连构成应用系统.    它内部包含1 个模拟处理通道,能实现CVBS.S-V ...

  10. kafka 入门笔记 #1

    kafka 入门笔记(#1) 单机测试 下载版本,解压 tar -xzf kafka_2.11-0.10.1.1.tgz cd kafka_2.11-0.10.1.1 启动服务 Kafka用到了Zoo ...