QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#426753#7520. Monster GeneratorpandapythonerWA 0ms3652kbC++205.1kb2024-05-31 19:42:202024-05-31 19:42:21

Judging History

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

  • [2024-05-31 19:42:21]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3652kb
  • [2024-05-31 19:42:20]
  • 提交

answer

#include <bits/stdc++.h>


using namespace std;


#define ll __int128_t
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()


const ll inf = 1e18;
mt19937 rnd(234);
const ll mod = 1e9 + 7;


struct line {
    ll k, b;

    ll operator()(ll x) const {
        return k * x + b;
    }

    ll sign(ll x) {
        ll val = k * x + b;
        if (val > 0) {
            return 1;
        }
        if (val < 0) {
            return -1;
        }
        if (k > 0) {
            return 1;
        }
        return -1;
    }
};


line operator-(const line& a) {
    return line{ -a.k, -a.b };
}


line operator+(const line& a, const line& b) {
    return line{ a.k + b.k, a.b + b.b };
}


line operator-(const line& a, const line& b) {
    return line{ a.k - b.k, a.b - b.b };
}


ll intersect(const line& a, const line& b) {
    assert(a.k != b.k);
    ll p = (b.b - a.b);
    ll q = (a.k - b.k);
    if (q < 0) {
        p *= -1;
        q *= -1;
    }
    ll md = (p % q + q) % q;
    return (p - md) / q + (md == 0 ? 0 : 1);
}


istream& operator>>(istream& in, ll& x) {
    unsigned long long f;
    in >> f;
    x = f;
    return in;
}


ostream& operator<<(ostream& out, ll y) {
    unsigned long long f = y;
    out << f;
    return out;
}





int n, m;
vector<ll> a, da, b, db;
vector<line> lna, lnb, sm;


unsigned long long solve() {
    unsigned long long rs = 0;
    ll mod = 1;
    for (int i = 0; i < 64; i += 1) {
        mod *= 2;
    }
    vector<ll> aboba = { 0, m + 1 };
    lna.resize(n);
    lnb.resize(n);
    sm.resize(n);
    for (int i = 0; i < n; i += 1) {
        lna[i] = line{ da[i], a[i] };
        lnb[i] = line{ db[i], b[i] };
        sm[i] = -lna[i] + lnb[i];
    }
    for (int i = 0; i < n; i += 1) {
        if (da[i] != db[i]) {
            aboba.push_back(intersect(lna[i], lnb[i]));
        }
        for (int j = i + 1; j < n; j += 1) {
            if (lna[i].k != lna[j].k) {
                aboba.push_back(intersect(lna[i], lna[j]));
            }
            if (lnb[i].k != lnb[j].k) {
                aboba.push_back(intersect(lnb[i], lnb[j]));
            }
        }
    }
    sort(all(aboba));
    for (int i = 0; i < (int)aboba.size() - 1; i += 1) {
        ll l = aboba[i];
        ll r = aboba[i + 1];
        if (l < 0 or r > m + 1 or l == r) {
            continue;
        }
        vector<int> p(n);
        for (int i = 0; i < n; i += 1) {
            p[i] = i;
        }
        sort(all(p), [&](int x, int y) {
            if (sm[x].sign(l) != sm[y].sign(l)) {
                return sm[x].sign(l) > sm[y].sign(l);
            }
            if (sm[x].sign(l) == 1) {
                return lna[x](l) < lnb[y](l);
            }
            return lnb[x](l) > lnb[y](l);
            });
        line acc{ 0, 0 };
        vector<line> lines;
        lines.reserve(n + 1);
        lines.push_back(line{ 0, 0 });
        for (auto i : p) {
            acc = acc + lna[i];
            lines.push_back(acc);
            acc = acc - lnb[i];
        }
        sort(all(lines), [&](const line& a, const line& b) {
            return a.k < b.k;
            });
        vector<pair<ll, line>> stck;
        for (auto ln : lines) {
            if (stck.empty()) {
                stck.push_back(make_pair(l, ln));
                continue;
            }
            if (stck.back().second.k == ln.k and stck.back().second.b >= ln.b) {
                continue;
            }
            while (!stck.empty()) {
                if (stck.back().second(stck.back().first) > ln(stck.back().first)) {
                    break;
                }
                stck.pop_back();
            }
            if (stck.empty()) {
                stck.push_back(make_pair(l, ln));
            } else {
                ll bg = intersect(ln, stck.back().second);
                stck.push_back(make_pair(bg, ln));
            }
        }
        for (int j = 0; j < (int)stck.size(); j += 1) {
            ll tl = max(l, stck[j].first);
            ll tr = r;
            if (j + 1 < (int)stck.size()) {
                tr = min(tr, stck[j + 1].first);
            }
            if (tl >= tr) {
                continue;
            }
            auto ln = stck[j].second;
            ll p = (tr - tl);
            ll q = (ln(tl) + ln(tr - 1));
            assert(ln(tl) >= 0 and ln(tr - 1) >= 0);
            if (p % 2 == 0) {
                p /= 2;
            } else if (q % 2 == 0) {
                q /= 2;
            } else {
                assert(0);
            }
            p &= mod - 1;
            q &= mod - 1;
            rs += ((unsigned long long)p) * ((unsigned long long)q);
        }
    }
    return rs;
}


int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }
    cin >> n >> m;
    a.resize(n);
    da.resize(n);
    b.resize(n);
    db.resize(n);
    for (int i = 0; i < n; i += 1) {
        cin >> a[i] >> da[i] >> b[i] >> db[i];
    }
    auto rs = solve();
    cout << rs << "\n";
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3640kb

input:

3 5
3 1 5 2
4 2 1 3
1 9 100 1

output:

113

result:

ok single line: '113'

Test #2:

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

input:

3 100000000
3 1 5 2
4 2 1 3
1 9 100 1

output:

35000000549999998

result:

ok single line: '35000000549999998'

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3652kb

input:

10 1000000000000000000
776874380544333 197 471391764744275 33
159838820333814 107 677112662750393 41
962335658276824 48 255593531071176 11
127404116579775 209 268525254990127 34
647620110614714 76 897947476313307 13
146196843402516 221 772928712898807 39
637929916804442 2 716937021892338 15
64200226...

output:

0

result:

wrong answer 1st lines differ - expected: '17883317185357051350', found: '0'