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 ...
随机推荐
- Jenkins使用二:新建任务
准备一个用于测试脚本,就打印hello world 新建job 配置: 添加步骤 立即构建
- ubuntu搭建jdk+jenkins
第一步,安装jdk(如果已安装,直接进行第二步) 1.下载 jdk-8u172-linux-x64.tar.gz 点此下载 2.解压 tar -zxvf jdk-8u172-linux ...
- 快速测试端口的连通性(HTTP/HTTPS)
ping 仅限 80 端口,命令中无法指定端口: C:\Users\Administrator>ping kikakika.com 遗失对主机的连接. 正在 Ping kikakika.com ...
- File类的相关方法
java.io.File类 文件和路径名的抽象表达形式 java把电脑中的文件和文件夹(目录)封装了一个File类,我们可以使用File类对文件和文件夹进行如下操作 创建一个文件/文件夹 删除 获取 ...
- 学习ECMAScript标准和具体实现-JavaScript
在NDN的JavaScript Guide里,Array和Map,Set都属于collections of data.它们的区别就是,Array是ordered by an index value, ...
- deepin修改默认Python2到Python3
第一步 打开终端 第二步 输入 sudo vi ~/.bashrc 然后你会看到如下界面: 切大写,输入E,进入如下界面,并在最后输入我已经输入的 alias python='python3' ,记住 ...
- sql注入判断流程(结合sqli-labs学习)
sql注入判断流程(结合sqli-labs学习) 类型一 类型判断 ?id=1 and 1=2 --+ 如果返回结果正常,说明不是数字类型 and 为两方都为真才算争取 ?id=1' --+ 显示不正 ...
- ubuntu server 12.04安装任何软件都出现the following packages have unmet dependencies的解决方法
虽然目前没太弄清这到底是怎么回事,但是暂时可以给出一个解决的方法, 如果在安装任何软件都会出现这个问题,那么尝试着输入sudo apt-get install -f试一下. 在该命令执行完成后,我这边 ...
- dfs(最长路径)
http://poj.org/problem?id=1154 LETTERS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- Anaconda Jupyter WinError2:The system cannot find the file specified
Traceback (most recent call last): File "C:\Users\builder\Miniconda3\Scripts\conda-build-script ...