题意

最初,农夫约翰的每头奶牛每天生产G加仑的牛奶 (1≤G≤109)(1≤G≤10^9)(1≤G≤109) 。由于随着时间的推移,奶牛的产奶量可能会发生变化,农夫约翰决定定期对奶牛的产奶量进行测量,并将其记录在日志中。

他的日志中的记录如下:

35 1234 -2

14 2345 +3

第一个条目表明:在第35天,1234号奶牛的产奶量比上次测量时降低了2加仑。

第二个条目表明:在第14天,2345号奶牛的产奶量比上次测量时增加了3加仑。

农夫约翰只有在任何一天内做最多一次测量的时间(即每天最多做一次测量,但可能不做)。不幸的是,约翰有点杂乱无章,他不一定按照时间顺序记下测量结果。为了保持奶牛的产奶动力,农夫约翰自豪地在谷仓的墙上展示了目前产奶量最高的奶牛的照片(如果有若干头奶牛的产奶量最高,他就会展示所有的图片)。

请求出约翰需要调整所展示的照片的次数。

请注意,农夫约翰有一大群奶牛。所以尽管日志中记录了一些奶牛改变了产奶量,但仍然还有很多奶牛的产奶量保持在G加仑。

题解

每一个时间只有一个奶牛产奶量会变化。

我们算出变化前的产奶量的排名,和变化后的产奶量排名。

如果发现是从第一变到不是第一,或从不是第一到第一,那照片一定发生变化。

如果变化后,和变化前都是第一。照片可能变也可能不会变,分四种情况讨论:
1,、可能一开始有很多奶牛并列第一,然后其中的一个奶牛独占了第一。这时照片会变。

2、一开始一个奶牛是第一,然后产奶量增加还是第一,显然不变。

3、也有可能开始一个奶牛第一然后,产奶量下降,变得和第二一样,这时会变。

4、一开始一个奶牛是第一,然后产奶量下降但比第二高还是第一,显然不变。

所以我们找出产奶量为变化之前的奶牛的数量,和产奶量为变化之后的奶牛的数量,判断相不相等即可。

这些东西都可以用平衡树维护。(权值线段树,主席树什么的当然也可以)

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
const int N=;
map<int,int>ma;
int n,g,mx,num;
int a[N],b[N],ans[N];
int cnt[N],size[N],ch[N][],fa[N],v[N];
int root,tot;
struct hhh{
int t,id,w;
}c[N];
bool cmp(hhh a,hhh b){
return a.t<b.t;
}
void update(int x){
size[x]=size[ch[x][]]+size[ch[x][]]+cnt[x];
}
int son(int x){
return x==ch[fa[x]][];
}
void rotate(int x){
int y=fa[x],z=fa[y],a=son(x),b=son(y),c=ch[x][!a];
if(z)ch[z][b]=x;
else root=x;
fa[x]=z;
if(c)fa[c]=y;
ch[x][!a]=y;
ch[y][a]=c;
fa[y]=x;
update(y);
update(x);
}
void splay(int x,int f){
while(fa[x]!=f){
int y=fa[x];
int z=fa[y];
if(z==f)rotate(x);
else{
if(son(y)==son(x))rotate(y);
else rotate(x);
rotate(x);
}
}
if(f==)root=x;
}
void ins(int x){
int p=root;
int f=;
while(p&&v[p]!=x){
f=p;
size[p]++;
p=ch[p][v[p]<x];
}
if(p){
size[p]++;
cnt[p]++;
}
else{
p=++tot;
if(f)ch[f][v[f]<x]=p;
size[p]=cnt[p]=;
v[p]=x;
fa[p]=f;
}
splay(p,);
}
int getmn(int rt){
int p=rt,ans=-;
while(p){
ans=p;
p=ch[p][];
}
return ans;
} void del(int rt,int x){
if(v[rt]==x){
if(cnt[rt]>)cnt[rt]--,size[rt]--;
else{
splay(rt,);
int p=getmn(ch[rt][]);
if(p!=-){
splay(p,rt);
root=p;
fa[p]=;
ch[p][]=ch[rt][];
fa[ch[rt][]]=p;
update(p);
}
else {
root=ch[rt][];
fa[root]=;
}
}
return;
}
if(x<v[rt])del(ch[rt][],x),update(rt);
else del(ch[rt][],x),update(rt);
}
int rank(int rt,int k){
if(v[rt]==k){
splay(rt,);
return size[ch[rt][]]+;
}
if(k<v[rt])return rank(ch[rt][],k);
else return rank(ch[rt][],k);
}
int getsame(int rt,int x){
if(v[rt]==x){
splay(rt,);
return cnt[rt];
}
if(x<v[rt])return rank(ch[rt][],x);
else return rank(ch[rt][],x);
}
int main(){
scanf("%d%d",&n,&g);
for(int i=;i<=n;i++){
scanf("%d%d",&c[i].t,&c[i].id);
char ch;
cin>>ch;
scanf("%d",&c[i].w);
if(ch=='-')c[i].w=-c[i].w;
b[i]=c[i].id;
}
sort(b+,b++n);
int cnt=unique(b+,b++n)-b-;
for(int i=;i<=n;i++){
c[i].id=lower_bound(b+,b++cnt,c[i].id)-b;
}
sort(c+,c++n,cmp);
for(int i=;i<=cnt+;i++){
a[i]=g;
ins(g);
}
ma[g]=cnt+;
for(int i=;i<=n;i++){
// cout<<";asjhfljashfjashdfasdf"<<endl;
if(c[i].w==)continue;
int k=rank(root,a[c[i].id]);
int num1=getsame(root,a[c[i].id]);
// cout<<k<<" "<<num1<<endl<<"aaa"<<endl;
del(root,a[c[i].id]);
a[c[i].id]+=c[i].w;
ins(a[c[i].id]);
int kk=rank(root,a[c[i].id]);
int num2=getsame(root,a[c[i].id]);
if((kk==&&k!=)||(k==&&kk!=)||(num1!=num2&&kk==&&k==)){
ans[++num]=c[i].t;
}
}
printf("%d\n",num);
// for(int i=1;i<=num;i++){
// printf("%d\n",ans[i]);
// }
return ;
}
/*
4 10
7 3 +3
4 2 -1
9 3 -1
1 1 +2
*/

[USACO17DEC]Milk Measurement(平衡树)的更多相关文章

  1. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1017  Solved: ...

  2. [BZOJ3223]Tyvj 1729 文艺平衡树

    [BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...

  3. [BZOJ3224]Tyvj 1728 普通平衡树

    [BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...

  4. Principles of measurement of sound intensity

    Introduction In accordance with the definition of instantaneous sound intensity as the product of th ...

  5. BZOJ3223: Tyvj 1729 文艺平衡树 [splay]

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3595  Solved: 2029[Submit][Sta ...

  6. [普通平衡树treap]【学习笔记】

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9046  Solved: 3840[Submit][Sta ...

  7. BZOJ 3224: Tyvj 1728 普通平衡树

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9629  Solved: 4091[Submit][Sta ...

  8. BZOJ 3223: Tyvj 1729 文艺平衡树

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3628  Solved: 2052[Submit][Sta ...

  9. 机器学习中的相似性度量(Similarity Measurement)

    机器学习中的相似性度量(Similarity Measurement) 在做分类时常常需要估算不同样本之间的相似性度量(Similarity Measurement),这时通常采用的方法就是计算样本间 ...

随机推荐

  1. JavaScript中的线程与进程

    定义: 线程分为:单线程和多线程 单线程:一个正在运行的程序(即进行)至少有一个线程,这个线程叫做主线程,只有一个主线程的程序叫做单线程程序,主线程负责执行所有代码的执行(UI展现及刷新.网络请求.本 ...

  2. Java基础——Servlet

    什么是Servlet Servlet是Java Web的三大组件之一,它属于动态资源.Servlet的作用是处理请求,服务器会把接收到的请求交给Servlet来处理,在Servlet中通常需要: l  ...

  3. ajax第二天学习

    post方式发送请求 要首先设置请求头(参数设置为ajax.setRequestHeader("content-type","application/x-www-form ...

  4. Github添加SSHkey

    Git详细教程可参考廖雪峰的Git教程 1. 打开 Git Bash,输入cd ~/.ssh——回车(看你是否有了ssh key 密钥,有了就备份): 2. 输入ssh-keygen -t rsa - ...

  5. 使用Git--将本地项目提交到Github

    前置工作 1. 在GitHub官网注册一个GitHub账号: 2. 安装git工具,在Git官网下载对应版本的Git: 方法一: 1. 进入Github首页,点击New repository新建一个项 ...

  6. CSS布局总结(一)

    前言:今天是学校为期六周的实训第一天,实训课感觉很水,第一天讲的竟然是HTML...实训老师丢了一个静态页面给我们做.感觉很久没写过这种东西,突然觉得自己的基本功很渣.布局这方面感觉需要总结一下,然后 ...

  7. BZOJ 2049 [SDOI2008]洞穴勘测 (LCT)

    题目大意:维护一个森林,支持边的断,连,以及查询连通性 LCT裸题 洛谷P2147传送门 1A了,给自己鼓鼓掌 #include <cstdio> #include <algorit ...

  8. Layui表格编辑【不依赖Layui的动态table加载】

    依赖jquer,layui/css <td class="My_edit"></td> Jquery代码 //-----[Layui表格编辑(<td ...

  9. python 进程 multiprocessing模块

    一.multiprocess.process模块 1.process类 Process([group [, target [, name [, args [, kwargs]]]]]),由该类实例化得 ...

  10. 旋转VR相机不头晕:一个反直觉的发现

    旋转VR相机不头晕:一个反直觉的发现 本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/deta ...