Kattis - gcpc (treap模板)
ne hundred years from now, in 21172117, the International Collegiate Programming Contest (of which the NCPC is a part) has expanded significantly and it is now the Galactic Collegiate Programming Contest (GCPC).
This year there are nn teams in the contest. The teams are numbered 1,2,…,n1,2,…,n, and your favorite team has number 11.
Like today, the score of a team is a pair of integers (a,b)(a,b)where aa is the number of solved problems and bb is the total penalty of that team. When a team solves a problem there is some associated penalty (not necessarily calculated in the same way as in the NCPC – the precise details are not important in this problem). The total penalty of a team is the sum of the penalties for the solved problems of the team.
Consider two teams t1t1 and t2t2 whose scores are (a1,b1)(a1,b1) and (a2,b2)(a2,b2). The score of team t1t1 is better than that of t2t2if either a1>a2a1>a2, or if a1=a2a1=a2 and b1<b2b1<b2. The rank of a team is k+1k+1 where kk is the number of teams whose score is better.
You would like to follow the performance of your favorite team. Unfortunately, the organizers of GCPC do not provide a scoreboard. Instead, they send a message immediately whenever a team solves a problem.
Input
The first line of input contains two integers nn and mm, where 1≤n≤1051≤n≤105 is the number of teams, and 1≤m≤1051≤m≤105 is the number of events.
Then follow mm lines that describe the events. Each line contains two integers tt and pp (1≤t≤n1≤t≤n and 1≤p≤10001≤p≤1000), meaning that team tt has solved a problem with penalty pp. The events are ordered by the time when they happen.
Output
Output mm lines. On the ii’th line, output the rank of your favorite team after the first ii events have happened.
Sample Input 1
3 4
2 7
3 5
1 6
1 9 Sample Output 1
2
3
2
1
题意:好多队伍比赛,按照事件发生顺序给你队伍AC题目的信息,x队AC一道题,罚时为y,每有一个队过题输出编号为1的队伍的排名
我们每当每个队A题的时候把他原来状态删除,把新状态加入到treap.由于可能有好几个队A题数罚时都一样,这样按照队伍编号升序
我们每个节点上我们设置有多个队伍就行了
#include <bits/stdc++.h>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
const ll mod=;
ll powmod(ll a,ll b) {ll res=;a%=mod; assert(b>=); for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
const int maxn=1e5+;
struct Node
{
Node *ch[];//0 左儿子(名次靠前) 1右儿子(名次靠后)
int r;//随机优先级,数值越大优先级越高
int v;//值
int s;//以他为根的子树上队伍数的个数
int num;//当前节点上队伍数目(有队伍会题数罚时都相同)一个节点可以右多个队伍
int tim;//罚时
Node(int v,int tim):v(v),tim(tim){ch[]=ch[]=NULL;r=((rand()<<)|rand());s=;num=; }
inline int cmp(int x,int xtim) const {//比较函数 相等-1 名次靠前0(左子树) 名次靠后 1(右子树)
if(x==v&&tim==xtim) return -;//相等
if(x==v) return xtim<tim?:;//题数相同罚时少的优先
return x>v?:;
}
inline void maintain() {
s=num;//当前节点子树个数
if(ch[]!=NULL) s+=ch[]->s;//加上自己左子树
if(ch[]!=NULL) s+=ch[]->s;//加上自己右子树
}
};
void rotate(Node* &o,int d) {//旋转
Node* k=o->ch[d^];o->ch[d^]=k->ch[d];k->ch[d]=o;
o->maintain();
k->maintain();
o=k;
}
void insert(Node* &o,int x,int tim) {//将 过了x题罚时tim的队伍插入到treap
if(o==NULL) {
o=new Node(x,tim);
}
else {
int d;
if(x>o->v) {//题数多 查到左子树
d=;
}
else if(x==o->v) {//题数一样
if(tim<o->tim) d=;//罚时少 左子树
else d=;//罚时多 右子树
}
else d=;//题数少 右子树
if(x==o->v&&tim==o->tim) {//找到当前自己题数罚时都一样的节点
(o->s)++;//当前节点子树的节点上的队伍数++
(o->num)++;//在该节点的队伍数++
}
else {
insert(o->ch[d],x,tim);if(o->ch[d]->r > o->r) rotate(o,d^);
}
}
o->maintain();
}
void remove(Node* &o,int x,int tim) {
int d=o->cmp(x,tim);
if(d==-) {
if(o->num==) {//如果处于当前题数罚时的队伍就一个队
Node* u=o;
if(o->ch[]!=NULL&&o->ch[]!=NULL) {
int d2=(o->ch[]->r>o->ch[]->r?:);
rotate(o,d2);
remove(o->ch[d2],x,tim);
}
else {
if(o->ch[]==NULL) o=o->ch[];
else o=o->ch[];
delete u;
}
}
else {//否则直接当前节点队伍数--
(o->num)--;
}
}
else {
remove(o->ch[d],x,tim);
}
if(o!=NULL) o->maintain();
}
int Rank(Node* o,int x,int tim) {
if(o==NULL) return ;
int s=(o->ch[]==NULL?:o->ch[]->s);//s是o的左子树上队伍的个数
if(o->v==x&&o->tim==tim) {//找到当前队伍状态的节点
return s+;//返回左子树队伍数+1
}
else if(o->v<x||(o->v==x&&o->tim>tim)) {//排名在当前节点之前,往左子树找
return Rank(o->ch[],x,tim);
}
else {//排名在当前节点之后,排名为当前节点左子树队伍数+当前节点队伍数+往右子树找
return Rank(o->ch[],x,tim)+s+(o->num);
}
}
int nownum[maxn],nowtim[maxn];
int main()
{
Node *root=NULL;//初始化
int n,m;
scanf("%d%d",&n,&m);
int u,v;
rep(i,,n) insert(root,,);//大家一开始都没过题
rep(i,,m) {
scanf("%d%d",&u,&v);
remove(root,nownum[u],nowtim[u]);//移除当前队伍之前的状态
nownum[u]++;//题数加1
nowtim[u]+=v;//加罚时
insert(root,nownum[u],nowtim[u]);//插入当前状态
printf("%d\n",Rank(root,nownum[],nowtim[]));//查询队伍编号为1的名次
}
return ;
}
/**
3 10
2 1
2 1
3 1
1 1
3 5
3 5
1 1
1 2
3 1 */
Kattis - gcpc (treap模板)的更多相关文章
- BZOJ 1588: Treap 模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12171 Solved: 4352 Description ...
- G - Galactic Collegiate Programming Contest Kattis - gcpc (set使用)
题目链接: G - Galactic Collegiate Programming Contest Kattis - gcpc 题目大意:当前有n个人,一共有m次提交记录,每一次的提交包括两个数,st ...
- [luogu3369]普通平衡树(treap模板)
解题关键:treap模板保存. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- Treap 模板 poj1442&hdu4557
原理可以看hihocoder上面的讲解,很清楚,不多说了. 模板抄lrj训练指南上面的. /** Treap 实现 名次树 功能: 1.找到排名为k的元素 2.值为x的元素的名次 初始化:Node* ...
- 平衡树Treap模板与原理
这次我们来讲一讲Treap(splay以后再更) 平衡树是一种排序二叉树(或二叉搜索树),所以排序二叉树可以迅速地判断两个值的大小,当然操作肯定不止那么多(不然我们还学什么). 而平衡树在排序二叉树的 ...
- POJ1442-查询第K大-Treap模板题
模板题,以后要学splay,大概看一下treap就好了. #include <cstdio> #include <algorithm> #include <cstring ...
- Treap 模板
感觉平衡树也没有以前想的那么玄乎,(其实set超好用的),非旋式Treap挺好理解,和可并堆,二叉搜索树有很大联系 推荐博客:http://memphis.is-programmer.com/post ...
- 【Treap模板详细注释】BZOJ3224-普通平衡树
模板题:D错因见注释 #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- 非旋treap模板
bzoj3580 非旋转treap 在大神教导下发现split一段区间时先split右边再split左边比较好写 #include <cstdio> #include <cstdli ...
随机推荐
- javascript-object对象属性操作之Object.defineProperty
一.基本用法简介 声明一个简单的对象,如下 var obj = { name: 'ldld' } 我们可以用Object.defineProperty来声明这个对象 var obj = {} Obje ...
- Vue.js的列表数据的同步更新方法
这次给大家带来Vue.js的列表数据的同步更新方法,Vue.js列表数据同步更新方法的注意事项有哪些,下面就是实战案例,一起来看一下. 数组的 push(),pop(),shift(),unshift ...
- Win10.设置(放大)
1.ZC:我从感觉,我在Win10 里面截图 貌似被放大了,尤其是 在往cnblogs里面贴图的时候 比较明显,于是 度娘“Win10 自动放大”,找到如下帖子,里面有 2种设置方式: win10系 ...
- MySQL数据库忘记密码如何重新设置?
前 言当我们忘记了MySQL数据库密码后,该如何重新进行设置? 操作步骤步骤1:cmd打开命名窗口 步骤2:关闭正在运行的MySQL服务(命令:net stop mysql)(如果:此时MySQL正在 ...
- oracle存储过程调试-plsql
1.搜索找到存储过程的包,并打开 选中右击--view spec&body 2.找到要测试的存储过程,设置断点,进行测试
- 线段树(two value)与树状数组(RMQ算法st表)
士兵杀敌(三) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进行比 ...
- Day8---Python的字典类型及操作
字典类 1.生成方法: a.介绍: 字典是键值对的集合,键值对 : 键是数据索引的扩展 b.生成方法: 使用{} 或者 dict() a = {'a' = 1, 'b' = 2, 'c' = 3 ...
- setter getter 方法
MRC下setter.getter方法写法.重写dealloc方法 @interface People : NSObject @property (nonatomic,strong) NSString ...
- 框架frameset
转自: http://www.cnblogs.com/sunfeiwto/archive////.html <FRAMESET> <FRAME> <NOFRAMES> ...
- idea模块搭建新手党常见错误
一.搭建java和web模块会出现的错误(此篇以分布式模块为例) 1.创建空工程 1.点击file ,在弹出的窗口左侧选项中在最后有一个Empty Project选项.此处就是创建空工程. 2.在此空 ...