Submission #903077

# Submission time Handle Problem Language Result Execution time Memory
903077 2024-01-11T05:32:49 Z box Harvest (JOI20_harvest) C++17
100 / 100
616 ms 121332 KB
#include <bits/stdc++.h>
using namespace std;
 
#ifdef LOCAL
template <class T, class... U> void bug_h(const T& t, const U&... u) { cerr << t; ((cerr << " | " << u), ...); cerr << endl; }
#define bug(...) cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: ", bug_h(__VA_ARGS__)
#else
#define cerr if (0) cerr
#define bug(...)
#endif
 
#define ar array
#define all(v) std::begin(v), std::end(v)
#define sz(v) int(std::size(v))
typedef long long i64;
typedef vector<int> vi;
typedef vector<i64> v64;
typedef pair<int, int> pi;
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
 
template <class K> using oset = tree<K, null_type, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
 
struct DS {
    oset<pair<i64, int>> S; i64 tag = 0;
    constexpr int size() { return sz(S); }
    void insert(i64 v) { S.insert({v - tag, sz(S)}); }
    void extend(i64 v) { tag += v; }
    int num_leq(i64 v) { return S.order_of_key({v - tag, INT_MAX}); }
    void pr() {
        for (auto [k,v] : S) cout << k+tag << ' ';
        cout << '\n';
    }
};
void mrg_self(DS &me, DS &ds) {
    if (me.size() < ds.size()) me.S.swap(ds.S), swap(me.tag, ds.tag);
    for (auto [k, v] : exchange(ds.S, {})) me.insert(k+ds.tag);
}
 
i64 fdiv(i64 a, i64 b) {
    return a/b - ((a^b) < 0 && a%b);
}
 
int N, M, L, C, Q;
vi A, B, P;
vector<vi> adj;
vector<DS> ds;
v64 D, pf;
vector<v64> orig;
vi done, on_cyc;
vector<vi> qry;
v64 T, ans;
pi banned;
bool only_cyc;
 
void dfs(int i) {
    done[i] = 1;
    for (int j : adj[i]) {
        if (only_cyc && !on_cyc[j]) continue;
        if (!only_cyc && on_cyc[j]) {
            bug(j);
        }
        if (pi(i,j) == banned) continue;
        // bug("EDGE", i, j);
        dfs(j);
        mrg_self(ds[i], ds[j]);
    }
    for (auto x : orig[i]) ds[i].insert(x);
    for (auto x : qry[i]) ans[x] = ds[i].num_leq(T[x]);
    ds[i].extend(D[i]);
}
 
int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> N >> M >> L >> C;
    A = vi(N), B = vi(M);
    for (auto &x : A) cin >> x;
    for (auto &x : B) cin >> x;
    orig.resize(N);
    ds.resize(N);
    for (auto x : B) {
        int y = x;
        if (y < A[0]) y += L;
        int p = upper_bound(all(A), y)-begin(A)-1;
        // bug(p, y-A[p]);
        orig.at(p).push_back(y-A[p]);
    }
    P = vi(N), D = v64(N);
    adj.resize(N);
    for (int i = 0; i < N; i++) {
        int y = (A[i]-C)%L;
        if (y < 0) y += L;
        if (y < A[0]) y += L;
        int p = upper_bound(all(A), y)-begin(A)-1;
        assert(0 <= p && p < N);
        P[i] = p;
        D[i] = C + (y-A[p]);
        bug(i+1, P[i]+1, D[i]);
        adj.at(p).push_back(i);
    }
    cin >> Q;
    qry.resize(N);
    ans = T = v64(Q, -i64(1e18));
    for (int i = 0; i < Q; i++) {
        int v; cin >> v >> T[i], v--;
        qry.at(v).push_back(i);
    }
    done = on_cyc = vi(N);
    pf.resize(N);
    for (int i = 0; i < N; i++) {
        if (done[i]) continue;
        int x = i, y = P.at(i);
        while (x != y) x = P.at(x), y = P.at(P.at(y));
        vi cyc;
        do {
            done[x] = 1;
            cyc.push_back(x);
            x = P[x];
        } while (!done[x]);
        i64 len = 0;
        for (int x : cyc) {
            pf[x] = len;
            len += D[x];
        }
        for (auto x : cyc) on_cyc.at(x) = 1;
        // cerr << "NODE " << x+1 << endl;
        // cerr << "DONE" << endl;
        v64 upds;
        int funny = cyc.back();
        bug(funny+1);
        banned = {P.at(funny), funny};
        only_cyc = 0;
        for (int x : cyc) for (auto z : orig[x]) upds.push_back(z+len-pf.at(x));
        for (auto x : cyc) {
            for (int y : adj[x]) if (!on_cyc[y]) {
                dfs(y);
                for (auto [k, v] : ds[y].S) {
                    orig[x].push_back(k+ds[y].tag);
                    upds.push_back(k+ds[y].tag+len-pf.at(x));
                }
            }
        }
        only_cyc = 1;
        dfs(funny);
        vector<pair<i64, int>> qrys;
        for (int x : cyc) for (int y : qry[x]) qrys.push_back({T[y]-pf.at(x), y});
        sort(all(qrys));
        sort(all(upds), greater<>());
        DS ds; i64 con = 0;
        for (auto [v, y] : qrys) {
            while (sz(upds) && upds.back() <= v) {
                bug(upds.back());
                ds.insert(upds.back()%len);
                con += fdiv(upds.back(), len);
                upds.pop_back();
            }
            bug(y, v);
            ans.at(y) += fdiv(v,len)*ds.size()-con+ds.num_leq(v%len);
        }
 
    }
    for (auto x : ans) cout << x << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 612 KB Output is correct
2 Correct 3 ms 1380 KB Output is correct
3 Correct 4 ms 1632 KB Output is correct
4 Correct 5 ms 1656 KB Output is correct
5 Correct 4 ms 1884 KB Output is correct
6 Correct 6 ms 1884 KB Output is correct
7 Correct 5 ms 1884 KB Output is correct
8 Correct 3 ms 1628 KB Output is correct
9 Correct 5 ms 1628 KB Output is correct
10 Correct 4 ms 1640 KB Output is correct
11 Correct 4 ms 1640 KB Output is correct
12 Correct 4 ms 1820 KB Output is correct
13 Correct 6 ms 2152 KB Output is correct
14 Correct 5 ms 1696 KB Output is correct
15 Correct 5 ms 1896 KB Output is correct
16 Correct 5 ms 1920 KB Output is correct
17 Correct 5 ms 1896 KB Output is correct
18 Correct 5 ms 1676 KB Output is correct
19 Correct 5 ms 1896 KB Output is correct
20 Correct 6 ms 1728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 10320 KB Output is correct
2 Correct 196 ms 53988 KB Output is correct
3 Correct 175 ms 55136 KB Output is correct
4 Correct 174 ms 56360 KB Output is correct
5 Correct 237 ms 78116 KB Output is correct
6 Correct 244 ms 84700 KB Output is correct
7 Correct 200 ms 56772 KB Output is correct
8 Correct 204 ms 57060 KB Output is correct
9 Correct 275 ms 89732 KB Output is correct
10 Correct 185 ms 86780 KB Output is correct
11 Correct 377 ms 90444 KB Output is correct
12 Correct 344 ms 90204 KB Output is correct
13 Correct 439 ms 88520 KB Output is correct
14 Correct 311 ms 86672 KB Output is correct
15 Correct 237 ms 76804 KB Output is correct
16 Correct 175 ms 73552 KB Output is correct
17 Correct 200 ms 73660 KB Output is correct
18 Correct 89 ms 26192 KB Output is correct
19 Correct 125 ms 26056 KB Output is correct
20 Correct 155 ms 61828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 612 KB Output is correct
2 Correct 3 ms 1380 KB Output is correct
3 Correct 4 ms 1632 KB Output is correct
4 Correct 5 ms 1656 KB Output is correct
5 Correct 4 ms 1884 KB Output is correct
6 Correct 6 ms 1884 KB Output is correct
7 Correct 5 ms 1884 KB Output is correct
8 Correct 3 ms 1628 KB Output is correct
9 Correct 5 ms 1628 KB Output is correct
10 Correct 4 ms 1640 KB Output is correct
11 Correct 4 ms 1640 KB Output is correct
12 Correct 4 ms 1820 KB Output is correct
13 Correct 6 ms 2152 KB Output is correct
14 Correct 5 ms 1696 KB Output is correct
15 Correct 5 ms 1896 KB Output is correct
16 Correct 5 ms 1920 KB Output is correct
17 Correct 5 ms 1896 KB Output is correct
18 Correct 5 ms 1676 KB Output is correct
19 Correct 5 ms 1896 KB Output is correct
20 Correct 6 ms 1728 KB Output is correct
21 Correct 112 ms 10320 KB Output is correct
22 Correct 196 ms 53988 KB Output is correct
23 Correct 175 ms 55136 KB Output is correct
24 Correct 174 ms 56360 KB Output is correct
25 Correct 237 ms 78116 KB Output is correct
26 Correct 244 ms 84700 KB Output is correct
27 Correct 200 ms 56772 KB Output is correct
28 Correct 204 ms 57060 KB Output is correct
29 Correct 275 ms 89732 KB Output is correct
30 Correct 185 ms 86780 KB Output is correct
31 Correct 377 ms 90444 KB Output is correct
32 Correct 344 ms 90204 KB Output is correct
33 Correct 439 ms 88520 KB Output is correct
34 Correct 311 ms 86672 KB Output is correct
35 Correct 237 ms 76804 KB Output is correct
36 Correct 175 ms 73552 KB Output is correct
37 Correct 200 ms 73660 KB Output is correct
38 Correct 89 ms 26192 KB Output is correct
39 Correct 125 ms 26056 KB Output is correct
40 Correct 155 ms 61828 KB Output is correct
41 Correct 489 ms 91736 KB Output is correct
42 Correct 254 ms 77748 KB Output is correct
43 Correct 155 ms 61988 KB Output is correct
44 Correct 424 ms 79476 KB Output is correct
45 Correct 523 ms 115912 KB Output is correct
46 Correct 414 ms 116660 KB Output is correct
47 Correct 486 ms 117488 KB Output is correct
48 Correct 423 ms 115572 KB Output is correct
49 Correct 525 ms 115880 KB Output is correct
50 Correct 379 ms 89204 KB Output is correct
51 Correct 587 ms 101080 KB Output is correct
52 Correct 615 ms 119124 KB Output is correct
53 Correct 616 ms 119136 KB Output is correct
54 Correct 600 ms 119872 KB Output is correct
55 Correct 467 ms 107872 KB Output is correct
56 Correct 548 ms 119008 KB Output is correct
57 Correct 486 ms 107104 KB Output is correct
58 Correct 542 ms 121332 KB Output is correct
59 Correct 403 ms 105388 KB Output is correct
60 Correct 450 ms 106068 KB Output is correct
61 Correct 420 ms 106064 KB Output is correct
62 Correct 505 ms 100536 KB Output is correct
63 Correct 350 ms 68368 KB Output is correct
64 Correct 434 ms 68492 KB Output is correct
65 Correct 361 ms 68604 KB Output is correct
66 Correct 490 ms 68256 KB Output is correct
67 Correct 313 ms 55808 KB Output is correct
68 Correct 509 ms 67812 KB Output is correct
69 Correct 556 ms 107756 KB Output is correct
70 Correct 328 ms 91712 KB Output is correct
71 Correct 511 ms 105688 KB Output is correct
72 Correct 605 ms 106056 KB Output is correct