Submission #354431

# Submission time Handle Problem Language Result Execution time Memory
354431 2021-01-22T00:27:29 Z 12tqian New Home (APIO18_new_home) C++17
100 / 100
3999 ms 167656 KB
#include <bits/stdc++.h>

using namespace std;

#define f1r(i, a, b) for (int i = a; i < b; i++)
#define f0r(i, a) f1r(i, 0, a)
#define trav(t, a) for (auto& t : a)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

typedef vector<int> vi; 
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef pair<ll, ll> pl;
typedef vector<pl> vpl;

const int N = 3e5 + 1;
const int M = (1 << 21);
const int INF = 1e9;

template <class T> bool ckmin(T& a, T b) { return a > b ? a = b, 1 : 0; }
template <class T> bool ckmax(T& a, T b) { return a < b ? a = b, 1 : 0; }


int get_pos(vi& v, int x) { return upper_bound(all(v), x) - v.begin(); }

template <int N> struct SegmentTree {
    vector<array<int, 4>> todo;
    // loc to loc, time to time
    vi num;
    int mn[N];

    void build(int ind, int L, int R) {
        mn[ind] = 2 * INF;
        if (L == R) return;
        int M = (L + R) >> 1;
        build(2*ind, L, M);
        build(2*ind+1, M+1, R);
    }

    void build() {
        num.pb(-INF);
        trav(a, todo) num.pb(a[2]), num.pb(a[3]+1);
        sort(all(num));
        num.erase(unique(all(num)), num.end());
        build(1, 0, sz(num)-1);
    }

    void modify(int x, int y, int t, int ind, int L, int R) {
        if (R < x || y < L) return;
        if (x <= L && R <= y) {
            ckmin(mn[ind], t);
            return;
        }
        int M = (L + R) >> 1;
        modify(x, y, t, 2*ind, L, M);
        modify(x, y, t, 2*ind+1, M+1, R);
    }

    void modify(int x, int y, int t) {
        modify(get_pos(num, x), get_pos(num, y), t, 1, 0, sz(num)-1);
    }

    int query(int x, int ind, int L, int R) {
        int ret = mn[ind];
        if (L != R) {
            int M = (L + R) >> 1;
            if (x <= M) {
                ckmin(ret, query(x, 2*ind, L, M));
            } else {
                ckmin(ret, query(x, 2*ind+1, M+1, R));
            }
        }
        return ret;
    }
    int query(int x) {
        return query(get_pos(num, x), 1, 0, sz(num)-1);
    }

};


int n, k, q; 
int ans[N]; // stores answers
map<pi, int> type[N];
vector<array<int, 5>> mod; // modifications
vector<array<int, 3>> query; // queries
SegmentTree<M> S[2];


void insert(int x, pair<pi, int> a, pair<pi, int> b) {
    if (a.f.f == -INF && b.f.f == INF) {
        S[0].todo.pb({INF, -INF, a.s, x});
        S[1].todo.pb({-INF, INF, a.s, x});
    } else {
        int m = (a.f.f+b.f.f) >> 1;
        S[0].todo.pb({m, a.f.f, a.s, x});
        S[1].todo.pb({m+1, b.f.f, a.s, x});
    }
}
pair<pi, int> get_next(map<pi, int>& a, map<pi, int>::iterator b) {
    b = next(b);
    if (b == a.end()) return {{INF, -1}, 1};
    else return *b;
}

void init() {
    cin.tie(0)->sync_with_stdio();
    cin >> n >> k >> q;
    f0r(i, n) {
        int x, t, a, b; cin >> x >> t >> a >> b;
        mod.push_back({a, 1, t, x, i});
        mod.push_back({b+1, -1, t, x, i});
    }
    f1r(i, 1, k+1) {
        type[i][{-INF, -1}] = 1;
    }
    sort(all(mod));
    trav(a, mod) {
        pi t = {a[3], a[4]};
        if (a[1] == 1) { // adding something
            type[a[2]][t] = 0;
            auto it = type[a[2]].find(t);
            insert(a[0]-1, *prev(it), get_next(type[a[2]], it)); // adds the things that ended
            prev(it)->s = it->s = a[0]; // this is when they started a new thing
        } else { // removing something
            auto it = type[a[2]].find(t);
            insert(a[0]-1, *prev(it), *it); 
            insert(a[0]-1, *it, get_next(type[a[2]], it));
            prev(it)->s = a[0]; // set previous time
            type[a[2]].erase(it); // get rid of thing
        }
    }
    f1r(i, 1, k+1) {
        insert(1e8, *type[i].begin(), get_next(type[i], type[i].begin())); // set the remainder that didn't get removed, which is the negative infinity
        // this is so we don't ever have nothing?
        // figure this out
    }
    
    f0r(i, q) {
        int l, y; cin >> l >> y;
        query.pb({l, y, i});
    }
} 
void solve0() {
    sort(query.rbegin(), query.rend());
    S[0].build();
    sort(all(S[0].todo));
    trav(a, query) {
        while (sz(S[0].todo)) {
            auto x = S[0].todo.back();
            if (x[0] < a[0]) break; // doesn't affect
            S[0].modify(x[2], x[3], x[1]);
            S[0].todo.pop_back();
        }
        ckmax(ans[a[2]], a[0]-S[0].query(a[1]));
    }
}
void solve1() {
    reverse(all(query));
    S[1].build();
    sort(S[1].todo.rbegin(), S[1].todo.rend());
    trav(a, query) {
        while (sz(S[1].todo)) {
            auto x = S[1].todo.back();
            if (x[0] > a[0]) break; // doesn't apply
            S[1].modify(x[2], x[3], -x[1]);
            S[1].todo.pop_back();
        }
        ckmax(ans[a[2]], -S[1].query(a[1])-a[0]);
    }

}
int main() {
    init();
    solve0();
    solve1();
    f0r(i, q) {
        if (ans[i] > 1e8) cout << -1;
        else cout << ans[i];
        cout << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14444 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 12 ms 14572 KB Output is correct
6 Correct 12 ms 14572 KB Output is correct
7 Correct 12 ms 14572 KB Output is correct
8 Correct 13 ms 14572 KB Output is correct
9 Correct 12 ms 14700 KB Output is correct
10 Correct 12 ms 14572 KB Output is correct
11 Correct 12 ms 14572 KB Output is correct
12 Correct 12 ms 14572 KB Output is correct
13 Correct 13 ms 14572 KB Output is correct
14 Correct 14 ms 14572 KB Output is correct
15 Correct 12 ms 14572 KB Output is correct
16 Correct 13 ms 14572 KB Output is correct
17 Correct 12 ms 14572 KB Output is correct
18 Correct 12 ms 14572 KB Output is correct
19 Correct 13 ms 14572 KB Output is correct
20 Correct 12 ms 14572 KB Output is correct
21 Correct 11 ms 14572 KB Output is correct
22 Correct 12 ms 14700 KB Output is correct
23 Correct 12 ms 14572 KB Output is correct
24 Correct 12 ms 14572 KB Output is correct
25 Correct 12 ms 14572 KB Output is correct
26 Correct 14 ms 14572 KB Output is correct
27 Correct 11 ms 14572 KB Output is correct
28 Correct 12 ms 14572 KB Output is correct
29 Correct 12 ms 14572 KB Output is correct
30 Correct 12 ms 14572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14444 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 12 ms 14572 KB Output is correct
6 Correct 12 ms 14572 KB Output is correct
7 Correct 12 ms 14572 KB Output is correct
8 Correct 13 ms 14572 KB Output is correct
9 Correct 12 ms 14700 KB Output is correct
10 Correct 12 ms 14572 KB Output is correct
11 Correct 12 ms 14572 KB Output is correct
12 Correct 12 ms 14572 KB Output is correct
13 Correct 13 ms 14572 KB Output is correct
14 Correct 14 ms 14572 KB Output is correct
15 Correct 12 ms 14572 KB Output is correct
16 Correct 13 ms 14572 KB Output is correct
17 Correct 12 ms 14572 KB Output is correct
18 Correct 12 ms 14572 KB Output is correct
19 Correct 13 ms 14572 KB Output is correct
20 Correct 12 ms 14572 KB Output is correct
21 Correct 11 ms 14572 KB Output is correct
22 Correct 12 ms 14700 KB Output is correct
23 Correct 12 ms 14572 KB Output is correct
24 Correct 12 ms 14572 KB Output is correct
25 Correct 12 ms 14572 KB Output is correct
26 Correct 14 ms 14572 KB Output is correct
27 Correct 11 ms 14572 KB Output is correct
28 Correct 12 ms 14572 KB Output is correct
29 Correct 12 ms 14572 KB Output is correct
30 Correct 12 ms 14572 KB Output is correct
31 Correct 616 ms 36812 KB Output is correct
32 Correct 229 ms 30308 KB Output is correct
33 Correct 595 ms 35172 KB Output is correct
34 Correct 595 ms 35036 KB Output is correct
35 Correct 610 ms 36408 KB Output is correct
36 Correct 603 ms 36540 KB Output is correct
37 Correct 514 ms 34264 KB Output is correct
38 Correct 525 ms 34136 KB Output is correct
39 Correct 459 ms 34008 KB Output is correct
40 Correct 461 ms 33900 KB Output is correct
41 Correct 473 ms 34096 KB Output is correct
42 Correct 471 ms 34008 KB Output is correct
43 Correct 231 ms 31460 KB Output is correct
44 Correct 467 ms 34264 KB Output is correct
45 Correct 505 ms 34048 KB Output is correct
46 Correct 426 ms 33960 KB Output is correct
47 Correct 335 ms 33240 KB Output is correct
48 Correct 329 ms 33112 KB Output is correct
49 Correct 371 ms 33448 KB Output is correct
50 Correct 409 ms 34012 KB Output is correct
51 Correct 371 ms 33448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1268 ms 109648 KB Output is correct
2 Correct 1235 ms 102980 KB Output is correct
3 Correct 1376 ms 155288 KB Output is correct
4 Correct 1304 ms 113376 KB Output is correct
5 Correct 1250 ms 102988 KB Output is correct
6 Correct 1250 ms 102884 KB Output is correct
7 Correct 1365 ms 155292 KB Output is correct
8 Correct 1275 ms 113596 KB Output is correct
9 Correct 1231 ms 106668 KB Output is correct
10 Correct 1195 ms 103284 KB Output is correct
11 Correct 1180 ms 101804 KB Output is correct
12 Correct 1182 ms 102768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2720 ms 119420 KB Output is correct
2 Correct 1169 ms 100268 KB Output is correct
3 Correct 2758 ms 114100 KB Output is correct
4 Correct 2554 ms 156976 KB Output is correct
5 Correct 2516 ms 121016 KB Output is correct
6 Correct 2487 ms 128548 KB Output is correct
7 Correct 2761 ms 121156 KB Output is correct
8 Correct 2717 ms 118596 KB Output is correct
9 Correct 2419 ms 158296 KB Output is correct
10 Correct 2467 ms 131052 KB Output is correct
11 Correct 2606 ms 122316 KB Output is correct
12 Correct 2768 ms 119596 KB Output is correct
13 Correct 1875 ms 118196 KB Output is correct
14 Correct 1851 ms 103932 KB Output is correct
15 Correct 2095 ms 118132 KB Output is correct
16 Correct 2113 ms 119392 KB Output is correct
17 Correct 2103 ms 117644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14444 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 12 ms 14572 KB Output is correct
6 Correct 12 ms 14572 KB Output is correct
7 Correct 12 ms 14572 KB Output is correct
8 Correct 13 ms 14572 KB Output is correct
9 Correct 12 ms 14700 KB Output is correct
10 Correct 12 ms 14572 KB Output is correct
11 Correct 12 ms 14572 KB Output is correct
12 Correct 12 ms 14572 KB Output is correct
13 Correct 13 ms 14572 KB Output is correct
14 Correct 14 ms 14572 KB Output is correct
15 Correct 12 ms 14572 KB Output is correct
16 Correct 13 ms 14572 KB Output is correct
17 Correct 12 ms 14572 KB Output is correct
18 Correct 12 ms 14572 KB Output is correct
19 Correct 13 ms 14572 KB Output is correct
20 Correct 12 ms 14572 KB Output is correct
21 Correct 11 ms 14572 KB Output is correct
22 Correct 12 ms 14700 KB Output is correct
23 Correct 12 ms 14572 KB Output is correct
24 Correct 12 ms 14572 KB Output is correct
25 Correct 12 ms 14572 KB Output is correct
26 Correct 14 ms 14572 KB Output is correct
27 Correct 11 ms 14572 KB Output is correct
28 Correct 12 ms 14572 KB Output is correct
29 Correct 12 ms 14572 KB Output is correct
30 Correct 12 ms 14572 KB Output is correct
31 Correct 616 ms 36812 KB Output is correct
32 Correct 229 ms 30308 KB Output is correct
33 Correct 595 ms 35172 KB Output is correct
34 Correct 595 ms 35036 KB Output is correct
35 Correct 610 ms 36408 KB Output is correct
36 Correct 603 ms 36540 KB Output is correct
37 Correct 514 ms 34264 KB Output is correct
38 Correct 525 ms 34136 KB Output is correct
39 Correct 459 ms 34008 KB Output is correct
40 Correct 461 ms 33900 KB Output is correct
41 Correct 473 ms 34096 KB Output is correct
42 Correct 471 ms 34008 KB Output is correct
43 Correct 231 ms 31460 KB Output is correct
44 Correct 467 ms 34264 KB Output is correct
45 Correct 505 ms 34048 KB Output is correct
46 Correct 426 ms 33960 KB Output is correct
47 Correct 335 ms 33240 KB Output is correct
48 Correct 329 ms 33112 KB Output is correct
49 Correct 371 ms 33448 KB Output is correct
50 Correct 409 ms 34012 KB Output is correct
51 Correct 371 ms 33448 KB Output is correct
52 Correct 559 ms 41684 KB Output is correct
53 Correct 548 ms 41504 KB Output is correct
54 Correct 572 ms 38848 KB Output is correct
55 Correct 493 ms 36824 KB Output is correct
56 Correct 495 ms 38484 KB Output is correct
57 Correct 483 ms 35148 KB Output is correct
58 Correct 509 ms 36900 KB Output is correct
59 Correct 513 ms 38136 KB Output is correct
60 Correct 483 ms 35032 KB Output is correct
61 Correct 250 ms 38436 KB Output is correct
62 Correct 541 ms 42452 KB Output is correct
63 Correct 538 ms 38984 KB Output is correct
64 Correct 555 ms 37460 KB Output is correct
65 Correct 516 ms 35556 KB Output is correct
66 Correct 482 ms 34392 KB Output is correct
67 Correct 262 ms 31192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14444 KB Output is correct
2 Correct 10 ms 14444 KB Output is correct
3 Correct 10 ms 14444 KB Output is correct
4 Correct 10 ms 14444 KB Output is correct
5 Correct 12 ms 14572 KB Output is correct
6 Correct 12 ms 14572 KB Output is correct
7 Correct 12 ms 14572 KB Output is correct
8 Correct 13 ms 14572 KB Output is correct
9 Correct 12 ms 14700 KB Output is correct
10 Correct 12 ms 14572 KB Output is correct
11 Correct 12 ms 14572 KB Output is correct
12 Correct 12 ms 14572 KB Output is correct
13 Correct 13 ms 14572 KB Output is correct
14 Correct 14 ms 14572 KB Output is correct
15 Correct 12 ms 14572 KB Output is correct
16 Correct 13 ms 14572 KB Output is correct
17 Correct 12 ms 14572 KB Output is correct
18 Correct 12 ms 14572 KB Output is correct
19 Correct 13 ms 14572 KB Output is correct
20 Correct 12 ms 14572 KB Output is correct
21 Correct 11 ms 14572 KB Output is correct
22 Correct 12 ms 14700 KB Output is correct
23 Correct 12 ms 14572 KB Output is correct
24 Correct 12 ms 14572 KB Output is correct
25 Correct 12 ms 14572 KB Output is correct
26 Correct 14 ms 14572 KB Output is correct
27 Correct 11 ms 14572 KB Output is correct
28 Correct 12 ms 14572 KB Output is correct
29 Correct 12 ms 14572 KB Output is correct
30 Correct 12 ms 14572 KB Output is correct
31 Correct 616 ms 36812 KB Output is correct
32 Correct 229 ms 30308 KB Output is correct
33 Correct 595 ms 35172 KB Output is correct
34 Correct 595 ms 35036 KB Output is correct
35 Correct 610 ms 36408 KB Output is correct
36 Correct 603 ms 36540 KB Output is correct
37 Correct 514 ms 34264 KB Output is correct
38 Correct 525 ms 34136 KB Output is correct
39 Correct 459 ms 34008 KB Output is correct
40 Correct 461 ms 33900 KB Output is correct
41 Correct 473 ms 34096 KB Output is correct
42 Correct 471 ms 34008 KB Output is correct
43 Correct 231 ms 31460 KB Output is correct
44 Correct 467 ms 34264 KB Output is correct
45 Correct 505 ms 34048 KB Output is correct
46 Correct 426 ms 33960 KB Output is correct
47 Correct 335 ms 33240 KB Output is correct
48 Correct 329 ms 33112 KB Output is correct
49 Correct 371 ms 33448 KB Output is correct
50 Correct 409 ms 34012 KB Output is correct
51 Correct 371 ms 33448 KB Output is correct
52 Correct 1268 ms 109648 KB Output is correct
53 Correct 1235 ms 102980 KB Output is correct
54 Correct 1376 ms 155288 KB Output is correct
55 Correct 1304 ms 113376 KB Output is correct
56 Correct 1250 ms 102988 KB Output is correct
57 Correct 1250 ms 102884 KB Output is correct
58 Correct 1365 ms 155292 KB Output is correct
59 Correct 1275 ms 113596 KB Output is correct
60 Correct 1231 ms 106668 KB Output is correct
61 Correct 1195 ms 103284 KB Output is correct
62 Correct 1180 ms 101804 KB Output is correct
63 Correct 1182 ms 102768 KB Output is correct
64 Correct 2720 ms 119420 KB Output is correct
65 Correct 1169 ms 100268 KB Output is correct
66 Correct 2758 ms 114100 KB Output is correct
67 Correct 2554 ms 156976 KB Output is correct
68 Correct 2516 ms 121016 KB Output is correct
69 Correct 2487 ms 128548 KB Output is correct
70 Correct 2761 ms 121156 KB Output is correct
71 Correct 2717 ms 118596 KB Output is correct
72 Correct 2419 ms 158296 KB Output is correct
73 Correct 2467 ms 131052 KB Output is correct
74 Correct 2606 ms 122316 KB Output is correct
75 Correct 2768 ms 119596 KB Output is correct
76 Correct 1875 ms 118196 KB Output is correct
77 Correct 1851 ms 103932 KB Output is correct
78 Correct 2095 ms 118132 KB Output is correct
79 Correct 2113 ms 119392 KB Output is correct
80 Correct 2103 ms 117644 KB Output is correct
81 Correct 559 ms 41684 KB Output is correct
82 Correct 548 ms 41504 KB Output is correct
83 Correct 572 ms 38848 KB Output is correct
84 Correct 493 ms 36824 KB Output is correct
85 Correct 495 ms 38484 KB Output is correct
86 Correct 483 ms 35148 KB Output is correct
87 Correct 509 ms 36900 KB Output is correct
88 Correct 513 ms 38136 KB Output is correct
89 Correct 483 ms 35032 KB Output is correct
90 Correct 250 ms 38436 KB Output is correct
91 Correct 541 ms 42452 KB Output is correct
92 Correct 538 ms 38984 KB Output is correct
93 Correct 555 ms 37460 KB Output is correct
94 Correct 516 ms 35556 KB Output is correct
95 Correct 482 ms 34392 KB Output is correct
96 Correct 262 ms 31192 KB Output is correct
97 Correct 3295 ms 167392 KB Output is correct
98 Correct 1242 ms 92708 KB Output is correct
99 Correct 3728 ms 126348 KB Output is correct
100 Correct 3238 ms 163848 KB Output is correct
101 Correct 3504 ms 143156 KB Output is correct
102 Correct 3999 ms 130700 KB Output is correct
103 Correct 3037 ms 118608 KB Output is correct
104 Correct 2960 ms 120992 KB Output is correct
105 Correct 2591 ms 119716 KB Output is correct
106 Correct 2595 ms 115824 KB Output is correct
107 Correct 2889 ms 132524 KB Output is correct
108 Correct 2893 ms 149368 KB Output is correct
109 Correct 2873 ms 126668 KB Output is correct
110 Correct 3049 ms 136020 KB Output is correct
111 Correct 3127 ms 149916 KB Output is correct
112 Correct 3011 ms 123780 KB Output is correct
113 Correct 1287 ms 153960 KB Output is correct
114 Correct 3241 ms 167656 KB Output is correct
115 Correct 3292 ms 150800 KB Output is correct
116 Correct 3291 ms 134308 KB Output is correct
117 Correct 3256 ms 123428 KB Output is correct
118 Correct 3083 ms 118100 KB Output is correct
119 Correct 1397 ms 96520 KB Output is correct
120 Correct 1603 ms 110668 KB Output is correct
121 Correct 1787 ms 117292 KB Output is correct
122 Correct 1769 ms 112596 KB Output is correct
123 Correct 1981 ms 118316 KB Output is correct
124 Correct 2202 ms 115792 KB Output is correct
125 Correct 1981 ms 118956 KB Output is correct
126 Correct 2272 ms 119724 KB Output is correct