萌新刚学OI!全WA求助

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

bellmanford @ 2019-08-07 17:15:59

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

#define OPTIMIZE ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define In inline
#define Re register
#define db double
#define ll long long
const int M=1e6+5;
const int n=5e4+5;
const int MOD1=39989;
const int MOD2=1e9;

int q,num=0,lastans=0;
bool vis[M];
struct node{
    int num;
    double b,k;
}x[M<<1],tr[M<<3];

int read(){
    int x=0,y=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') y=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*y;
}

double Calc(node hs,int x){
    return 1.0*hs.k*x+hs.b;
}

void insert(int u,int l,int r,int x1,int x2,node p){
    if(l>=x1&&r<=x2){
        int mid=(l+r)>>1;
        if(Calc(tr[u],mid)<Calc(p,mid)||(!tr[u].num)) swap(tr[u],p);
        double jd=1.0*(tr[u].b-p.b)/(p.k-tr[u].k);
        if(l==r||tr[u].k==p.k||jd<1.0*l||jd>1.0*r||(!p.num)) return;
        if(p.k<tr[u].k) insert(u<<1,l,mid,x1,x2,p);
        else insert(u<<1|1,mid+1,r,x1,x2,p);
    }
    else{
        int mid=(l+r)>>1;
        if(x1<=mid) insert(u<<1,l,mid,x1,x2,p);
        if(x2>mid) insert(u<<1|1,mid+1,r,x1,x2,p);
    }
}

node query(int u,int l,int r,int x){
    if(l==r) return tr[u];
    int mid=(l+r)>>1;
    node k;
    if(x<=mid) query(u<<1,l,mid,x);
    else query(u<<1|1,mid+1,r,x);
    return ((!k.num)||Calc(k,x)<Calc(tr[u],x))?tr[u]:k;
}

int main(){
    OPTIMIZE
    freopen("shuju.in","r",stdin);
    q=read();
    while(q--){
        int opt=read();
        if(opt==0){
            int k=read();
            k=(k+lastans-1)%MOD1+1;
            lastans=query(1,1,n,k).num;
            printf("%d\n",lastans);
        }
        if(opt==1){
            int x1=read(),y1=read(),x2=read(),y2=read();
            num++;
            x1=(x1+lastans-1)%MOD1+1;
            y1=(y1+lastans-1)%MOD2+1;
            x2=(x2+lastans-1)%MOD1+1;
            y2=(y2+lastans-1)%MOD2+1;
            if(x1>x2) swap(x1,x2),swap(y1,y2);
            x[num].k=(y1-y2)/(x1-x2);
            x[num].b=(x1*y2-x2*y1)/(x1-x2);
            x[num].num=num;
            insert(1,1,n,x1,x2,x[num]);
        }
    }
}

by 陌屿 @ 2019-08-07 19:54:39

@bellmanford 结构体可以交换的说


by bellmanford @ 2019-08-07 20:23:41

@落羽 谢谢

调出来了


by bellmanford @ 2019-08-07 20:24:33

b的值算错了QAQ


by Light_Poet @ 2019-08-07 20:55:49

@bellmanford 可以请教一下您改了那里吗QAQ,我的还是WA90...


by bellmanford @ 2019-08-09 23:21:16

@Light_Poet

错误b值:

x[num].k=(y1-y2)/(x1-x2);
x[num].b=(x1*y2-x2*y1)/(x1-x2);

正确b值:

x[num].k=1.0*(y1-y2)/(x1-x2);
x[num].b=1.0*y1-1.0*x1*s[cnt].k;

窝的b的值脑抽算错了。。。

QAQ


上一页 |