Submission #77341

# Submission time Handle Problem Language Result Execution time Memory
77341 2018-09-25T20:32:07 Z 298iq New Home (APIO18_new_home) C++14
47 / 100
5000 ms 97896 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
 
#include <bits/stdc++.h>
 
using namespace std;
 
const int INF = 1e9 + 7;
const int MAXV = 1e8 + 7;
const int D = 2e6; 

namespace DD {
 
mt19937 rnd(228);
    
struct Node {
    pair <int, int> p;
    int y, mx;
    int l, r;
};

int ptr = 0;
Node d[D];

int init(pair <int, int> p) {
    d[ptr].p = p;
    d[ptr].y = rnd();
    d[ptr].mx = p.second;
    d[ptr].l = d[ptr].r = -1;
    ++ptr;
    return ptr - 1;
}
 
int mx(int t) {
    if (t == -1) return -1;
    else return d[t].mx;
}
   
void relax(int t) {
    d[t].mx = max(max(mx(d[t].l), mx(d[t].r)), d[t].p.second);
}
 
int merge(int l, int r) {
    if (l == -1) return r;
    if (r == -1) return l;
    if (d[l].y < d[r].y) {
        d[l].r = merge(d[l].r, r);
        relax(l);
        return l;   
    }
    else {
        d[r].l = merge(l, d[r].l);
        relax(r);
        return r;
    }
}
 
pair <int, int> split(int t, pair <int, int> key) {
    if (t == -1) return {-1, -1};
    if (d[t].p <= key) {
        auto tmp = split(d[t].r, key);
        d[t].r = tmp.first;
        relax(t);
        return {t, tmp.second};
    }
    else {
        auto tmp = split(d[t].l, key);
        d[t].l = tmp.second;
        relax(t);
        return {tmp.first, t};
    }
}
 
int insert(int t, pair <int, int> p) {
    auto tmp = split(t, p);
    return merge(merge(tmp.first, init(p)), tmp.second);
}
 
int erase(int t, pair <int, int> p) {
    auto tmp1 = split(t, p);
    auto tmp2 = split(tmp1.first, {p.first, p.second - 1});
    tmp2.second = merge(d[tmp2.second].l, d[tmp2.second].r);
    return merge(merge(tmp2.first, tmp2.second), tmp1.second);
}
};
 
const int MAXN = 3e5 + 7;
 
int n, k, q;
int p[MAXN], c[MAXN], l[MAXN], r[MAXN];
int p2[MAXN], t2[MAXN];
void read() {
    cin >> n >> k >> q;
    for (int i = 0; i < n; ++i) {
        cin >> p[i] >> c[i] >> l[i] >> r[i];
        --c[i];
    }
    for (int i = 0; i < q; ++i) {
        cin >> p2[i] >> t2[i];
    }
}
 
struct Event {
    bool type;
    int t, i;
    Event(bool type_, int t_, int i_) {
        type = type_; t = t_; i = i_;
    }
    Event(){}
};
 
bool comp(Event a, Event b) {
    return a.t < b.t;
}
 
int ans[MAXN];
int qper[MAXN];
 
bool comp1(int i, int j) {
    return t2[i] < t2[j];
}
 
using namespace DD;
 
int dd = -1;
multiset <int> ps[MAXN];
 
void add(int c, int p, int num) {
    auto r = ps[c].lower_bound(p);
    auto l = r; --l;
    dd = erase(dd, {*l, *r});
    dd = insert(dd, {*l, p});
    dd = insert(dd, {p, *r});
    ps[c].insert(p);
}
 
void del(int c, int p, int num) {
    auto it = ps[c].lower_bound(p);
    auto l = it; --l;
    auto r = it; ++r;
    dd = erase(dd, {*l, p});
    dd = erase(dd, {p, *r});
    dd = insert(dd, {*l, *r});
    ps[c].erase(it);
}
 
void upd(Event e) {
    if (e.type) {
        add(c[e.i], p[e.i], e.i);
    }
    else {
        del(c[e.i], p[e.i], e.i);
    }
}
 
bool check(int l, int r) {
    l = max(l, 1);
    r = min(r, MAXV);
    --l; ++r;
    auto tmp = split(dd, {l, INF});
    int res = mx(tmp.first);
    dd = merge(tmp.first, tmp.second);
    return (res < r);
}
 
int get(int p) {
    if (!check(1, MAXV)) return -1;
    int l = -1;
    int r = MAXV;
    while (l < r - 1) {
        int m = (l + r) / 2;
        if (!check(p - m, p + m)) l = m;
        else r = m;
    }
    return r;
}
 
void solve() {
    vector <Event> v;
    for (int i = 0; i < n; ++i) {
        v.push_back(Event(1, l[i], i));
        v.push_back(Event(0, r[i] + 1, i));
    }
    sort(v.begin(), v.end(), comp);
 
    for (int i = 0; i < q; ++i) qper[i] = i;
    sort(qper, qper + q, comp1);
 
    for (int i = 0; i < k; ++i) {
        ps[i].insert(-INF);
        ps[i].insert(INF);
        dd = insert(dd, {-INF, INF});
    }

    int u = 0;
    for (int i = 0; i < q; ++i) {
        while (u < (int)v.size() && v[u].t <= t2[qper[i]]) {
            upd(v[u]);
            ++u;
        }   
        ans[qper[i]] = get(p2[qper[i]]);
    }
}
 
void print() {
    for (int i = 0; i < q; ++i) {
        cout << ans[i] << '\n';
    }
}
 
signed main() {
    //freopen("input.txt", "r", stdin);
 
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    read();
    solve();
    print();
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 53 ms 61432 KB Output is correct
2 Correct 53 ms 61564 KB Output is correct
3 Correct 53 ms 61564 KB Output is correct
4 Correct 52 ms 61564 KB Output is correct
5 Correct 55 ms 61564 KB Output is correct
6 Correct 56 ms 61584 KB Output is correct
7 Correct 54 ms 61688 KB Output is correct
8 Correct 56 ms 61688 KB Output is correct
9 Correct 53 ms 61688 KB Output is correct
10 Correct 57 ms 61688 KB Output is correct
11 Correct 53 ms 61688 KB Output is correct
12 Correct 54 ms 61688 KB Output is correct
13 Correct 52 ms 61688 KB Output is correct
14 Correct 56 ms 61772 KB Output is correct
15 Correct 48 ms 61792 KB Output is correct
16 Correct 57 ms 61792 KB Output is correct
17 Correct 56 ms 61792 KB Output is correct
18 Correct 57 ms 61792 KB Output is correct
19 Correct 58 ms 61792 KB Output is correct
20 Correct 54 ms 61792 KB Output is correct
21 Correct 56 ms 61792 KB Output is correct
22 Correct 55 ms 61792 KB Output is correct
23 Correct 57 ms 61792 KB Output is correct
24 Correct 55 ms 61792 KB Output is correct
25 Correct 56 ms 61792 KB Output is correct
26 Correct 54 ms 61792 KB Output is correct
27 Correct 57 ms 61792 KB Output is correct
28 Correct 55 ms 61792 KB Output is correct
29 Correct 56 ms 61792 KB Output is correct
30 Correct 54 ms 61792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 61432 KB Output is correct
2 Correct 53 ms 61564 KB Output is correct
3 Correct 53 ms 61564 KB Output is correct
4 Correct 52 ms 61564 KB Output is correct
5 Correct 55 ms 61564 KB Output is correct
6 Correct 56 ms 61584 KB Output is correct
7 Correct 54 ms 61688 KB Output is correct
8 Correct 56 ms 61688 KB Output is correct
9 Correct 53 ms 61688 KB Output is correct
10 Correct 57 ms 61688 KB Output is correct
11 Correct 53 ms 61688 KB Output is correct
12 Correct 54 ms 61688 KB Output is correct
13 Correct 52 ms 61688 KB Output is correct
14 Correct 56 ms 61772 KB Output is correct
15 Correct 48 ms 61792 KB Output is correct
16 Correct 57 ms 61792 KB Output is correct
17 Correct 56 ms 61792 KB Output is correct
18 Correct 57 ms 61792 KB Output is correct
19 Correct 58 ms 61792 KB Output is correct
20 Correct 54 ms 61792 KB Output is correct
21 Correct 56 ms 61792 KB Output is correct
22 Correct 55 ms 61792 KB Output is correct
23 Correct 57 ms 61792 KB Output is correct
24 Correct 55 ms 61792 KB Output is correct
25 Correct 56 ms 61792 KB Output is correct
26 Correct 54 ms 61792 KB Output is correct
27 Correct 57 ms 61792 KB Output is correct
28 Correct 55 ms 61792 KB Output is correct
29 Correct 56 ms 61792 KB Output is correct
30 Correct 54 ms 61792 KB Output is correct
31 Correct 1240 ms 67884 KB Output is correct
32 Correct 722 ms 67884 KB Output is correct
33 Correct 1064 ms 67884 KB Output is correct
34 Correct 1094 ms 67884 KB Output is correct
35 Correct 1300 ms 67964 KB Output is correct
36 Correct 1277 ms 67964 KB Output is correct
37 Correct 826 ms 67964 KB Output is correct
38 Correct 806 ms 67964 KB Output is correct
39 Correct 589 ms 67964 KB Output is correct
40 Correct 586 ms 67964 KB Output is correct
41 Correct 633 ms 67964 KB Output is correct
42 Correct 515 ms 67964 KB Output is correct
43 Correct 463 ms 67964 KB Output is correct
44 Correct 604 ms 67964 KB Output is correct
45 Correct 520 ms 67964 KB Output is correct
46 Correct 378 ms 67964 KB Output is correct
47 Correct 302 ms 67964 KB Output is correct
48 Correct 306 ms 67964 KB Output is correct
49 Correct 383 ms 67964 KB Output is correct
50 Correct 448 ms 67964 KB Output is correct
51 Correct 375 ms 67964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4519 ms 97896 KB Output is correct
2 Execution timed out 5011 ms 97896 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5009 ms 97896 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 53 ms 61432 KB Output is correct
2 Correct 53 ms 61564 KB Output is correct
3 Correct 53 ms 61564 KB Output is correct
4 Correct 52 ms 61564 KB Output is correct
5 Correct 55 ms 61564 KB Output is correct
6 Correct 56 ms 61584 KB Output is correct
7 Correct 54 ms 61688 KB Output is correct
8 Correct 56 ms 61688 KB Output is correct
9 Correct 53 ms 61688 KB Output is correct
10 Correct 57 ms 61688 KB Output is correct
11 Correct 53 ms 61688 KB Output is correct
12 Correct 54 ms 61688 KB Output is correct
13 Correct 52 ms 61688 KB Output is correct
14 Correct 56 ms 61772 KB Output is correct
15 Correct 48 ms 61792 KB Output is correct
16 Correct 57 ms 61792 KB Output is correct
17 Correct 56 ms 61792 KB Output is correct
18 Correct 57 ms 61792 KB Output is correct
19 Correct 58 ms 61792 KB Output is correct
20 Correct 54 ms 61792 KB Output is correct
21 Correct 56 ms 61792 KB Output is correct
22 Correct 55 ms 61792 KB Output is correct
23 Correct 57 ms 61792 KB Output is correct
24 Correct 55 ms 61792 KB Output is correct
25 Correct 56 ms 61792 KB Output is correct
26 Correct 54 ms 61792 KB Output is correct
27 Correct 57 ms 61792 KB Output is correct
28 Correct 55 ms 61792 KB Output is correct
29 Correct 56 ms 61792 KB Output is correct
30 Correct 54 ms 61792 KB Output is correct
31 Correct 1240 ms 67884 KB Output is correct
32 Correct 722 ms 67884 KB Output is correct
33 Correct 1064 ms 67884 KB Output is correct
34 Correct 1094 ms 67884 KB Output is correct
35 Correct 1300 ms 67964 KB Output is correct
36 Correct 1277 ms 67964 KB Output is correct
37 Correct 826 ms 67964 KB Output is correct
38 Correct 806 ms 67964 KB Output is correct
39 Correct 589 ms 67964 KB Output is correct
40 Correct 586 ms 67964 KB Output is correct
41 Correct 633 ms 67964 KB Output is correct
42 Correct 515 ms 67964 KB Output is correct
43 Correct 463 ms 67964 KB Output is correct
44 Correct 604 ms 67964 KB Output is correct
45 Correct 520 ms 67964 KB Output is correct
46 Correct 378 ms 67964 KB Output is correct
47 Correct 302 ms 67964 KB Output is correct
48 Correct 306 ms 67964 KB Output is correct
49 Correct 383 ms 67964 KB Output is correct
50 Correct 448 ms 67964 KB Output is correct
51 Correct 375 ms 67964 KB Output is correct
52 Correct 608 ms 97896 KB Output is correct
53 Correct 609 ms 97896 KB Output is correct
54 Correct 612 ms 97896 KB Output is correct
55 Correct 901 ms 97896 KB Output is correct
56 Correct 971 ms 97896 KB Output is correct
57 Correct 735 ms 97896 KB Output is correct
58 Correct 805 ms 97896 KB Output is correct
59 Correct 692 ms 97896 KB Output is correct
60 Correct 673 ms 97896 KB Output is correct
61 Correct 699 ms 97896 KB Output is correct
62 Correct 844 ms 97896 KB Output is correct
63 Correct 878 ms 97896 KB Output is correct
64 Correct 855 ms 97896 KB Output is correct
65 Correct 839 ms 97896 KB Output is correct
66 Correct 669 ms 97896 KB Output is correct
67 Correct 732 ms 97896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 61432 KB Output is correct
2 Correct 53 ms 61564 KB Output is correct
3 Correct 53 ms 61564 KB Output is correct
4 Correct 52 ms 61564 KB Output is correct
5 Correct 55 ms 61564 KB Output is correct
6 Correct 56 ms 61584 KB Output is correct
7 Correct 54 ms 61688 KB Output is correct
8 Correct 56 ms 61688 KB Output is correct
9 Correct 53 ms 61688 KB Output is correct
10 Correct 57 ms 61688 KB Output is correct
11 Correct 53 ms 61688 KB Output is correct
12 Correct 54 ms 61688 KB Output is correct
13 Correct 52 ms 61688 KB Output is correct
14 Correct 56 ms 61772 KB Output is correct
15 Correct 48 ms 61792 KB Output is correct
16 Correct 57 ms 61792 KB Output is correct
17 Correct 56 ms 61792 KB Output is correct
18 Correct 57 ms 61792 KB Output is correct
19 Correct 58 ms 61792 KB Output is correct
20 Correct 54 ms 61792 KB Output is correct
21 Correct 56 ms 61792 KB Output is correct
22 Correct 55 ms 61792 KB Output is correct
23 Correct 57 ms 61792 KB Output is correct
24 Correct 55 ms 61792 KB Output is correct
25 Correct 56 ms 61792 KB Output is correct
26 Correct 54 ms 61792 KB Output is correct
27 Correct 57 ms 61792 KB Output is correct
28 Correct 55 ms 61792 KB Output is correct
29 Correct 56 ms 61792 KB Output is correct
30 Correct 54 ms 61792 KB Output is correct
31 Correct 1240 ms 67884 KB Output is correct
32 Correct 722 ms 67884 KB Output is correct
33 Correct 1064 ms 67884 KB Output is correct
34 Correct 1094 ms 67884 KB Output is correct
35 Correct 1300 ms 67964 KB Output is correct
36 Correct 1277 ms 67964 KB Output is correct
37 Correct 826 ms 67964 KB Output is correct
38 Correct 806 ms 67964 KB Output is correct
39 Correct 589 ms 67964 KB Output is correct
40 Correct 586 ms 67964 KB Output is correct
41 Correct 633 ms 67964 KB Output is correct
42 Correct 515 ms 67964 KB Output is correct
43 Correct 463 ms 67964 KB Output is correct
44 Correct 604 ms 67964 KB Output is correct
45 Correct 520 ms 67964 KB Output is correct
46 Correct 378 ms 67964 KB Output is correct
47 Correct 302 ms 67964 KB Output is correct
48 Correct 306 ms 67964 KB Output is correct
49 Correct 383 ms 67964 KB Output is correct
50 Correct 448 ms 67964 KB Output is correct
51 Correct 375 ms 67964 KB Output is correct
52 Correct 4519 ms 97896 KB Output is correct
53 Execution timed out 5011 ms 97896 KB Time limit exceeded
54 Halted 0 ms 0 KB -