BZOJ 2957 楼房重建-线段树

这个题最主要的是解决一个统计答案的问题。

首先我们注意到,只要考虑右区间的答案统计就好了。

记左区间的最大值为K,当前右区间为P。

我们把当前右区间又分成两个子区间,s1,s2。

那么如果s1的最大值比K小,那么显然只要递归处理s2就好。

否则,如果s1的最大值比K大,那么原本的属于s2的答案一定都满足条件,即ans[p]-ans[s1],再加上递归处理s1的答案就好了。

这里注意,不是加上ans[s2],例如区间:3,4,5,1,2,9 ans[p]=4,ans[s1]=3,ans[s2]=3,但是显然1,2是不能加入到答案里去的。

#include <bits/stdc++.h>
#define lson p<<1
#define rson (p<<1)|1
using namespace std;

const int N=1e5+10;
int n,m,Np,high;

struct Segment_Tree {
    int l, r, Ans;
    double K;
}Tr[N<<2];

void Pushup (int p) {
    Tr[p].K=max (Tr[lson].K, Tr[rson].K);
}

void Build (int p, int l, int r) {
    Tr[p].l=l, Tr[p].r=r;
    if (l==r) return;
    int Mid= (l+r) >> 1;
    Build (lson, l, Mid);
    Build (rson, Mid+1, r);
}

int Get_Count (int p, double Val) {
    if (Tr[p].l==Tr[p].r) 
        return Tr[p].K>Val;
    if (Tr[lson].K<=Val) return Get_Count (rson, Val);
    else return Get_Count (lson, Val)+Tr[p].Ans-Tr[lson].Ans;
} 

void Update (int p, int Npp, double val) {
    if (Tr[p].l==Tr[p].r) {
        Tr[p].K=val;
        Tr[p].Ans=1;
        return;
    }
    int Mid= (Tr[p].l+Tr[p].r) >> 1;
    if (Npp<=Mid) Update (lson, Npp, val);
    else Update (rson, Npp, val);
    Pushup (p);
    Tr[p].Ans=Tr[lson].Ans+Get_Count (rson, Tr[lson].K);
}

int main ()
{
    scanf ("%d%d", &n, &m);
    Build (1, 1, n);
    while (m--) {
        scanf ("%d%d", &Np, &high);
        Update (1, Np, (double) high/Np );
        printf ("%d\n", Tr[1].Ans);
    }
    return 0;
}
BY BHLLX

猜你喜欢

转载自www.cnblogs.com/Bhllx/p/9931911.html