洛谷P4254 [JSOI2008]Blue Mary开公司(李超线段树)
题面
题解
李超线段树板子
具体可以看这里
//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
double readdb()
{
R double x=0,y=0.1,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(x=ch-'0';(ch=getc())>='0'&&ch<='9';x=x*10+ch-'0');
for(ch=='.'&&(ch=getc());ch>='0'&&ch<='9';x+=(ch-'0')*y,y*=0.1,ch=getc());
return x*f;
}
inline char getop(){R char ch;while((ch=getc())!='Q'&&ch!='P');return ch;}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=1e5+5;
struct node{
node *lc,*rc;double b,k,lv,rv;bool flag;
inline void ins(R double bb,R double kk,R int l,R int r){b=bb,k=kk,lv=k*l+b,rv=k*r+b,flag=1;}
inline double val(const R int &x){return k*x+b;}
}pool[N<<2],*rt;int tot;
inline node *newnode(){return &pool[tot++];}
int n,x,q;double b,k,bb,kk,res;char op;
void build(node* &p,int l,int r){
p=newnode();if(l==r)return;
int mid=(l+r)>>1;
build(p->lc,l,mid),build(p->rc,mid+1,r);
}
void query(node *p,int l,int r){
cmax(res,p->val(x));if(l==r)return;
int mid=(l+r)>>1;
if(x<=mid)query(p->lc,l,mid);
else query(p->rc,mid+1,r);
}
void update(node *p,int l,int r){
if(!p->flag)return p->ins(b,k,l,r),void();
double lv=l*k+b,rv=r*k+b;
if(lv<=p->lv&&rv<=p->rv)return;
if(lv>p->lv&&rv>p->rv)return p->ins(b,k,l,r),void();
int mid=(l+r)>>1;
double x=(b-p->b)/(p->k-k);
if(lv<=p->lv){
if(x<=mid)bb=p->b,kk=p->k,p->ins(b,k,l,r),b=bb,k=kk,update(p->lc,l,mid);
else update(p->rc,mid+1,r);
}else{
if(x<=mid)update(p->lc,l,mid);
else bb=p->b,kk=p->k,p->ins(b,k,l,r),b=bb,k=kk,update(p->rc,mid+1,r);
}
}
int main(){
// freopen("testdata.in","r",stdin);
n=50000,q=read();
build(rt,1,n);
while(q--){
op=getop();
if(op=='Q')x=read(),res=0,query(rt,1,n),print(res/100);
else b=readdb(),k=readdb(),b-=k,update(rt,1,n);
}
return Ot(),0;
}
深深地明白自己的弱小