楼房重建

楼房重建

线段树

bzoj2957

题解:

这里写图片描述

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 100005;

struct Node{
    int l,r,mid;
    int ans; double maxk;
}t[N*4];

void build(int num,int l,int r){
    Node &now=t[num];
    now.l=l; now.r=r; now.mid=(l+r)>>1;
    now.ans=0; now.maxk=0.0;
    if(l!=r){
        build(num*2,l,now.mid);  
        build(num*2+1,now.mid+1,r);
    }
}

int count(int num,double limit){
    Node &now=t[num];
    if(now.l==now.r) return now.maxk>limit;
    Node &lch=t[num*2], &rch=t[num*2+1];
    if(lch.maxk<=limit) return count(num*2+1,limit);
    else return now.ans-lch.ans+count(num*2,limit);
}

void change(int num,int x,double k){
    Node &now=t[num];
    if(now.l==now.r){ now.ans=1; now.maxk=k; }
    else{
        if(x<=now.mid) change(num*2,x,k);
        else change(num*2+1,x,k);
        Node &lch=t[num*2], &rch=t[num*2+1];
        now.maxk=max(lch.maxk,rch.maxk);
        now.ans=lch.ans+count(num*2+1,lch.maxk);
    }
}

void read(int &num){
    num=0; char c;
    while(!isdigit(c=getchar()));
    num=c-'0';
    while(isdigit(c=getchar()))num=num*10+c-'0';
}

int n,m;
int main(){
    freopen("building.in","r",stdin);
    read(n); read(m);
    build(1,1,n);
    int x,y;
    for(int i=1;i<=m;i++){
        read(x); read(y);
        change(1,x,double(y)/x);
        printf("%d\n",t[1].ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值