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 ...
随机推荐
- day06—JavaScript之闭包
转行学开发,代码100天——2018-03-22 第一次听说“闭包”这个词,还是在2015年某个深夜在听一节腾讯课堂的公开课上,当时老师讲什么已经没有清晰的记忆了,只知道是一次web的开发课程. 过了 ...
- win2016
slmgr /ipk CB7KF-BWN84-R7R2Y-793K2-8XDDG slmgr /skms kms.03k.org slmgr /ato
- Drone 中的概念:webhooks、workspace、cloning、pipelines、services、plugins、deployments
webhooks 跳过提交 包含/跳过分支 branches workspace base 属性 path 属性 cloning pipelines 构建步骤 并行执行 group 条件执行 when ...
- 【Unity 系统知识】 各种路径
一.Assets下的Resources(Unity系统文件夹) :路径 Application.dataPath/Resources 可以使用Resources.Load("文件名字,注:不 ...
- hdu6575Budget
Problem Description Avin’s company has many ongoing projects with different budgets. His company rec ...
- linux常用命令介绍
参考博客: https://www.cnblogs.com/caozy/p/9261224.html 学前理论 linux主要特征 :一切且文件(目录.硬盘等都是文件):硬件都在/dev 目录,如硬盘 ...
- css 绘制checkbox,radio
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- Libre OJ 2255 (线段树优化建图+Tarjan缩点+DP)
题面 传送门 分析 主体思路:若x能引爆y,从x向y连一条有向边,最后的答案就是从x出发能够到达的点的个数 首先我们发现一个炸弹可以波及到的范围一定是坐标轴上的一段连续区间 我们可以用二分查找求出炸弹 ...
- luoguP1505 [国家集训队]旅游(真的毒瘤)
luogu P1505 [国家集训队]旅游 题目 #include<iostream> #include<cstdio> #include<cstdlib> #in ...
- opencv2——图像上的算术运算4
1.图像算术运算 参数含义: src1:第一张图像 src2:第二张图像 dst:destination,目标图像,需要提前分配空间,可省略 mask:掩膜 scale:缩放比,常量 dtype:数据 ...