题意

最初,农夫约翰的每头奶牛每天生产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. ZBrush中Pinch捏挤笔刷介绍

    随着版本的升级ZBrush®中给我们提供了越来越多的笔刷,对于这款软件来说,笔刷的使用是第一要素,也会一直伴随我们创作.虽然Zbrush中有那么多的笔刷,但是很多朋友会根据自己的习惯来使用,这个并不是 ...

  2. luoguP4238 【模板】多项式求逆 NTT

    Code: #include <bits/stdc++.h> #define N 1000010 #define mod 998244353 #define setIO(s) freope ...

  3. JS实现HTML打印机效果

    5月最近在学Django,所以所有的Demo都没有PO出去 <!DOCTYPE html> <html lang="en"> <head> &l ...

  4. django patch 解决 ["'15428560000' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

    __init__.py import datetime from django.apps import AppConfig from django.db.models.fields import Da ...

  5. [AtCoder Grand Contest 024 Problem E]Sequence Growing Hard

    题目大意:考虑 N +1 个数组 {A0,A1,…,AN}.其中 Ai 的长度是 i,Ai 内的所有数字都在 1 到 K 之间. Ai−1 是 Ai 的子序列,即 Ai 删一个数字可以得到 Ai−1. ...

  6. IE9以下版本兼容h5标签

    随着html5(后面用h5代表)标签越来越广泛的使用,IE9以下(IE6-IE8)不识别h5标签的问题让人很是烦恼. 在火狐和chrome之类的浏览器中,遇到不认识的标签,只要给个display:bl ...

  7. Mysql 5.7 官方文档翻译

    始于 2017年4月1日-愚人节 1.1 MySQL 5.7 新功能 本章节介绍了MySQL 5.7 新版本中新增.废弃.删除的功能. 在1.5章节 Section 1.5, "Server ...

  8. POJ 3696

    这里面的一个转换的小技巧很重要,把888...8转换成(10^x-1)/9*8.神来之笔,佩服. 这样有(10^x-1)/9*8=L*p得10^x-1=L*p*9/8,设m=9*L/gcd(L,8). ...

  9. IntelliJ IDEA启动spring boot项目出现Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]

    IntelliJ IDEA启动spring boot项目出现Failed to start component [StandardEngine[Tomcat].StandardHost[localho ...

  10. 基础数位DP小结

    HDU 3555 Bomb dp[i][0] 表示含 i 位数的方案总和. sp[i][0] 表示对于位数为len 的 num 在区间[ 10^(i-1) , num/(10^(len-i)) ] 内 ...