题意

最初,农夫约翰的每头奶牛每天生产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. vector的resize与reserve的区别

  2. kernel zram feature

    what is zram? Zram wiki zram zram(也称为 zRAM,先前称为 compcache)是 Linux 内核的一项功能,可提供虚拟内存压缩.zram 通过在 RAM 内的压 ...

  3. tcpsock for Golang

    前记:本文所述的 tcpsock 库托管在 Github. Golang 中的 net 标准库已对 TCP 网络编程作了简洁(却很不简单)的封装,基本上,可直接通过引用其提供的相关接口开发简易的网络应 ...

  4. node 常用命令行

    安装模块命令 npm install moduleName –save npm install moduleName npm install npm start express创建项目目录 expre ...

  5. Vue.mixin Vue.extend(Vue.component)的原理与区别

    1.本文将讲述 方法 Vue.extend Vue.mixin 与 new Vue({mixins:[], extend:{}})的区别与原理 先回顾一下 Vue.mixin 官网如下描述: Vue. ...

  6. 2019-03-18 使用Request POST获取CNABS网站上JSON格式的表格数据,并解析出来用pymssql写到SQL Server中

    import requests import pymssql url = 'https://v1.cn-abs.com/ajax/ChartMarketHandler.ashx' headers = ...

  7. dbgview

    这两天在看一个问题,matlab打不开摄像头,总是报错. 尝试抓包,打印,分析代码,一直没有找出问题,后来用dbgview打印出来调试信息,找到了问题点. 不得不说,这个工具真不错,以前从来不知道. ...

  8. 【codeforces 452D】Washer, Dryer, Folder

    [题目链接]:http://codeforces.com/problemset/problem/452/D [题意] 洗衣服有3个步骤,洗,干,叠; 有对应的3种洗衣机,分别有n1,n2,n3台,然后 ...

  9. 使用excel进行数据挖掘(6)---- 预測

    在配置环境后,能够使用excel进行数据挖掘. 环境配置问题可參阅: http://blog.csdn.net/xinxing__8185/article/details/46445435 例子 DM ...

  10. 关于Servo项目中Rust代码行数的数据来源

    我两个月之前的一篇博客<为什么我说Rust是靠谱的编程语言>(下面简称原文),在当中"6. 两个半大型成功案例"一节.我以前写道: Servo: 下一代浏览器渲染引擎( ...