QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#532225#2342. Directing RainfallSwarthmore#AC ✓3200ms308892kbC++207.1kb2024-08-25 01:41:592024-08-25 01:42:01

Judging History

你现在查看的是最新测评结果

  • [2024-08-25 01:42:01]
  • 评测
  • 测评结果:AC
  • 用时:3200ms
  • 内存:308892kb
  • [2024-08-25 01:41:59]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a) - 1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define sz(x) (int)(x).size()
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

const char nl = '\n';


const ll id= 0;
const ll SZ = 131072*16;

ll sum[2*SZ], lazy[2*SZ][4];


void push(int ind, ll L, ll R) {
    /*sum[index] = combineUpd(sum[index], lazy[index]);
    if (L != R) lazy[2*index] = combineUpd(lazy[2*index], lazy[index]), lazy[2*index+1] = combineUpd(lazy[2*index+1], lazy[index]);
    lazy[index] = id;*/
    int indL = 2*ind, indR = 2*ind+1;
    sum[ind] += lazy[ind][0];
    if (L != R) {
        lazy[indL][0] += lazy[ind][0];
        lazy[indL][1] += lazy[ind][0];
        lazy[indR][0] += lazy[ind][0];
        lazy[indR][1] += lazy[ind][0];
    }
    sum[ind] = min(sum[ind], lazy[ind][1]);
    if (L != R) {
        lazy[indL][1] = min(lazy[indL][1], lazy[ind][1]);
        lazy[indR][1] = min(lazy[indR][1], lazy[ind][1]);
    }

    if (lazy[ind][2]) {
        if (L != R) {
            lazy[indL][2] = 1;
            lazy[indR][2] = 1;
            ll val = min(sum[indL]+lazy[indL][0], lazy[indL][1]);
            lazy[indR][1] = min(lazy[indR][1], val);
        }
    }
    if (lazy[ind][3]) {
        if (L != R) {
            lazy[indL][3] = 1;
            lazy[indR][3] = 1;
            ll val = min(sum[indR]+lazy[indR][0], lazy[indR][1]);
            lazy[indL][1] = min(lazy[indL][1], val);
        }
    }


    lazy[ind][0] = 0;
    lazy[ind][1] = 1e18;
    lazy[ind][2] = 0;
    lazy[ind][3] = 0;

}

void pull(int index) {
    sum[index] = min(sum[2*index], sum[2*index+1]);
}

ll query(int lo, int hi, int index = 1, ll L = 0, ll R = SZ-1) {
    push(index, L, R);
    if (lo > R || L > hi) return 1e18;
    if (lo <= L && R <= hi) return sum[index];

    int M = (L+R) / 2;
    return min(query(lo, hi, 2*index, L, M), query(lo, hi, 2*index+1, M+1, R));
}

ll update(int lo, int hi, ll increase, int ty, ll cur = 1e18, int index = 1,
        ll L = 0, ll R = SZ-1) {
    push(index, L, R);
    if (hi < L || R < lo) return 1e18;
    if (lo <= L && R <= hi) {
        lazy[index][ty] = increase;
        lazy[index][1] = min(lazy[index][1], cur);
        push(index, L, R);
        /*if (ty == 3) {
            cout << L << " " << R << " UPD " << sum[index] << " " << cur << endl;
        }*/
        return min(sum[index], cur);
    }

    int M = (L+R) / 2;
    /*if (ty == 3) {
        cout << L << " " << R << " " << cur << endl;
    }*/
    if (ty == 0) { 
        update(lo, hi, increase, ty, cur, 2*index, L, M); update(lo, hi, increase, ty, cur, 2*index+1, M+1, R);
    } else if (ty == 2) {
        cur = min(cur, update(lo, hi, increase, ty, cur, 2*index, L, M));
        cur = min(cur, update(lo, hi, increase, ty, cur, 2*index+1, M+1, R));
    } else if (ty == 3) {
        cur = min(cur, update(lo, hi, increase, ty, cur, 2*index+1, M+1, R));
        cur = min(cur, update(lo, hi, increase, ty, cur, 2*index, L, M));
    }
    /*if (ty == 3) {
        cout << "DONE " << L << " " << R << " " << cur << endl;
    }*/

    pull(index);
    return cur;
}

ll X;

struct Line {
    ll x1, x2, y1, y2, v;

    Line (ll _x1, ll _x2, ll _y1, ll _y2, ll _v) {
        x1 = _x1;
        x2 = _x2;
        y1 = _y1;
        y2 = _y2;
        v = _v;
        if (x1 > x2) {
            swap(x1, x2);
            swap(y1, y2);
        }
    }

    bool operator<(const Line &l2) const{
        __int128 v1 = (X - x1) * (y2 - y1) + y1 * (x2 - x1);
        __int128 v2 = (X - l2.x1) * (l2.y2 - l2.y1) + l2.y1 * (l2.x2 - l2.x1);
        v1 *= l2.x2 - l2.x1;
        v2 *= x2 - x1;
        return v1 < v2;
    }
};


void solve() {
    F0R(i, 2*SZ) lazy[i][1] = 1e18;
    ll totL, totR; cin >> totL >> totR;
    int N; cin >> N;
    if (N == 0) {
        cout << 0 << nl; return;
    }
    vector<vi> graph(N);
    ll X1[N], Y1[N], X2[N], Y2[N];
    F0R(i, N) cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
    vector<array<ll, 3>> ev;
    F0R(i, N) {
        ev.pb({min(X1[i], X2[i]), 0, i});
        ev.pb({max(X1[i], X2[i]), 1, i});
    }
    sort(all(ev));
    set<Line> lines;
    if (totL < ev[0][0]) {
        cout << 0 << nl; return;
    }
    if (totR > ev.back()[0]) {
        cout << 0 << nl; return;
    }
    F0R(i, sz(ev)) {
        auto a = ev[i];
        X = a[0];
        if (a[1] == 0) {
            ll ind = a[2];
            Line cur(X1[a[2]], X2[a[2]], Y1[a[2]], Y2[a[2]], ind);
            lines.ins(cur);
            auto it = lines.find(cur);
            if (it != lines.begin()) {
                it--;
                graph[a[2]].pb(it->v);
                it++;
            }
            it++;
            if (it != lines.end()) {
                graph[it->v].pb(a[2]);
            }
        } else {
            Line cur(X1[a[2]], X2[a[2]], Y1[a[2]], Y2[a[2]], a[2]);
            lines.erase(cur);
        }
        if (sz(lines) == 0 && i < sz(ev) - 1 && max(totL, a[0]) < min(totR, ev[i+1][0])) {
            cout << 0 << nl; return;
        }
        /*trav(a, lines) {
            cout << a.v << " ";
        }
        cout << nl;*/
    }

    vi ord;
    int ind[N]; F0R(i, N) ind[i] = 0;
    F0R(i, N) {
        trav(a, graph[i]) ind[a]++;
    }
    queue<int> q; F0R(i, N) if (ind[i] == 0) q.push(i);
    while (!q.empty()) {
        int v = q.front(); q.pop();
        ord.pb(v);
        trav(a, graph[v]) {
            ind[a]--;
            if (ind[a] == 0) {
                q.push(a);
            }
        }
    }

    vl coords;
    totL *= 2; totR *= 2;
    F0R(i, N) {
        X1[i] *= 2; X2[i] *= 2;
    }
    coords.pb(totL); coords.pb(totR);
    F0R(i, N) {
        coords.pb(X1[i]); coords.pb(X2[i]); 
        coords.pb(X1[i]-1); coords.pb(X2[i]+1);
    }
    sort(all(coords)); coords.resize(unique(all(coords)) - coords.begin());
    totL = lb(all(coords), totL) - coords.begin();
    totR = lb(all(coords), totR) - coords.begin();
    F0R(i, N) {
        X1[i] = lb(all(coords), X1[i]) - coords.begin();
        X2[i] = lb(all(coords), X2[i]) - coords.begin();
    }

    update(0, totL-1, 1e18, 0);
    update(totR+1, SZ-1, 1e18, 0);

    trav(a, ord) {
        //cout << X1[a] << " " << X2[a] << nl;
        if (X1[a] < X2[a]) {
            update(X1[a], X2[a], 1, 3);
            update(X1[a]+1, X2[a], 1, 0);
        } else {
            update(X2[a], X1[a], 1, 2);
            /*F0R(i, 12) {
                cout << min(10ll, query(i, i)) << " ";
            }
            cout << nl;*/
            update(X2[a], X1[a]-1, 1, 0);
        }
        /*F0R(i, 12) {
            cout << min(10ll, query(i, i)) << " ";
        }
        cout << nl;*/
    }
    cout << query(totL, totR) << nl;


}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	solve();
	return 0;
}

Details

Test #1:

score: 100
Accepted
time: 3ms
memory: 135404kb

Test #2:

score: 0
Accepted
time: 3ms
memory: 135976kb

Test #3:

score: 0
Accepted
time: 12ms
memory: 135900kb

Test #4:

score: 0
Accepted
time: 4ms
memory: 135288kb

Test #5:

score: 0
Accepted
time: 3ms
memory: 136788kb

Test #6:

score: 0
Accepted
time: 4ms
memory: 136796kb

Test #7:

score: 0
Accepted
time: 8ms
memory: 135928kb

Test #8:

score: 0
Accepted
time: 8ms
memory: 134792kb

Test #9:

score: 0
Accepted
time: 10ms
memory: 135724kb

Test #10:

score: 0
Accepted
time: 8ms
memory: 134824kb

Test #11:

score: 0
Accepted
time: 4ms
memory: 135744kb

Test #12:

score: 0
Accepted
time: 1963ms
memory: 308892kb

Test #13:

score: 0
Accepted
time: 0ms
memory: 135952kb

Test #14:

score: 0
Accepted
time: 4ms
memory: 136172kb

Test #15:

score: 0
Accepted
time: 11ms
memory: 135912kb

Test #16:

score: 0
Accepted
time: 14ms
memory: 134896kb

Test #17:

score: 0
Accepted
time: 3ms
memory: 135096kb

Test #18:

score: 0
Accepted
time: 3ms
memory: 135420kb

Test #19:

score: 0
Accepted
time: 4ms
memory: 135640kb

Test #20:

score: 0
Accepted
time: 0ms
memory: 135748kb

Test #21:

score: 0
Accepted
time: 8ms
memory: 135580kb

Test #22:

score: 0
Accepted
time: 4ms
memory: 135984kb

Test #23:

score: 0
Accepted
time: 14ms
memory: 136336kb

Test #24:

score: 0
Accepted
time: 4ms
memory: 136300kb

Test #25:

score: 0
Accepted
time: 3ms
memory: 136284kb

Test #26:

score: 0
Accepted
time: 4ms
memory: 135104kb

Test #27:

score: 0
Accepted
time: 0ms
memory: 135212kb

Test #28:

score: 0
Accepted
time: 7ms
memory: 136404kb

Test #29:

score: 0
Accepted
time: 8ms
memory: 135352kb

Test #30:

score: 0
Accepted
time: 4ms
memory: 136700kb

Test #31:

score: 0
Accepted
time: 12ms
memory: 135676kb

Test #32:

score: 0
Accepted
time: 7ms
memory: 136000kb

Test #33:

score: 0
Accepted
time: 0ms
memory: 135940kb

Test #34:

score: 0
Accepted
time: 4ms
memory: 134716kb

Test #35:

score: 0
Accepted
time: 1921ms
memory: 301444kb

Test #36:

score: 0
Accepted
time: 391ms
memory: 213780kb

Test #37:

score: 0
Accepted
time: 1960ms
memory: 305192kb

Test #38:

score: 0
Accepted
time: 1976ms
memory: 305016kb

Test #39:

score: 0
Accepted
time: 1374ms
memory: 261000kb

Test #40:

score: 0
Accepted
time: 1097ms
memory: 263312kb

Test #41:

score: 0
Accepted
time: 3200ms
memory: 285776kb

Test #42:

score: 0
Accepted
time: 3074ms
memory: 282896kb

Test #43:

score: 0
Accepted
time: 0ms
memory: 135012kb

Test #44:

score: 0
Accepted
time: 4ms
memory: 135108kb

Test #45:

score: 0
Accepted
time: 3ms
memory: 136460kb

Test #46:

score: 0
Accepted
time: 7ms
memory: 136580kb

Test #47:

score: 0
Accepted
time: 0ms
memory: 136276kb

Test #48:

score: 0
Accepted
time: 8ms
memory: 135220kb

Test #49:

score: 0
Accepted
time: 10ms
memory: 135272kb

Test #50:

score: 0
Accepted
time: 17ms
memory: 135216kb

Test #51:

score: 0
Accepted
time: 3ms
memory: 135836kb

Test #52:

score: 0
Accepted
time: 4ms
memory: 135696kb

Test #53:

score: 0
Accepted
time: 8ms
memory: 135160kb

Test #54:

score: 0
Accepted
time: 7ms
memory: 135032kb

Test #55:

score: 0
Accepted
time: 4ms
memory: 135300kb

Test #56:

score: 0
Accepted
time: 4ms
memory: 134940kb

Test #57:

score: 0
Accepted
time: 4ms
memory: 135796kb

Test #58:

score: 0
Accepted
time: 0ms
memory: 135596kb

Test #59:

score: 0
Accepted
time: 4ms
memory: 136196kb

Test #60:

score: 0
Accepted
time: 15ms
memory: 135888kb

Test #61:

score: 0
Accepted
time: 12ms
memory: 134816kb

Test #62:

score: 0
Accepted
time: 0ms
memory: 135300kb

Test #63:

score: 0
Accepted
time: 9ms
memory: 135572kb

Test #64:

score: 0
Accepted
time: 12ms
memory: 135720kb

Test #65:

score: 0
Accepted
time: 12ms
memory: 136488kb

Test #66:

score: 0
Accepted
time: 12ms
memory: 135696kb

Test #67:

score: 0
Accepted
time: 16ms
memory: 135828kb

Test #68:

score: 0
Accepted
time: 32ms
memory: 137968kb

Test #69:

score: 0
Accepted
time: 36ms
memory: 138336kb

Test #70:

score: 0
Accepted
time: 26ms
memory: 138368kb

Test #71:

score: 0
Accepted
time: 41ms
memory: 139240kb

Test #72:

score: 0
Accepted
time: 414ms
memory: 165248kb

Test #73:

score: 0
Accepted
time: 424ms
memory: 165068kb

Test #74:

score: 0
Accepted
time: 446ms
memory: 165376kb

Test #75:

score: 0
Accepted
time: 437ms
memory: 165480kb

Test #76:

score: 0
Accepted
time: 423ms
memory: 165092kb

Test #77:

score: 0
Accepted
time: 3035ms
memory: 279976kb

Test #78:

score: 0
Accepted
time: 2976ms
memory: 280088kb

Test #79:

score: 0
Accepted
time: 2822ms
memory: 276948kb

Test #80:

score: 0
Accepted
time: 2846ms
memory: 283324kb

Test #81:

score: 0
Accepted
time: 2816ms
memory: 280528kb

Test #82:

score: 0
Accepted
time: 2858ms
memory: 280508kb

Test #83:

score: 0
Accepted
time: 2567ms
memory: 281252kb

Test #84:

score: 0
Accepted
time: 2670ms
memory: 280456kb

Test #85:

score: 0
Accepted
time: 2695ms
memory: 282456kb

Test #86:

score: 0
Accepted
time: 2611ms
memory: 281160kb

Test #87:

score: 0
Accepted
time: 1568ms
memory: 272096kb

Test #88:

score: 0
Accepted
time: 1627ms
memory: 272764kb

Test #89:

score: 0
Accepted
time: 1826ms
memory: 272036kb

Test #90:

score: 0
Accepted
time: 2107ms
memory: 272000kb

Test #91:

score: 0
Accepted
time: 2518ms
memory: 278192kb

Test #92:

score: 0
Accepted
time: 1470ms
memory: 270848kb

Test #93:

score: 0
Accepted
time: 1446ms
memory: 270836kb

Test #94:

score: 0
Accepted
time: 1514ms
memory: 271280kb

Test #95:

score: 0
Accepted
time: 1535ms
memory: 270372kb

Test #96:

score: 0
Accepted
time: 1341ms
memory: 271380kb