BZOJ3165: [Heoi2013]Segment(李超線段樹)

自為風月馬前卒發表於2019-02-07

題意

題目連結

Sol

李超線段樹板子題。具體原理就不講了。

一開始自己yy著寫差點寫自閉都快把叉積搬出來了。。。

後來看了下litble的寫法才發現原來可以寫的這麼清晰簡潔Orz

#include<bits/stdc++.h>
#define pdd pair<double, double> 
#define MP make_pair
#define fi first
#define se second 
using namespace std;
const int MAXN = 1e6 + 10, Lim = 1e9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < `0` || c > `9`) {if(c == `-`) f = -1; c = getchar();}
    while(c >= `0` && c <= `9`) x = x * 10 + c - `0`, c = getchar();
    return x * f;
}
int N = 39989, M;
int ls[MAXN], rs[MAXN], root, cnt, tot;
pdd mx[MAXN];
struct Line {
    double k, b;
    int id;
}s[MAXN];
pdd get(int x0, int y0, int x1, int y1) {
    double k = (double) (y1 - y0) / (x1 - x0),
           b = (double) y0 - k * x0;
    return {k, b};
}
double calc(Line line, int x) {
    return line.k * x + line.b;
}
double GetPoint(Line a, Line b) {
    return (b.b - a.b) / (a.k - b.k);
}
pdd ret;
void Query(int k, int l, int r, int p) {//fi: val  se: id
    if(chmax(ret.fi, calc(s[k], p))) ret.se = s[k].id;
    if(l == r) return ;
    int mid = l + r >> 1;
    if(p <= mid) Query(ls[k], l, mid, p);
    else Query(rs[k], mid + 1, r, p);
}
void Modify(int &k, int l, int r, int ql, int qr, Line seg) {
    if(!k) k = ++tot;
    int mid = l + r >> 1;
    if(ql <= l && r <= qr) {
        if(!s[k].id) {s[k] = seg; return ;}
        int p = GetPoint(s[k], seg);
        int pl = calc(s[k], l), pr = calc(s[k], r), nl = calc(seg, l), nr = calc(seg, r);
        if(pl > nl && pr > nr) return ;
        if(pl < nl && pr < nr) {s[k] = seg; return ;}
        if(pl < nl) {
            if(p > mid) Modify(rs[k], mid + 1, r, mid + 1, r, s[k]), s[k] = seg;
            else Modify(ls[k], l, mid, l, mid, seg);
        } else {
            if(p > mid) Modify(rs[k], mid + 1, r, mid + 1, r, seg);
            else Modify(ls[k], l, mid, l, mid, s[k]), s[k] = seg;
        }
        return ;
    }
    if(l == r) return ;
    if(ql <= mid) Modify(ls[k], l, mid, ql, qr, seg);
    if(qr  > mid) Modify(rs[k], mid + 1, r, ql, qr, seg);
}
signed main() {
    M = read();
    for(int i = 1, lastans = 0; i <= M; i++) {
        int opt = read();
        if(!opt) {
            int k = read(), x = (k + lastans - 1) % 39989 + 1;
            ret.fi = 0; ret.se = 0;
            Query(root, 1, N, x);
            printf("%d
", lastans = (mx[x].fi > ret.fi ? mx[x].se : ret.se));
        } else {
            int x0 = (read() + lastans - 1) % 39989 + 1, y0 = (read() + lastans - 1) % Lim + 1,
                x1 = (read() + lastans - 1) % 39989 + 1, y1 = (read() + lastans - 1) % Lim + 1;
            if(x0 > x1) swap(x0, x1), swap(y0, y1);
            if(x0 == x1 && chmax(mx[x0].fi, max(y0, y1))) mx[x0].se = i;
            pdd li = get(x0, y0, x1, y1);
            Modify(root, 1, N, x0, x1, {li.fi, li.se, ++cnt});
        }
    }
    return 0;
}

相關文章