Submission #412622

# Submission time Handle Problem Language Result Execution time Memory
412622 2021-05-27T08:10:59 Z dolphingarlic New Home (APIO18_new_home) C++14
57 / 100
5000 ms 185400 KB
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#pragma GCC target("sse4,avx2,fma,avx")
typedef long long ll;
using namespace std;

const int INF = 1e8;

vector<int> c_loc, c_time;
unordered_map<int, vector<pair<int, int>>> queries, toggle, ins, del;
vector<tuple<int, int, int>> stores[300001];
int ans[300001], segtree[1200001];
multiset<int> rays[300001];

int query(int loc, int node = 1, int l = 0, int r = c_loc.size() - 1) {
    if (r <= loc) return segtree[node];
    if (l > loc) return -INF;
    int mid = (l + r) / 2;
    return max(query(loc, node * 2, l, mid),
               query(loc, node * 2 + 1, mid + 1, r));
}

void update(int loc, int val, int node = 1, int l = 0,
            int r = c_loc.size() - 1) {
    if (l == r)
        segtree[node] = val;
    else {
        int mid = (l + r) / 2;
        if (loc > mid)
            update(loc, val, node * 2 + 1, mid + 1, r);
        else
            update(loc, val, node * 2, l, mid);
        segtree[node] = max(segtree[node * 2], segtree[node * 2 + 1]);
    }
}

void ins_ray(int loc, int val) {
    if (loc == c_loc.size()) return;
    rays[loc].insert(val);
    update(loc, *rays[loc].rbegin());
}

void del_ray(int loc, int val) {
    if (loc == c_loc.size()) return;
    rays[loc].erase(rays[loc].find(val));
    update(loc, *rays[loc].rbegin());
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, k, q;
    cin >> n >> k >> q;
    for (int i = 1; i <= n; i++) {
        int loc, type, a, b;
        cin >> loc >> type >> a >> b;
        stores[type].push_back({a, loc, 0});
        stores[type].push_back({b + 1, loc, 1});
    }
    for (int i = 1; i <= q; i++) {
        int loc, time;
        cin >> loc >> time;
        c_loc.push_back(2 * loc);
        c_time.push_back(time);
        queries[time].push_back({i, loc});
        ans[i] = -1;
    }
    sort(c_loc.begin(), c_loc.end());
    c_loc.erase(unique(c_loc.begin(), c_loc.end()), c_loc.end());
    sort(c_time.begin(), c_time.end());
    c_time.erase(unique(c_time.begin(), c_time.end()), c_time.end());
    c_time.push_back(INF + 1);

    for (int _ : {0, 1}) {
        for (int i = 0; i < c_loc.size(); i++) {
            rays[i].clear();
            rays[i].insert(-INF);
        }

        for (int t = 1; t <= k; t++) {
            sort(stores[t].begin(), stores[t].end());
            map<int, int> active, ray_at;
            for (auto i : stores[t]) {
                int time, loc, close;
                tie(time, loc, close) = i;
                time = *lower_bound(c_time.begin(), c_time.end(), time);
                if (close) {
                    active[loc]--;
                    if (!active[loc]) {
                        active.erase(loc);
                        auto ub = active.upper_bound(loc);
                        del[time].push_back({ray_at[loc], loc});
                        ray_at.erase(loc);
                        if (ub != active.end()) {
                            del[time].push_back({ray_at[ub->first], ub->first});
                            if (ub != active.begin()) {
                                ins[time].push_back(
                                    {prev(ub)->first + ub->first, ub->first});
                                ray_at[ub->first] = prev(ub)->first + ub->first;
                            } else {
                                ins[time].push_back({0, ub->first});
                                ray_at[ub->first] = 0;
                            }
                        }
                    }
                } else {
                    if (!active.count(loc)) {
                        auto ub = active.upper_bound(loc);
                        if (ub != active.end()) {
                            del[time].push_back({ray_at[ub->first], ub->first});
                            ins[time].push_back({ub->first + loc, ub->first});
                            ray_at[ub->first] = ub->first + loc;
                        }
                        if (ub != active.begin()) {
                            ins[time].push_back({loc + prev(ub)->first, loc});
                            ray_at[loc] = loc + prev(ub)->first;
                        } else {
                            ins[time].push_back({0, loc});
                            ray_at[loc] = 0;
                        }
                    }
                    active[loc]++;
                }
                toggle[time].push_back({t, 1 - 2 * close});
            }
        }

        unordered_map<int, int> active;
        fill_n(segtree + 1, 4 * c_loc.size(), -INF);
        for (int t : c_time) {
            for (pair<int, int> i : ins[t])
                ins_ray(lower_bound(c_loc.begin(), c_loc.end(), i.first) -
                            c_loc.begin(),
                        i.second);
            for (pair<int, int> i : del[t])
                del_ray(lower_bound(c_loc.begin(), c_loc.end(), i.first) -
                            c_loc.begin(),
                        i.second);
            for (pair<int, int> i : toggle[t]) {
                active[i.first] += i.second;
                if (!active[i.first]) active.erase(i.first);
            }
            if (active.size() == k) {
                for (pair<int, int> i : queries[t]) {
                    int res = query(
                        lower_bound(c_loc.begin(), c_loc.end(), 2 * i.second) -
                        c_loc.begin());
                    ans[i.first] = max(ans[i.first], res - i.second);
                }
            }
        }

        for (int i = 1; i <= k; i++)
            for (auto& j : stores[i]) get<1>(j) = INF + 1 - get<1>(j);
        for (auto& i : queries)
            for (auto& j : i.second) j.second = INF + 1 - j.second;
        for (auto& i : c_loc) i = 2 * INF + 2 - i;
        reverse(c_loc.begin(), c_loc.end());
        toggle.clear();
        ins.clear();
        del.clear();
    }
    for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
    return 0;
}

Compilation message

new_home.cpp: In function 'void ins_ray(int, int)':
new_home.cpp:39:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     if (loc == c_loc.size()) return;
      |         ~~~~^~~~~~~~~~~~~~~
new_home.cpp: In function 'void del_ray(int, int)':
new_home.cpp:45:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     if (loc == c_loc.size()) return;
      |         ~~~~^~~~~~~~~~~~~~~
new_home.cpp: In function 'int main()':
new_home.cpp:75:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |         for (int i = 0; i < c_loc.size(); i++) {
      |                         ~~^~~~~~~~~~~~~~
new_home.cpp:143:31: warning: comparison of integer expressions of different signedness: 'std::unordered_map<int, int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  143 |             if (active.size() == k) {
      |                 ~~~~~~~~~~~~~~^~~~
new_home.cpp:74:14: warning: unused variable '_' [-Wunused-variable]
   74 |     for (int _ : {0, 1}) {
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21452 KB Output is correct
2 Correct 12 ms 21452 KB Output is correct
3 Correct 13 ms 21452 KB Output is correct
4 Correct 12 ms 21392 KB Output is correct
5 Correct 13 ms 21420 KB Output is correct
6 Correct 15 ms 21580 KB Output is correct
7 Correct 14 ms 21608 KB Output is correct
8 Correct 14 ms 21680 KB Output is correct
9 Correct 14 ms 21580 KB Output is correct
10 Correct 15 ms 21580 KB Output is correct
11 Correct 14 ms 21588 KB Output is correct
12 Correct 14 ms 21580 KB Output is correct
13 Correct 14 ms 21668 KB Output is correct
14 Correct 14 ms 21660 KB Output is correct
15 Correct 14 ms 21668 KB Output is correct
16 Correct 14 ms 21636 KB Output is correct
17 Correct 14 ms 21708 KB Output is correct
18 Correct 15 ms 21580 KB Output is correct
19 Correct 16 ms 21580 KB Output is correct
20 Correct 15 ms 21556 KB Output is correct
21 Correct 13 ms 21556 KB Output is correct
22 Correct 14 ms 21672 KB Output is correct
23 Correct 14 ms 21608 KB Output is correct
24 Correct 14 ms 21580 KB Output is correct
25 Correct 14 ms 21580 KB Output is correct
26 Correct 15 ms 21636 KB Output is correct
27 Correct 13 ms 21452 KB Output is correct
28 Correct 14 ms 21560 KB Output is correct
29 Correct 14 ms 21580 KB Output is correct
30 Correct 15 ms 21708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21452 KB Output is correct
2 Correct 12 ms 21452 KB Output is correct
3 Correct 13 ms 21452 KB Output is correct
4 Correct 12 ms 21392 KB Output is correct
5 Correct 13 ms 21420 KB Output is correct
6 Correct 15 ms 21580 KB Output is correct
7 Correct 14 ms 21608 KB Output is correct
8 Correct 14 ms 21680 KB Output is correct
9 Correct 14 ms 21580 KB Output is correct
10 Correct 15 ms 21580 KB Output is correct
11 Correct 14 ms 21588 KB Output is correct
12 Correct 14 ms 21580 KB Output is correct
13 Correct 14 ms 21668 KB Output is correct
14 Correct 14 ms 21660 KB Output is correct
15 Correct 14 ms 21668 KB Output is correct
16 Correct 14 ms 21636 KB Output is correct
17 Correct 14 ms 21708 KB Output is correct
18 Correct 15 ms 21580 KB Output is correct
19 Correct 16 ms 21580 KB Output is correct
20 Correct 15 ms 21556 KB Output is correct
21 Correct 13 ms 21556 KB Output is correct
22 Correct 14 ms 21672 KB Output is correct
23 Correct 14 ms 21608 KB Output is correct
24 Correct 14 ms 21580 KB Output is correct
25 Correct 14 ms 21580 KB Output is correct
26 Correct 15 ms 21636 KB Output is correct
27 Correct 13 ms 21452 KB Output is correct
28 Correct 14 ms 21560 KB Output is correct
29 Correct 14 ms 21580 KB Output is correct
30 Correct 15 ms 21708 KB Output is correct
31 Correct 1351 ms 52780 KB Output is correct
32 Correct 85 ms 25832 KB Output is correct
33 Correct 1287 ms 51168 KB Output is correct
34 Correct 1196 ms 51908 KB Output is correct
35 Correct 1346 ms 52052 KB Output is correct
36 Correct 1361 ms 52116 KB Output is correct
37 Correct 1075 ms 50880 KB Output is correct
38 Correct 1101 ms 50496 KB Output is correct
39 Correct 884 ms 50396 KB Output is correct
40 Correct 915 ms 50392 KB Output is correct
41 Correct 972 ms 49632 KB Output is correct
42 Correct 882 ms 50020 KB Output is correct
43 Correct 74 ms 26788 KB Output is correct
44 Correct 926 ms 50036 KB Output is correct
45 Correct 852 ms 49740 KB Output is correct
46 Correct 821 ms 49628 KB Output is correct
47 Correct 615 ms 48844 KB Output is correct
48 Correct 623 ms 48716 KB Output is correct
49 Correct 666 ms 49324 KB Output is correct
50 Correct 764 ms 49780 KB Output is correct
51 Correct 731 ms 49328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4414 ms 183780 KB Output is correct
2 Correct 3666 ms 181824 KB Output is correct
3 Correct 3058 ms 172444 KB Output is correct
4 Correct 4075 ms 177052 KB Output is correct
5 Correct 3445 ms 180240 KB Output is correct
6 Correct 3480 ms 181692 KB Output is correct
7 Correct 3009 ms 172592 KB Output is correct
8 Correct 4027 ms 176344 KB Output is correct
9 Correct 4423 ms 185400 KB Output is correct
10 Correct 4183 ms 183840 KB Output is correct
11 Correct 3415 ms 181436 KB Output is correct
12 Correct 3838 ms 183752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5063 ms 154496 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21452 KB Output is correct
2 Correct 12 ms 21452 KB Output is correct
3 Correct 13 ms 21452 KB Output is correct
4 Correct 12 ms 21392 KB Output is correct
5 Correct 13 ms 21420 KB Output is correct
6 Correct 15 ms 21580 KB Output is correct
7 Correct 14 ms 21608 KB Output is correct
8 Correct 14 ms 21680 KB Output is correct
9 Correct 14 ms 21580 KB Output is correct
10 Correct 15 ms 21580 KB Output is correct
11 Correct 14 ms 21588 KB Output is correct
12 Correct 14 ms 21580 KB Output is correct
13 Correct 14 ms 21668 KB Output is correct
14 Correct 14 ms 21660 KB Output is correct
15 Correct 14 ms 21668 KB Output is correct
16 Correct 14 ms 21636 KB Output is correct
17 Correct 14 ms 21708 KB Output is correct
18 Correct 15 ms 21580 KB Output is correct
19 Correct 16 ms 21580 KB Output is correct
20 Correct 15 ms 21556 KB Output is correct
21 Correct 13 ms 21556 KB Output is correct
22 Correct 14 ms 21672 KB Output is correct
23 Correct 14 ms 21608 KB Output is correct
24 Correct 14 ms 21580 KB Output is correct
25 Correct 14 ms 21580 KB Output is correct
26 Correct 15 ms 21636 KB Output is correct
27 Correct 13 ms 21452 KB Output is correct
28 Correct 14 ms 21560 KB Output is correct
29 Correct 14 ms 21580 KB Output is correct
30 Correct 15 ms 21708 KB Output is correct
31 Correct 1351 ms 52780 KB Output is correct
32 Correct 85 ms 25832 KB Output is correct
33 Correct 1287 ms 51168 KB Output is correct
34 Correct 1196 ms 51908 KB Output is correct
35 Correct 1346 ms 52052 KB Output is correct
36 Correct 1361 ms 52116 KB Output is correct
37 Correct 1075 ms 50880 KB Output is correct
38 Correct 1101 ms 50496 KB Output is correct
39 Correct 884 ms 50396 KB Output is correct
40 Correct 915 ms 50392 KB Output is correct
41 Correct 972 ms 49632 KB Output is correct
42 Correct 882 ms 50020 KB Output is correct
43 Correct 74 ms 26788 KB Output is correct
44 Correct 926 ms 50036 KB Output is correct
45 Correct 852 ms 49740 KB Output is correct
46 Correct 821 ms 49628 KB Output is correct
47 Correct 615 ms 48844 KB Output is correct
48 Correct 623 ms 48716 KB Output is correct
49 Correct 666 ms 49324 KB Output is correct
50 Correct 764 ms 49780 KB Output is correct
51 Correct 731 ms 49328 KB Output is correct
52 Correct 746 ms 51052 KB Output is correct
53 Correct 647 ms 49200 KB Output is correct
54 Correct 859 ms 51076 KB Output is correct
55 Correct 716 ms 51020 KB Output is correct
56 Correct 694 ms 51088 KB Output is correct
57 Correct 785 ms 49372 KB Output is correct
58 Correct 762 ms 50052 KB Output is correct
59 Correct 692 ms 50568 KB Output is correct
60 Correct 778 ms 49996 KB Output is correct
61 Correct 140 ms 31968 KB Output is correct
62 Correct 656 ms 51988 KB Output is correct
63 Correct 744 ms 50760 KB Output is correct
64 Correct 792 ms 50068 KB Output is correct
65 Correct 853 ms 50040 KB Output is correct
66 Correct 915 ms 50064 KB Output is correct
67 Correct 253 ms 29744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21452 KB Output is correct
2 Correct 12 ms 21452 KB Output is correct
3 Correct 13 ms 21452 KB Output is correct
4 Correct 12 ms 21392 KB Output is correct
5 Correct 13 ms 21420 KB Output is correct
6 Correct 15 ms 21580 KB Output is correct
7 Correct 14 ms 21608 KB Output is correct
8 Correct 14 ms 21680 KB Output is correct
9 Correct 14 ms 21580 KB Output is correct
10 Correct 15 ms 21580 KB Output is correct
11 Correct 14 ms 21588 KB Output is correct
12 Correct 14 ms 21580 KB Output is correct
13 Correct 14 ms 21668 KB Output is correct
14 Correct 14 ms 21660 KB Output is correct
15 Correct 14 ms 21668 KB Output is correct
16 Correct 14 ms 21636 KB Output is correct
17 Correct 14 ms 21708 KB Output is correct
18 Correct 15 ms 21580 KB Output is correct
19 Correct 16 ms 21580 KB Output is correct
20 Correct 15 ms 21556 KB Output is correct
21 Correct 13 ms 21556 KB Output is correct
22 Correct 14 ms 21672 KB Output is correct
23 Correct 14 ms 21608 KB Output is correct
24 Correct 14 ms 21580 KB Output is correct
25 Correct 14 ms 21580 KB Output is correct
26 Correct 15 ms 21636 KB Output is correct
27 Correct 13 ms 21452 KB Output is correct
28 Correct 14 ms 21560 KB Output is correct
29 Correct 14 ms 21580 KB Output is correct
30 Correct 15 ms 21708 KB Output is correct
31 Correct 1351 ms 52780 KB Output is correct
32 Correct 85 ms 25832 KB Output is correct
33 Correct 1287 ms 51168 KB Output is correct
34 Correct 1196 ms 51908 KB Output is correct
35 Correct 1346 ms 52052 KB Output is correct
36 Correct 1361 ms 52116 KB Output is correct
37 Correct 1075 ms 50880 KB Output is correct
38 Correct 1101 ms 50496 KB Output is correct
39 Correct 884 ms 50396 KB Output is correct
40 Correct 915 ms 50392 KB Output is correct
41 Correct 972 ms 49632 KB Output is correct
42 Correct 882 ms 50020 KB Output is correct
43 Correct 74 ms 26788 KB Output is correct
44 Correct 926 ms 50036 KB Output is correct
45 Correct 852 ms 49740 KB Output is correct
46 Correct 821 ms 49628 KB Output is correct
47 Correct 615 ms 48844 KB Output is correct
48 Correct 623 ms 48716 KB Output is correct
49 Correct 666 ms 49324 KB Output is correct
50 Correct 764 ms 49780 KB Output is correct
51 Correct 731 ms 49328 KB Output is correct
52 Correct 4414 ms 183780 KB Output is correct
53 Correct 3666 ms 181824 KB Output is correct
54 Correct 3058 ms 172444 KB Output is correct
55 Correct 4075 ms 177052 KB Output is correct
56 Correct 3445 ms 180240 KB Output is correct
57 Correct 3480 ms 181692 KB Output is correct
58 Correct 3009 ms 172592 KB Output is correct
59 Correct 4027 ms 176344 KB Output is correct
60 Correct 4423 ms 185400 KB Output is correct
61 Correct 4183 ms 183840 KB Output is correct
62 Correct 3415 ms 181436 KB Output is correct
63 Correct 3838 ms 183752 KB Output is correct
64 Execution timed out 5063 ms 154496 KB Time limit exceeded
65 Halted 0 ms 0 KB -