楼房重建
线段树
题解:
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);
}
}