QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#252990#6327. Count Arithmetic Progressionstd_abs#WA 47ms7792kbC++143.3kb2023-11-16 16:22:062023-11-16 16:22:07

Judging History

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

  • [2023-11-16 16:22:07]
  • 评测
  • 测评结果:WA
  • 用时:47ms
  • 内存:7792kb
  • [2023-11-16 16:22:06]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
const int mod = 998244353, N = 200005;

struct Pt {
    ll x, y;
    Pt (ll _x, ll _y) : x(_x), y(_y) {}
    Pt operator + (const Pt &o) {
        return Pt(x + o.x, y + o.y);
    }
    Pt operator - (const Pt &o) {
        return Pt(x - o.x, y - o.y);
    }
    ll operator * (const Pt &o) {
        return x * o.x + y * o.y;
    }
    ll operator ^ (const Pt &o) {
        return x * o.y - y * o.x;
    }
};

ll myfloor(ll a, ll b) {
    return a / b - (a < 0 && a % b);
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int n;
    cin >> n;
    vector <ll> l(n), r(n);
    for (int i = 0; i < n; ++i) {
        cin >> l[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> r[i];
    }
    auto check = [&](Pt a, Pt b, Pt c) {
        // (a.y - c.y) / (c.x - a.x) < (a.y - b.y) / (b.x - a.x)
        return (a.y - c.y) * (b.x - a.x) <= (a.y - b.y) * (c.x - a.x);
    };
    vector <Pt> mx, mn;
    for (int i = 0; i < n; ++i) {
        while (mx.size() >= 2 && check(mx[mx.size() - 2], mx.back(), Pt(i, l[i]))) {
            mx.pop_back();
        }
        mx.emplace_back(i, l[i]);
    }
    for (int i = n - 1; ~i; --i) {
        while (mn.size() >= 2 && check(mn[mn.size() - 2], mn.back(), Pt(i, r[i]))) {
            mn.pop_back();
        }
        mn.emplace_back(i, r[i]);
    }

    vector <ll> mx_inter(mx.size()), mn_inter(mn.size());
    for (int i = 0; i + 1 < mx.size(); ++i) {
        mx_inter[i] = myfloor((mx[i].y - mx[i + 1].y), (mx[i + 1].x - mx[i].x));
    }
    for (int i = 0; i + 1 < mn.size(); ++i) {
        mn_inter[i] = myfloor((mn[i + 1].y - mn[i].y), (mn[i].x - mn[i + 1].x));
    }

    ll inv = mod + 1 >> 1;

    ll ans = 0;
    auto solve = [&](Pt up, Pt down, ll l, ll r) {
        if (l > r) {
            return;
        }
        ll d = abs(up.x - down.x);
        ll cl = up.x * l + up.y - down.x * l - down.y + 1;
        ll cr = up.x * r + up.y - down.x * r - down.y + 1;
        if (cl < 0 && cr < 0) {
            return;
        }
        if (cl >= 0 && cr >= 0) {
            (ans += ((r - l + 1) % mod) * ((cl + cr) % mod) % mod * inv) %= mod;
        } else if (cl < 0) {
            // cr >= 0
            ll len = cr / d;
            (ans += ((len + 1) % mod) * ((cr + cr - len * d) % mod) * inv) %= mod;
        } else {
            // cl >= 0
            ll len = cl / d;
            (ans += ((len + 1) % mod) * ((cl + cl - len * d) % mod) * inv) %= mod;
        }
    };

    ll cur = -1ll << 40;
    for (int i = 0, j = 0; i + 1 < mx.size() || j + 1 < mn.size(); ) {
        if (i + 1 == mx.size()) {
            solve(mn[j], mx[i], cur, mn_inter[j]), cur = mn_inter[j] + 1, j++;
        } else if (j + 1 == mn.size()) {
            solve(mn[j], mx[i], cur, mx_inter[i]), cur = mx_inter[i] + 1, i++;
        } else {
            ll to = min(mn_inter[j], mx_inter[i]);
            solve(mn[j], mx[i], cur, to), cur = to + 1;
            if (to == mn_inter[j]) {
                j++;
            }
            if (to == mx_inter[i]) {
                i++;
            }
        }
    }
    solve(mn.back(), mx.back(), cur, 1ll << 40);

    cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
5 5 2
7 6 7

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3548kb

input:

4
2 3 1 6
5 6 4 8

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 47ms
memory: 7792kb

input:

300000
121398540794 60081434790 252920491076 43666218735 95583832019 21178121624 315727133812 89837170656 138389231466 174687947367 124350262341 108583578309 289629745492 321006985392 301201031648 138149017169 255983300245 138801256482 285614769875 130583757928 261690466826 74624776159 303504239011 ...

output:

2000014

result:

ok 1 number(s): "2000014"

Test #4:

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

input:

2
1 1
1000000000000 1000000000000

output:

705055641

result:

wrong answer 1st numbers differ - expected: '276262510', found: '705055641'