Submission #48960

# Submission time Handle Problem Language Result Execution time Memory
48960 2018-05-20T15:11:11 Z erdemkiraz New Home (APIO18_new_home) C++11
57 / 100
5000 ms 181084 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair < int, int > ii;
 
const int N = 3e5 + 5;
const int OUT = 0, QUE = 1;
const int addPositive = 0, addNegative = 1;
 
int n, q, k, total;
int ans[N];
map < int, int > s[N];
 
int z, g;
 
int tree[2][N << 1];
 
int curX;
vector < pair < int, pair < int*, int > > > changes;
 
void update(int t, int l, int r, int x) {
    for(l += N, r += N; l <= r; l = (l + 1) >> 1, r = (r - 1) >> 1) {
        if((l & 1) and x > tree[t][l]) {
            changes.push_back({curX, {&tree[t][l], tree[t][l]}});
            tree[t][l] = x;
        }
        if((~r & 1) and x > tree[t][r]) {
            changes.push_back({curX, {&tree[t][r], tree[t][r]}});
            tree[t][r] = x;
        }
    }
}
 
int get(int t, int x) {
    int res = -1e9;
    x += N;
    while(x >= 1) {
        res = max(res, tree[t][x]);
        x >>= 1;
    }
    return res;
}
 
inline int read() {
    char c = getchar();
    int x = 0;
    while(c < '0' or c > '9')
        c = getchar();
    while('0' <= c and c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}
 
vector < pair < ii, int > > M[N * 4];
int isany[N * 4];
 
void create(int x, int type, int l, int r, int x1, int x2, int v, int y) {
    if(x2 < l or r < x1)
        return;
    if(type == QUE)
        isany[x] = 1;
    if(x1 <= l and r <= x2) {
        // printf("type = %d l = %d r = %d\n", type, l, r);
        M[x].push_back({{type, v}, y});
        return;
    }
    int m = (l + r) >> 1;
    create(x + x, type, l, m, x1, x2, v, y);
    create(x + x + 1, type, m + 1, r, x1, x2, v, y);
}
 
vector < int > ques;
int rid(int x) {
    return lower_bound(ques.begin(), ques.end(), x) - ques.begin() + 1;
}
int lid(int x) {
    return upper_bound(ques.begin(), ques.end(), x) - ques.begin();
}
 
vector < int > ins;
int ri(int x) {
    return lower_bound(ins.begin(), ins.end(), x) - ins.begin() + 1;
}
int li(int x) {
    return upper_bound(ins.begin(), ins.end(), x) - ins.begin();
}
 
void solve(int x, int l, int r) {
    if(!isany[x])
        return;
 
    int delTotal = 0;
 
    curX = x;
 
    bool HARD_FAIL = 0;
 
    for(auto tmp : M[x]) {
        int type = tmp.first.first, x = tmp.first.second, t = tmp.second;
        if(type == OUT) {
            if(--s[t][x])
                continue;
            s[t].erase(x);
            // printf("deleting location = %d type = %d\n", x, t);
            auto it = s[t].begin();
            int nxt = (it=s[t].upper_bound(x)) == s[t].end() ? -1 : it->first;
                int bef = (it=s[t].lower_bound(x)) == s[t].begin() ? -1 : (--it)->first;
            if(bef == -1 and nxt == -1) {
                HARD_FAIL = 1;
            }
            else if(bef == -1) {
                update(addNegative, 1, lid((x + nxt) / 2), nxt);
            }
            else if(nxt == -1) {
                update(addPositive, rid((bef + x) / 2 + 1), z, -bef);
            }
            else {
                update(addPositive, rid((bef + x) / 2 + 1), lid((bef + nxt) / 2), -bef);
                update(addNegative, rid((bef + nxt) / 2 + 1), lid((x + nxt) / 2), nxt);
            }
        }
    }
 
    if(HARD_FAIL) {
        for(auto tmp : M[x]) {
            int type = tmp.first.first, x = tmp.first.second, t = tmp.second;
            if(type == OUT) {
                // printf("reverting x = %d t = %d\n", x, t);
                s[t][x]++;
            }
        }
 
        while(!changes.empty() and changes.back().first == x) {
            *changes.back().second.first = changes.back().second.second;
            changes.pop_back();
        }
        return;
    }
 
    if(l == r) {
        for(auto tmp : M[x]) {
            int type = tmp.first.first, x = tmp.first.second, t = tmp.second;
            if(type == QUE) {
                int a = get(0, rid(x));
                int b = get(1, rid(x));
                // printf("que x = %d i = %d a = %d b = %d\n", x, t, a, b);
                ans[t] = max(a + x, b - x);
            }
        }
    }
    else {
        int m = (l + r) >> 1;
        solve(x + x, l, m);
        solve(x + x + 1, m + 1, r);
    }
 
    for(auto tmp : M[x]) {
        int type = tmp.first.first, x = tmp.first.second, t = tmp.second;
        if(type == OUT) {
            // printf("reverting x = %d t = %d\n", x, t);
            s[t][x]++;
        }
    }
 
    while(!changes.empty() and changes.back().first == x) {
        *changes.back().second.first = changes.back().second.second;
        changes.pop_back();
    }
 
}
 
int x1[N], t1[N], a1[N], b1[N], x2[N], y2[N];
 
int main() {
 
    n = read();
    k = read();
    q = read();
 
    for(int i = 1; i <= n; i++) {
        int x, t, a, b;
        x = read();
        t = read();
        a = read();
        b = read();
        x1[i]=x,t1[i]=t,a1[i]=a,b1[i]=b;
    }
 
    for(int i = 1; i <= q; i++) {
        int x, y;
        x = read();
        y = read();
        x2[i] = x;
        y2[i] = y;
        ins.push_back(y);
        ques.push_back(x);
    }
 
    sort(ques.begin(), ques.end());
    ques.resize(unique(ques.begin(), ques.end()) - ques.begin());
    z = ques.size();
 
    ins.push_back(0);
    ins.push_back(1e8 + 1);
 
    sort(ins.begin(), ins.end());
    ins.resize(unique(ins.begin(), ins.end()) - ins.begin());
    g = ins.size();
 
    for(int i = 1; i <= n; i++) {
        int x=x1[i], t=t1[i], a=a1[i], b=b1[i];
        s[t][x]++;
        a = li(a - 1);
        b = ri(b + 1);
        if(a != 1) {
            create(1, OUT, 1, g, 1, a, x, t);
        }
        if(b != g) {
            create(1, OUT, 1, g, b, g, x, t);
        }
    }
 
    for(int i = 1; i <= q; i++) {
        int x=x2[i], y=y2[i];
        ans[i] = -1;
        create(1, QUE, 1, g, ri(y), ri(y), x, i);
    }
 
    memset(tree, -63, sizeof(tree));
 
    for(int i = 1; i <= k; i++) {
        vector < int > v;
        for(auto x : s[i])
            v.push_back(x.first);
        if(v.empty())
            break;
        total++;
        update(addNegative, 1, rid(v[0]), v[0]);
        for(int it = 0; it + 1 < v.size(); it++) {
            update(addPositive, rid(v[it]), lid((v[it] + v[it + 1]) / 2), -v[it]);
            update(addNegative, rid((v[it] + v[it + 1]) / 2 + 1), lid(v[it + 1]), v[it + 1]);
        }
        update(addPositive, rid(v.back()), z, -v.back());
    }
 
    changes.clear();
 
    if(total == k)
        solve(1, 1, g);
 
    for(int i = 1; i <= q; i++)
        printf("%d\n", ans[i]);
 
    return 0;
 
}

Compilation message

new_home.cpp: In function 'void solve(int, int, int)':
new_home.cpp:96:9: warning: unused variable 'delTotal' [-Wunused-variable]
     int delTotal = 0;
         ^~~~~~~~
new_home.cpp: In function 'int main()':
new_home.cpp:243:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int it = 0; it + 1 < v.size(); it++) {
                         ~~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 38 ms 47324 KB Output is correct
2 Correct 38 ms 47324 KB Output is correct
3 Correct 40 ms 47352 KB Output is correct
4 Correct 42 ms 47480 KB Output is correct
5 Correct 44 ms 47480 KB Output is correct
6 Correct 45 ms 47664 KB Output is correct
7 Correct 47 ms 47696 KB Output is correct
8 Correct 41 ms 47696 KB Output is correct
9 Correct 40 ms 47740 KB Output is correct
10 Correct 54 ms 47888 KB Output is correct
11 Correct 47 ms 47888 KB Output is correct
12 Correct 45 ms 47888 KB Output is correct
13 Correct 40 ms 47888 KB Output is correct
14 Correct 45 ms 47888 KB Output is correct
15 Correct 44 ms 47888 KB Output is correct
16 Correct 47 ms 47888 KB Output is correct
17 Correct 40 ms 47888 KB Output is correct
18 Correct 44 ms 47888 KB Output is correct
19 Correct 43 ms 47888 KB Output is correct
20 Correct 42 ms 47888 KB Output is correct
21 Correct 48 ms 47888 KB Output is correct
22 Correct 44 ms 47888 KB Output is correct
23 Correct 44 ms 47888 KB Output is correct
24 Correct 45 ms 47888 KB Output is correct
25 Correct 44 ms 47888 KB Output is correct
26 Correct 45 ms 47888 KB Output is correct
27 Correct 45 ms 47888 KB Output is correct
28 Correct 46 ms 47888 KB Output is correct
29 Correct 46 ms 47888 KB Output is correct
30 Correct 40 ms 47888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 47324 KB Output is correct
2 Correct 38 ms 47324 KB Output is correct
3 Correct 40 ms 47352 KB Output is correct
4 Correct 42 ms 47480 KB Output is correct
5 Correct 44 ms 47480 KB Output is correct
6 Correct 45 ms 47664 KB Output is correct
7 Correct 47 ms 47696 KB Output is correct
8 Correct 41 ms 47696 KB Output is correct
9 Correct 40 ms 47740 KB Output is correct
10 Correct 54 ms 47888 KB Output is correct
11 Correct 47 ms 47888 KB Output is correct
12 Correct 45 ms 47888 KB Output is correct
13 Correct 40 ms 47888 KB Output is correct
14 Correct 45 ms 47888 KB Output is correct
15 Correct 44 ms 47888 KB Output is correct
16 Correct 47 ms 47888 KB Output is correct
17 Correct 40 ms 47888 KB Output is correct
18 Correct 44 ms 47888 KB Output is correct
19 Correct 43 ms 47888 KB Output is correct
20 Correct 42 ms 47888 KB Output is correct
21 Correct 48 ms 47888 KB Output is correct
22 Correct 44 ms 47888 KB Output is correct
23 Correct 44 ms 47888 KB Output is correct
24 Correct 45 ms 47888 KB Output is correct
25 Correct 44 ms 47888 KB Output is correct
26 Correct 45 ms 47888 KB Output is correct
27 Correct 45 ms 47888 KB Output is correct
28 Correct 46 ms 47888 KB Output is correct
29 Correct 46 ms 47888 KB Output is correct
30 Correct 40 ms 47888 KB Output is correct
31 Correct 1890 ms 80616 KB Output is correct
32 Correct 109 ms 80616 KB Output is correct
33 Correct 2541 ms 81476 KB Output is correct
34 Correct 2222 ms 81476 KB Output is correct
35 Correct 2301 ms 81476 KB Output is correct
36 Correct 2303 ms 81476 KB Output is correct
37 Correct 1753 ms 81776 KB Output is correct
38 Correct 1778 ms 81776 KB Output is correct
39 Correct 1287 ms 81776 KB Output is correct
40 Correct 1562 ms 81776 KB Output is correct
41 Correct 1624 ms 81776 KB Output is correct
42 Correct 979 ms 81776 KB Output is correct
43 Correct 78 ms 81776 KB Output is correct
44 Correct 1371 ms 81776 KB Output is correct
45 Correct 1572 ms 81776 KB Output is correct
46 Correct 1747 ms 81776 KB Output is correct
47 Correct 771 ms 81776 KB Output is correct
48 Correct 870 ms 81776 KB Output is correct
49 Correct 926 ms 81776 KB Output is correct
50 Correct 840 ms 82340 KB Output is correct
51 Correct 1018 ms 82340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1433 ms 136448 KB Output is correct
2 Correct 1219 ms 136448 KB Output is correct
3 Correct 1265 ms 136448 KB Output is correct
4 Correct 1527 ms 136532 KB Output is correct
5 Correct 1351 ms 136532 KB Output is correct
6 Correct 1475 ms 136532 KB Output is correct
7 Correct 1273 ms 136532 KB Output is correct
8 Correct 1518 ms 136568 KB Output is correct
9 Correct 1426 ms 136584 KB Output is correct
10 Correct 1313 ms 136624 KB Output is correct
11 Correct 804 ms 136624 KB Output is correct
12 Correct 973 ms 136624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5029 ms 181084 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 47324 KB Output is correct
2 Correct 38 ms 47324 KB Output is correct
3 Correct 40 ms 47352 KB Output is correct
4 Correct 42 ms 47480 KB Output is correct
5 Correct 44 ms 47480 KB Output is correct
6 Correct 45 ms 47664 KB Output is correct
7 Correct 47 ms 47696 KB Output is correct
8 Correct 41 ms 47696 KB Output is correct
9 Correct 40 ms 47740 KB Output is correct
10 Correct 54 ms 47888 KB Output is correct
11 Correct 47 ms 47888 KB Output is correct
12 Correct 45 ms 47888 KB Output is correct
13 Correct 40 ms 47888 KB Output is correct
14 Correct 45 ms 47888 KB Output is correct
15 Correct 44 ms 47888 KB Output is correct
16 Correct 47 ms 47888 KB Output is correct
17 Correct 40 ms 47888 KB Output is correct
18 Correct 44 ms 47888 KB Output is correct
19 Correct 43 ms 47888 KB Output is correct
20 Correct 42 ms 47888 KB Output is correct
21 Correct 48 ms 47888 KB Output is correct
22 Correct 44 ms 47888 KB Output is correct
23 Correct 44 ms 47888 KB Output is correct
24 Correct 45 ms 47888 KB Output is correct
25 Correct 44 ms 47888 KB Output is correct
26 Correct 45 ms 47888 KB Output is correct
27 Correct 45 ms 47888 KB Output is correct
28 Correct 46 ms 47888 KB Output is correct
29 Correct 46 ms 47888 KB Output is correct
30 Correct 40 ms 47888 KB Output is correct
31 Correct 1890 ms 80616 KB Output is correct
32 Correct 109 ms 80616 KB Output is correct
33 Correct 2541 ms 81476 KB Output is correct
34 Correct 2222 ms 81476 KB Output is correct
35 Correct 2301 ms 81476 KB Output is correct
36 Correct 2303 ms 81476 KB Output is correct
37 Correct 1753 ms 81776 KB Output is correct
38 Correct 1778 ms 81776 KB Output is correct
39 Correct 1287 ms 81776 KB Output is correct
40 Correct 1562 ms 81776 KB Output is correct
41 Correct 1624 ms 81776 KB Output is correct
42 Correct 979 ms 81776 KB Output is correct
43 Correct 78 ms 81776 KB Output is correct
44 Correct 1371 ms 81776 KB Output is correct
45 Correct 1572 ms 81776 KB Output is correct
46 Correct 1747 ms 81776 KB Output is correct
47 Correct 771 ms 81776 KB Output is correct
48 Correct 870 ms 81776 KB Output is correct
49 Correct 926 ms 81776 KB Output is correct
50 Correct 840 ms 82340 KB Output is correct
51 Correct 1018 ms 82340 KB Output is correct
52 Correct 405 ms 181084 KB Output is correct
53 Correct 410 ms 181084 KB Output is correct
54 Correct 504 ms 181084 KB Output is correct
55 Correct 1063 ms 181084 KB Output is correct
56 Correct 891 ms 181084 KB Output is correct
57 Correct 1283 ms 181084 KB Output is correct
58 Correct 787 ms 181084 KB Output is correct
59 Correct 603 ms 181084 KB Output is correct
60 Correct 974 ms 181084 KB Output is correct
61 Correct 129 ms 181084 KB Output is correct
62 Correct 420 ms 181084 KB Output is correct
63 Correct 702 ms 181084 KB Output is correct
64 Correct 895 ms 181084 KB Output is correct
65 Correct 1198 ms 181084 KB Output is correct
66 Correct 1469 ms 181084 KB Output is correct
67 Correct 234 ms 181084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 47324 KB Output is correct
2 Correct 38 ms 47324 KB Output is correct
3 Correct 40 ms 47352 KB Output is correct
4 Correct 42 ms 47480 KB Output is correct
5 Correct 44 ms 47480 KB Output is correct
6 Correct 45 ms 47664 KB Output is correct
7 Correct 47 ms 47696 KB Output is correct
8 Correct 41 ms 47696 KB Output is correct
9 Correct 40 ms 47740 KB Output is correct
10 Correct 54 ms 47888 KB Output is correct
11 Correct 47 ms 47888 KB Output is correct
12 Correct 45 ms 47888 KB Output is correct
13 Correct 40 ms 47888 KB Output is correct
14 Correct 45 ms 47888 KB Output is correct
15 Correct 44 ms 47888 KB Output is correct
16 Correct 47 ms 47888 KB Output is correct
17 Correct 40 ms 47888 KB Output is correct
18 Correct 44 ms 47888 KB Output is correct
19 Correct 43 ms 47888 KB Output is correct
20 Correct 42 ms 47888 KB Output is correct
21 Correct 48 ms 47888 KB Output is correct
22 Correct 44 ms 47888 KB Output is correct
23 Correct 44 ms 47888 KB Output is correct
24 Correct 45 ms 47888 KB Output is correct
25 Correct 44 ms 47888 KB Output is correct
26 Correct 45 ms 47888 KB Output is correct
27 Correct 45 ms 47888 KB Output is correct
28 Correct 46 ms 47888 KB Output is correct
29 Correct 46 ms 47888 KB Output is correct
30 Correct 40 ms 47888 KB Output is correct
31 Correct 1890 ms 80616 KB Output is correct
32 Correct 109 ms 80616 KB Output is correct
33 Correct 2541 ms 81476 KB Output is correct
34 Correct 2222 ms 81476 KB Output is correct
35 Correct 2301 ms 81476 KB Output is correct
36 Correct 2303 ms 81476 KB Output is correct
37 Correct 1753 ms 81776 KB Output is correct
38 Correct 1778 ms 81776 KB Output is correct
39 Correct 1287 ms 81776 KB Output is correct
40 Correct 1562 ms 81776 KB Output is correct
41 Correct 1624 ms 81776 KB Output is correct
42 Correct 979 ms 81776 KB Output is correct
43 Correct 78 ms 81776 KB Output is correct
44 Correct 1371 ms 81776 KB Output is correct
45 Correct 1572 ms 81776 KB Output is correct
46 Correct 1747 ms 81776 KB Output is correct
47 Correct 771 ms 81776 KB Output is correct
48 Correct 870 ms 81776 KB Output is correct
49 Correct 926 ms 81776 KB Output is correct
50 Correct 840 ms 82340 KB Output is correct
51 Correct 1018 ms 82340 KB Output is correct
52 Correct 1433 ms 136448 KB Output is correct
53 Correct 1219 ms 136448 KB Output is correct
54 Correct 1265 ms 136448 KB Output is correct
55 Correct 1527 ms 136532 KB Output is correct
56 Correct 1351 ms 136532 KB Output is correct
57 Correct 1475 ms 136532 KB Output is correct
58 Correct 1273 ms 136532 KB Output is correct
59 Correct 1518 ms 136568 KB Output is correct
60 Correct 1426 ms 136584 KB Output is correct
61 Correct 1313 ms 136624 KB Output is correct
62 Correct 804 ms 136624 KB Output is correct
63 Correct 973 ms 136624 KB Output is correct
64 Execution timed out 5029 ms 181084 KB Time limit exceeded
65 Halted 0 ms 0 KB -