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 ...
随机推荐
- 用PHP实现一些常见的排序算法
1.冒泡排序: 两两相比,每循环一轮就不用再比较最后一个元素了,因为最后一个元素已经是最大或者最小. function maopaoSort ($list) { $len = count($list) ...
- loj#500 「LibreOJ β Round」ZQC 的拼图
分析 二分倍数 然后考虑dp[i][j]表示选到第i个x轴覆盖到j的情况y轴最多覆盖多少 贡献柿子可以画图然后相似三角形得到 代码 #include<bits/stdc++.h> usin ...
- 《图解设计模式》读书笔记4-1 Bridge模式
目录 概念 代码 角色 类图 想法 概念 Bridge模式即桥接模式.顾名思义,这个模式的作用是将类的功能层次结构和类的实现层次结构连接起来. 功能层次结构 Something -SomethingG ...
- 云计算openstack核心组件——keystone身份认证服务
一.Keystone介绍: keystone 是OpenStack的组件之一,用于为OpenStack家族中的其它组件成员提供统一的认证服务,包括身份验证.令牌的发放和校验.服务列表.用户 ...
- Dapper(一) 简介和性能
Dapper的简介 Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,并且是半自动的.Dapper只有一个代码文件,完全开源,你 ...
- C#通过UserAgent判断智能设备(Android,IOS,PC,Mac)
尝试通过 Agent 来获取相应的智能手机设备标识,根据标识的不同来输出对应设备所需的显示样式及其他.经过努力,终于搜集了比较全的 智能设备 的 Agent,相应的判断过程及代码如下,不明白的留言. ...
- package和import语句_2
package import 总结 1.如果想将一个类放入包中,在这个类源文件第一句话写package 2.必须保证该类的class文件位于正确目录下 1)该类的源码可能会产生影响 ...
- an安装jenkins时遇到ERROR: No Java executable found in current PATH: /bin:/usr/bin:/sbin:/usr/sbin的问题
# sudo /etc/init.d/jenkins restartERROR: No Java executable found in current PATH: /bin:/usr/bin:/sb ...
- [LeetCode] 182.查找重复的电子邮箱
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱. 示例: +----+---------+ | Id | Email | +----+---------+ | 1 | a@b.co ...
- Java 遍历某个目录
import java.io.File; import java.io.IOException; public class DirErgodic { public static void find(S ...