[李超线段树] BZOJ 3165: [Heoi2013]Segment

Solution

裸题。。。
刚开始求线段交点求错了,心态崩了

#include <bits/stdc++.h>
using namespace std;

const double eps = 1e-10;
const int N = 101010;
const int M1 = 39989;
const int M2 = 1000000000;
typedef pair<int, int> Pairs;

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
template<typename T>
inline void read(T &x) {
    static char c; x = 0; int sgn = 0;
    for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
    if (sgn) x = -x;
}

int info[N << 2];
double k[N], b[N];
int n, m, opt, ans;
int x1, z1, x2, z2;
double mx;

inline bool cmp(int i1, int i2, int x0) {
    double y1 = k[i1] * x0 + b[i1],
           y2 = k[i2] * x0 + b[i2];
    return y1 + eps < y2;
}
inline double cross(int i1, int i2) {
    return (b[i2] - b[i1]) / (k[i1] - k[i2]);
}
inline void Insert(int o, int l, int r, int L, int R, int id) {
    if (l >= L && r <= R) {
        int d1 = cmp(info[o], id, l), d2 = cmp(info[o], id, r);
        if (d1 && d2) return (void)(info[o] = id);
        if (!d1 && !d2) return;
        if (l == r) return;
        int mid = (l + r) >> 1;
        double pos = cross(id, info[o]);
        if (pos <= mid) {
            if (d2) Insert(o << 1, l, mid, L, R, info[o]);
            else Insert(o << 1, l, mid, L, R, id);
            if (d2) info[o] = id;
        } else {
            if (d1) Insert(o << 1 | 1, mid + 1, r, L, R, info[o]);
            else Insert(o << 1 | 1, mid + 1, r, L, R, id);
            if (d1) info[o] = id;
        }
        return;
    }
    int mid = (l + r) >> 1;
    if (L <= mid) Insert(o << 1, l, mid, L, R, id);
    if (R > mid) Insert(o << 1 | 1, mid + 1, r, L, R, id);
}
inline void Query(int o, int l, int r, int x0) {
    if (k[info[o]] * x0 + b[info[o]] - eps > mx) {
        mx = k[info[o]] * x0 + b[info[o]];
        ans = info[o];
    }
    if (fabs(k[info[o]] * x0 + b[info[o]] - mx) < eps && info[o] < ans) {
        ans = info[o];
    }
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (x0 <= mid) Query(o << 1, l, mid, x0);
    else Query(o << 1 | 1, mid + 1, r, x0);
}

int main(void) {
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    read(n);
    while (n--) {
        read(opt);
        if (opt) {
            read(x1); read(z1); read(x2); read(z2);
            x1 = (x1 + ans - 1) % M1 + 1; x2 = (x2 + ans - 1) % M1 + 1;
            z1 = (z1 + ans - 1) % M2 + 1; z2 = (z2 + ans - 1) % M2 + 1;
            if (x1 > x2) swap(x1, x2), swap(z1, z2);
            k[++m] = (double)(z2 - z1) / (x2 - x1);
            b[m] = z1 - x1 * k[m];
            Insert(1, 1, M1, x1, x2, m);
        } else {
            mx = -1e18; read(x1);
            x1 = (x1 + ans - 1) % M1 + 1;
            Query(1, 1, M1, x1);
            printf("%d\n", ans);
        }
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值