Milk Meass rement

9201 ワード

Milk Meass rement
時間制限:1 Secメモリ制限:128 MB
テーマの説明
Each of Farmer John’s cows initially produces G galles of milk per day(1≦G≦109).Since the milk output of a cow is known to potentally change over time,Farmer John decides to take periodic measremens of milk output and write these down n n n n logn book.Enties in his look like this:35 1234-2 14 2345+3 The first entry indicates day 35,cow atwantを選択します.cow実s milk output incread by 3 gallows from when it last measred.Farmer John has only enough time to make t most one measrement on any given day.Unfortunally,he is a bit disoganized,andement.
To keep his cows motivated、Farmer Jon proudidisplays on the wall of his barn the pictur of whicheever cow currently has the highest mitputt(if several cows tie for thehighethe highethe highehest mimimimimimimimimimimimitttttttdididididididididididiaaaaaaaaaatttttttttttttttttdididididididididididididididididididiaaaaaaaaaaaaaaaaaaaaaaaaaed to change this display.
Note that Farmer John has a very larghed of cows、so although some of them arm are noted in his log book as change their milk production、there are always plenty of other cows around whold whouse wlembook Out Melogs.Gut.galles.mes.
入力
The first line of input contains the number of meass Nthat Farmer John makes(1≦N≦100,000)、フォロワーby G.Each of the next N lines contains one measrement、in formation abook、specifting theinger 106and the change in her milk output since it was last meass red.Each cow’s milk output will always be in the range 0…109.
出力
Please output the number of days on which Farmer John needs to adjust his motivational display.
サンプル入力
4 10 7+3+4 2-1 9 3-1 1+2
サンプル出力
3
ヒント
ソース
USACO 2017 December Conttest,Silver
コード
#include 
#include 
#include 
#define L(u) (u<<1)
#define R(u) (u<<1|1)
using namespace std;
const int mx=100010;
struct Tree {
    int lch,rch,sum;
} tree[mx<<2];
struct data {
    int time,id,x;
} f[mx];
int d[mx],ans,n,G,Max,ID[mx],Tmp[mx],N;
bool cmp(data A,data B) {
    return A.timevoid Pushup(int u) {
    tree[u].sum=max(tree[L(u)].sum,tree[R(u)].sum);
}
void Build(int u,int l,int r) {
    tree[u].lch=l;
    tree[u].rch=r;
    if (l==r) {
        tree[u].sum=d[l];
        return ;
    }
    int mid=(l+r)>>1;
    Build(L(u),l,mid);
    Build(R(u),mid+1,r);
    Pushup(u);
}
void Update(int u,int pos,int val) {
    if (tree[u].lch==tree[u].rch) {
        tree[u].sum=val;
        return;
    }
    int mid=(tree[u].lch+tree[u].rch)>>1;
    if (pos<=mid) Update(L(u),pos,val);
    else Update(R(u),pos,val);
    Pushup(u);
}

bool Judge(int idx,int rev) { //             
    int OldMax = tree[1].sum;
    Update(1,idx,-10);
    int NewMax = tree[1].sum;
    Update(1,idx,rev);
    return !(OldMax == NewMax);//    
}

int main() {
    // freopen("in","r",stdin);
    scanf("%d%d",&n,&G);
    for (int i=1; i<=n; ++i) {
        scanf("%d%d%d",&f[i].time,&f[i].id,&f[i].x);
        Tmp[i]=f[i].id;
    }
    sort(f+1,f+1+n,cmp);
    sort(Tmp+1,Tmp+1+n);
    for (int i=1; i<=n; ++i) {
        if (Tmp[i]!=Tmp[i-1]) ID[++N]=Tmp[i];
    }
    for (int i=1; i<=N; ++i) d[i]=G;
    Build(1,1,N);
    for (int i=1; i<=n; ++i) {
        int val=f[i].x;
        int idx=lower_bound(ID+1,ID+1+N,f[i].id)-ID;
        if(d[idx] != tree[1].sum) { //       
            Update(1,idx,d[idx] + val);
            if(d[idx] + val == tree[1].sum) {   //        
                ans ++;
            } else {        //         
                ;
            }
        } else {    //      
            if(val > 0) {
                if(!Judge(idx,d[idx])) { //          
                    ans ++;
                } else {
                    ans += 0;
                }
                Update(1,idx,d[idx] + val);
            } else {    //    
                if(Judge(idx,d[idx])) { //           
                    Update(1,idx,d[idx] + val);
                    if(d[idx] + val != tree[1].sum) { //         
                        ans ++;
                    } else { //         
                        if(!Judge(idx, d[idx] + val)) { //          
                            ans ++;
                        } else {
                            ans += 0;
                        }
                    }
                } else {
                    Update(1,idx,d[idx] + val);
                    ans += 1;
                }
            }
        }
        d[idx] += val;
    }
    if (ans==0) ans=1;
    printf("%d
"
,ans); return 0; } /************************************************************** Problem: 5424 User: WC006 Language: C++ Result: Time:256 ms Memory:8740 kb ****************************************************************/