刚学线段树,爆0求救

P4097 【模板】李超线段树 / [HEOI2013] Segment

exzang @ 2020-02-26 23:52:42

全WA,爆0,然而找不出错

#include<bits/stdc++.h>
using namespace std;
#define ls rt<<1
#define rs rt<<1|1
const int N=100000+5;
struct function{
    double k,b;
}f[N<<2];
double val(int id,int x){
    return f[id].k*(x)+f[id].b;
}
namespace LC_Segment{
    int tag[N<<2];
    void upt(int rt,int l,int r,int id){
        if(!tag[rt]){
            tag[rt]=id; return;
        }
        if(l==r){
            if(val(tag[rt],l)<val(id,l)) tag[rt]=id; return;
        }
        int mid=(l+r)>>1;
        if(f[id].k>f[tag[rt]].k){
            if(val(id,mid)>val(tag[rt],mid)) upt(ls,l,mid,tag[rt]),tag[rt]=id;
            else upt(rt,mid+1,r,id);
        }
        else{
            if(val(id,mid)>val(tag[rt],mid)) upt(rs,mid+1,r,tag[rt]),tag[rt]=id;
            else upt(ls,l,mid,id);
        }
    }
    void change(int rt,int l,int r,int x,int y,int id){
        if(x<=l && r<=y){
            upt(rt,l,r,id); return;
        }
        int mid=(l+r)>>1;
        if(x<=mid) change(ls,l,mid,x,y,id);
        if(mid+1<=y) change(rs,mid+1,r,x,y,id);
    }
    int query(int rt,int l,int r,int x){
        if(l==r) return tag[rt];
        int mid=(l+r)>>1; int ans=0;
        if(x<=mid) ans=query(ls,l,mid,x);
        else ans=query(rs,mid+1,r,x);
        if(val(tag[rt],x)>val(ans,x)) ans=tag[rt];
        return ans;
    }
}
int opt,ans,id; int tot;
int main(){
//  freopen("P4097_1.in","r",stdin); 
//  freopen("P4097_1.ans","w",stdout);
    int n,x,x0,y0,x1,y1; double k,b;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%d",&opt);
        if(opt==1){
            scanf("%d%d%d%d",&x0,&y0,&x1,&y1);
            x0=(x0+ans-1)%39989+1; y0=(y0+ans-1)%1000000000+1;
            x1=(x1+ans-1)%39989+1; y1=(y1+ans-1)%1000000000+1;
            ++id;
            if(x0>x1) swap(x0,x1),swap(y0,y1);
            if(x0==x1) f[id].k=0,f[id].b=max(y0,y1);
            else f[id].k=(y1-y0)*1.0/(x1-x0),f[id].b=y0-f[id].k*x0;
            LC_Segment::change(1,1,40000,x0,x1,id);
        }
        else{
            scanf("%d",&x);
            x=(x+ans-1)%39989+1;
            ans=LC_Segment::query(1,1,40000,x);
            printf("%d\n",ans);
        }
    }
    return 0;
}

by Immortal_Bird @ 2020-02-27 00:06:55

好一个刚学


by yagyagyag @ 2020-02-27 08:02:20

好一个刚学


|