답안 #968839

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
968839 2024-04-24T07:17:36 Z socpite 새 집 (APIO18_new_home) C++14
57 / 100
5000 ms 338992 KB
#pragma GCC optimize("arch=skylake")
#include<bits/stdc++.h>
using namespace std;

const int maxn = 3e5+5;
const int INF = 1e9+5;

vector<pair<int, pair<int, int>>> ev;
vector<int> cp, cpy;
int n, q, k;

int ans[maxn];
int p[maxn], ty[maxn];
int L[maxn], Y[maxn];

int st[4*maxn][2];

struct ev_upd{
    int l, r, ty;
    ev_upd(int _l, int _r, int _ty): l(_l), r(_r), ty(_ty){};
};

map<int, vector<pair<int, int>>> leaf_q;  
set<pair<int, int>> pos[maxn];
int last_time[maxn][2];


vector<ev_upd> st_time[4*maxn];

int crrt;

pair<pair<int, int>, int> undo_stack[maxn*20];
int stksz = 0;

// 0 is min, 1 is max

void upd_st(const ev_upd &ele, int &cnt, int l = 0, int r = cp.size()-1, int id = 1){
    if(ele.l > ele.r || ele.l > cp[r] || ele.r < cp[l])return;
    if(ele.l <= cp[l] && cp[r] <= ele.r){
        if(ele.ty == 0){
            if(st[id][0] > ele.l){
                undo_stack[stksz++] = make_pair(make_pair(id, 0), st[id][0]);
                st[id][0] = ele.l;  
                cnt++;
            }
        }
        else {
            if(st[id][1] < ele.r){
                undo_stack[stksz++] = make_pair(make_pair(id, 1), st[id][1]);
                st[id][1] = ele.r;
                cnt++;
            }
        }
    }
    else {
        int mid = (l+r)>>1;
        upd_st(ele, cnt, l, mid, id<<1);
        upd_st(ele, cnt, mid+1, r, id<<1|1);
    }
}

int query_st(int pos, int l = 0, int r = cp.size()-1, int id = 1){
    int re = max(st[id][1] - pos, pos - st[id][0]);
    if(l < r){
        int mid = (l + r)>>1;
        if(pos <= cp[mid])re = max(re, query_st(pos, l, mid, id<<1));
        else re = max(re, query_st(pos, mid+1, r, id<<1|1));
    }
    return re;
}

void time_add(int ql, int qr, const ev_upd &ele, int l = 0, int r = cpy.size()-1, int id = 1){
    // cout << ql << " " << qr << " " << ele.l << " " << ele.r << " " << ele.ty << endl;  
    if(ql > qr || ql > cpy[r] || qr < cpy[l])return;
    if(ql <= cpy[l] && cpy[r] <= qr)st_time[id].push_back(ele);
    else {
        int mid = (l+r)>>1;
        time_add(ql, qr, ele, l, mid, id<<1);
        time_add(ql, qr, ele, mid+1, r, id<<1|1);
    }
}

int sumcol[maxn];
int sumall = 0, ptr = 0;

void time_dfs(int l = 0, int r = cpy.size()-1, int id = 1){
    int cnt = 0;
    for(auto ele: st_time[id])  {
        upd_st(ele, cnt);
    }
    if(l == r){
        while(ptr < ev.size() && ev[ptr].first <= cpy[l]){
            if(ev[ptr].second.first){
                sumcol[ty[ev[ptr].second.second]]--;
                if(!sumcol[ty[ev[ptr].second.second]])sumall--;
            }
            else {
                if(!sumcol[ty[ev[ptr].second.second]])sumall++;
                sumcol[ty[ev[ptr].second.second]]++;
            }
            ptr++;
        }
        // cout << sumall << endl;
        for(auto qq: leaf_q[cpy[l]]){
            ans[qq.second] = sumall < k ? -1 : query_st(qq.first);
        }
    }
    else {
        int mid = (l+r)>>1;
        time_dfs(l, mid, id<<1);
        time_dfs(mid+1, r, id<<1|1);
    }
    for(int i = 0; i < cnt; i++){
        stksz--;
        st[undo_stack[stksz].first.first][undo_stack[stksz].first.second] = undo_stack[stksz].second;
    }
}


void add_range(int l, int r){
    if(l != -INF)last_time[l][0] = crrt;
    if(r != INF)last_time[r][1] = crrt;
}

void del_range(int l, int r){
    // return;
    if(l == -INF){
        ev_upd ele(-INF, p[r], 1);
        time_add(last_time[r][1], crrt-1, ele);
        last_time[r][1] = 0;
    }
    else if(r == INF){
        time_add(last_time[l][0], crrt-1, ev_upd(p[l], INF, 0));
        last_time[l][0] = 0;
    }
    else {
        int mid = (p[l]+p[r])/2;
        time_add(last_time[l][0], crrt-1, ev_upd(p[l], mid, 0));
        time_add(last_time[r][1], crrt-1, ev_upd(mid+1, p[r], 1));
        last_time[r][1] = 0;
        last_time[l][0] = 0;
    }
}

void add(int x, int col){
    // cout << x << endl;
    auto it = pos[col].insert(make_pair(p[x], x)).first;
    if(pos[col].size() == 1){
        add_range(-INF, x);
        add_range(x, INF);
    }
    else {
        if(next(it) == pos[col].end()){
            int prv = prev(it)->second;
            del_range(prv, INF);
            add_range(prv, x);
            add_range(x, INF);
        }
        else if(it == pos[col].begin()){
            int nxt = next(it)->second;
            del_range(-INF, nxt);
            add_range(-INF, x);
            add_range(x, nxt);
        }
        else {
            int prv = prev(it)->second;
            int nxt = next(it)->second;
            del_range(prv, nxt);
            add_range(prv, x);
            add_range(x, nxt);
        }
    }
}

void del(int x, int col){
    // cout << "DEL " << x << endl;
    auto it = pos[col].erase(pos[col].find(make_pair(p[x], x)));
    if(pos[col].size() == 0){
        del_range(-INF, x);
        del_range(x, INF);
    }
    else {
        if(it == pos[col].end()){
            int prv = prev(it)->second;
            del_range(x, INF);
            del_range(prv, x);
            add_range(prv, INF);
        }
        else if(it == pos[col].begin()){
            int nxt = it->second;
            del_range(-INF, x);
            del_range(x, nxt);
            add_range(-INF, nxt);
        }
        else {
            int prv = prev(it)->second;
            int nxt = it->second;
            del_range(x, nxt);
            del_range(prv, x);
            add_range(prv, nxt);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k >> q;
    for(int i = 0; i < 4*maxn; i++)st[i][0] = INF, st[i][1] = -INF;
    for(int i = 1; i <= n; i++){
        int a, b;
        cin >> p[i] >> ty[i] >> a >> b;
        ev.push_back({a, {0, i}});
        ev.push_back({b+1, {1, i}});
    }
    for(int i = 1; i <= q; i++){
        cin >> L[i] >> Y[i];
        cp.push_back(L[i]);
        cpy.push_back(Y[i]);
        leaf_q[Y[i]].push_back({L[i], i});
    }
    sort(cp.begin(), cp.end());
    cp.erase(unique(cp.begin(), cp.end()), cp.end());
    sort(cpy.begin(), cpy.end());
    cpy.erase(unique(cpy.begin(), cpy.end()), cpy.end()); 
    sort(ev.begin(), ev.end());
    for(auto v: ev){
        crrt = v.first;
        if(v.second.first)del(v.second.second, ty[v.second.second]);
        else add(v.second.second, ty[v.second.second]);
    }
    // cout << "SUS" << endl;  
    // if(n > 100000)return 0;
    time_dfs();
    for(int i = 1; i <= q; i++)cout << ans[i] << "\n";
    
}

Compilation message

new_home.cpp:1:36: warning: bad option '-farch=skylake' to pragma 'optimize' [-Wpragmas]
    1 | #pragma GCC optimize("arch=skylake")
      |                                    ^
new_home.cpp:20:35: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
   20 |     ev_upd(int _l, int _r, int _ty): l(_l), r(_r), ty(_ty){};
      |                                   ^
new_home.cpp:37:84: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
   37 | void upd_st(const ev_upd &ele, int &cnt, int l = 0, int r = cp.size()-1, int id = 1){
      |                                                                                    ^
new_home.cpp:62:65: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
   62 | int query_st(int pos, int l = 0, int r = cp.size()-1, int id = 1){
      |                                                                 ^
new_home.cpp:72:93: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
   72 | void time_add(int ql, int qr, const ev_upd &ele, int l = 0, int r = cpy.size()-1, int id = 1){
      |                                                                                             ^
new_home.cpp:86:58: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
   86 | void time_dfs(int l = 0, int r = cpy.size()-1, int id = 1){
      |                                                          ^
new_home.cpp: In function 'void time_dfs(int, int, int)':
new_home.cpp:92:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |         while(ptr < ev.size() && ev[ptr].first <= cpy[l]){
      |               ~~~~^~~~~~~~~~~
new_home.cpp: At global scope:
new_home.cpp:120:28: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
  120 | void add_range(int l, int r){
      |                            ^
new_home.cpp:125:28: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
  125 | void del_range(int l, int r){
      |                            ^
new_home.cpp:145:24: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
  145 | void add(int x, int col){
      |                        ^
new_home.cpp:175:24: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
  175 | void del(int x, int col){
      |                        ^
new_home.cpp:205:10: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
  205 | int main() {
      |          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 56144 KB Output is correct
2 Correct 16 ms 55132 KB Output is correct
3 Correct 15 ms 55132 KB Output is correct
4 Correct 17 ms 55216 KB Output is correct
5 Correct 17 ms 55388 KB Output is correct
6 Correct 18 ms 55384 KB Output is correct
7 Correct 17 ms 55388 KB Output is correct
8 Correct 19 ms 55640 KB Output is correct
9 Correct 18 ms 55388 KB Output is correct
10 Correct 18 ms 55388 KB Output is correct
11 Correct 19 ms 55388 KB Output is correct
12 Correct 18 ms 55384 KB Output is correct
13 Correct 17 ms 55388 KB Output is correct
14 Correct 17 ms 55388 KB Output is correct
15 Correct 19 ms 55532 KB Output is correct
16 Correct 19 ms 55392 KB Output is correct
17 Correct 19 ms 55388 KB Output is correct
18 Correct 18 ms 55388 KB Output is correct
19 Correct 18 ms 55388 KB Output is correct
20 Correct 19 ms 55388 KB Output is correct
21 Correct 17 ms 55388 KB Output is correct
22 Correct 18 ms 55524 KB Output is correct
23 Correct 22 ms 55540 KB Output is correct
24 Correct 19 ms 55388 KB Output is correct
25 Correct 19 ms 55528 KB Output is correct
26 Correct 19 ms 55440 KB Output is correct
27 Correct 21 ms 55640 KB Output is correct
28 Correct 18 ms 55388 KB Output is correct
29 Correct 18 ms 55376 KB Output is correct
30 Correct 18 ms 55388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 56144 KB Output is correct
2 Correct 16 ms 55132 KB Output is correct
3 Correct 15 ms 55132 KB Output is correct
4 Correct 17 ms 55216 KB Output is correct
5 Correct 17 ms 55388 KB Output is correct
6 Correct 18 ms 55384 KB Output is correct
7 Correct 17 ms 55388 KB Output is correct
8 Correct 19 ms 55640 KB Output is correct
9 Correct 18 ms 55388 KB Output is correct
10 Correct 18 ms 55388 KB Output is correct
11 Correct 19 ms 55388 KB Output is correct
12 Correct 18 ms 55384 KB Output is correct
13 Correct 17 ms 55388 KB Output is correct
14 Correct 17 ms 55388 KB Output is correct
15 Correct 19 ms 55532 KB Output is correct
16 Correct 19 ms 55392 KB Output is correct
17 Correct 19 ms 55388 KB Output is correct
18 Correct 18 ms 55388 KB Output is correct
19 Correct 18 ms 55388 KB Output is correct
20 Correct 19 ms 55388 KB Output is correct
21 Correct 17 ms 55388 KB Output is correct
22 Correct 18 ms 55524 KB Output is correct
23 Correct 22 ms 55540 KB Output is correct
24 Correct 19 ms 55388 KB Output is correct
25 Correct 19 ms 55528 KB Output is correct
26 Correct 19 ms 55440 KB Output is correct
27 Correct 21 ms 55640 KB Output is correct
28 Correct 18 ms 55388 KB Output is correct
29 Correct 18 ms 55376 KB Output is correct
30 Correct 18 ms 55388 KB Output is correct
31 Correct 1522 ms 139244 KB Output is correct
32 Correct 101 ms 67100 KB Output is correct
33 Correct 1171 ms 133012 KB Output is correct
34 Correct 1454 ms 132940 KB Output is correct
35 Correct 1456 ms 139460 KB Output is correct
36 Correct 1257 ms 138476 KB Output is correct
37 Correct 896 ms 120292 KB Output is correct
38 Correct 777 ms 120000 KB Output is correct
39 Correct 504 ms 101708 KB Output is correct
40 Correct 544 ms 106500 KB Output is correct
41 Correct 722 ms 101704 KB Output is correct
42 Correct 712 ms 102896 KB Output is correct
43 Correct 60 ms 66504 KB Output is correct
44 Correct 692 ms 99840 KB Output is correct
45 Correct 564 ms 92920 KB Output is correct
46 Correct 329 ms 80068 KB Output is correct
47 Correct 216 ms 79032 KB Output is correct
48 Correct 195 ms 76740 KB Output is correct
49 Correct 321 ms 84864 KB Output is correct
50 Correct 492 ms 96912 KB Output is correct
51 Correct 292 ms 81132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 990 ms 155992 KB Output is correct
2 Correct 1056 ms 150116 KB Output is correct
3 Correct 882 ms 152332 KB Output is correct
4 Correct 1014 ms 158536 KB Output is correct
5 Correct 984 ms 147980 KB Output is correct
6 Correct 1043 ms 148160 KB Output is correct
7 Correct 817 ms 153104 KB Output is correct
8 Correct 929 ms 158304 KB Output is correct
9 Correct 981 ms 159784 KB Output is correct
10 Correct 934 ms 158272 KB Output is correct
11 Correct 612 ms 151896 KB Output is correct
12 Correct 678 ms 157368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5077 ms 338992 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 56144 KB Output is correct
2 Correct 16 ms 55132 KB Output is correct
3 Correct 15 ms 55132 KB Output is correct
4 Correct 17 ms 55216 KB Output is correct
5 Correct 17 ms 55388 KB Output is correct
6 Correct 18 ms 55384 KB Output is correct
7 Correct 17 ms 55388 KB Output is correct
8 Correct 19 ms 55640 KB Output is correct
9 Correct 18 ms 55388 KB Output is correct
10 Correct 18 ms 55388 KB Output is correct
11 Correct 19 ms 55388 KB Output is correct
12 Correct 18 ms 55384 KB Output is correct
13 Correct 17 ms 55388 KB Output is correct
14 Correct 17 ms 55388 KB Output is correct
15 Correct 19 ms 55532 KB Output is correct
16 Correct 19 ms 55392 KB Output is correct
17 Correct 19 ms 55388 KB Output is correct
18 Correct 18 ms 55388 KB Output is correct
19 Correct 18 ms 55388 KB Output is correct
20 Correct 19 ms 55388 KB Output is correct
21 Correct 17 ms 55388 KB Output is correct
22 Correct 18 ms 55524 KB Output is correct
23 Correct 22 ms 55540 KB Output is correct
24 Correct 19 ms 55388 KB Output is correct
25 Correct 19 ms 55528 KB Output is correct
26 Correct 19 ms 55440 KB Output is correct
27 Correct 21 ms 55640 KB Output is correct
28 Correct 18 ms 55388 KB Output is correct
29 Correct 18 ms 55376 KB Output is correct
30 Correct 18 ms 55388 KB Output is correct
31 Correct 1522 ms 139244 KB Output is correct
32 Correct 101 ms 67100 KB Output is correct
33 Correct 1171 ms 133012 KB Output is correct
34 Correct 1454 ms 132940 KB Output is correct
35 Correct 1456 ms 139460 KB Output is correct
36 Correct 1257 ms 138476 KB Output is correct
37 Correct 896 ms 120292 KB Output is correct
38 Correct 777 ms 120000 KB Output is correct
39 Correct 504 ms 101708 KB Output is correct
40 Correct 544 ms 106500 KB Output is correct
41 Correct 722 ms 101704 KB Output is correct
42 Correct 712 ms 102896 KB Output is correct
43 Correct 60 ms 66504 KB Output is correct
44 Correct 692 ms 99840 KB Output is correct
45 Correct 564 ms 92920 KB Output is correct
46 Correct 329 ms 80068 KB Output is correct
47 Correct 216 ms 79032 KB Output is correct
48 Correct 195 ms 76740 KB Output is correct
49 Correct 321 ms 84864 KB Output is correct
50 Correct 492 ms 96912 KB Output is correct
51 Correct 292 ms 81132 KB Output is correct
52 Correct 521 ms 98200 KB Output is correct
53 Correct 466 ms 94972 KB Output is correct
54 Correct 1025 ms 120736 KB Output is correct
55 Correct 679 ms 108040 KB Output is correct
56 Correct 677 ms 107236 KB Output is correct
57 Correct 691 ms 103104 KB Output is correct
58 Correct 675 ms 103616 KB Output is correct
59 Correct 647 ms 103192 KB Output is correct
60 Correct 712 ms 103288 KB Output is correct
61 Correct 57 ms 66676 KB Output is correct
62 Correct 534 ms 100964 KB Output is correct
63 Correct 829 ms 112668 KB Output is correct
64 Correct 911 ms 115300 KB Output is correct
65 Correct 1004 ms 116924 KB Output is correct
66 Correct 814 ms 106928 KB Output is correct
67 Correct 121 ms 68552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 56144 KB Output is correct
2 Correct 16 ms 55132 KB Output is correct
3 Correct 15 ms 55132 KB Output is correct
4 Correct 17 ms 55216 KB Output is correct
5 Correct 17 ms 55388 KB Output is correct
6 Correct 18 ms 55384 KB Output is correct
7 Correct 17 ms 55388 KB Output is correct
8 Correct 19 ms 55640 KB Output is correct
9 Correct 18 ms 55388 KB Output is correct
10 Correct 18 ms 55388 KB Output is correct
11 Correct 19 ms 55388 KB Output is correct
12 Correct 18 ms 55384 KB Output is correct
13 Correct 17 ms 55388 KB Output is correct
14 Correct 17 ms 55388 KB Output is correct
15 Correct 19 ms 55532 KB Output is correct
16 Correct 19 ms 55392 KB Output is correct
17 Correct 19 ms 55388 KB Output is correct
18 Correct 18 ms 55388 KB Output is correct
19 Correct 18 ms 55388 KB Output is correct
20 Correct 19 ms 55388 KB Output is correct
21 Correct 17 ms 55388 KB Output is correct
22 Correct 18 ms 55524 KB Output is correct
23 Correct 22 ms 55540 KB Output is correct
24 Correct 19 ms 55388 KB Output is correct
25 Correct 19 ms 55528 KB Output is correct
26 Correct 19 ms 55440 KB Output is correct
27 Correct 21 ms 55640 KB Output is correct
28 Correct 18 ms 55388 KB Output is correct
29 Correct 18 ms 55376 KB Output is correct
30 Correct 18 ms 55388 KB Output is correct
31 Correct 1522 ms 139244 KB Output is correct
32 Correct 101 ms 67100 KB Output is correct
33 Correct 1171 ms 133012 KB Output is correct
34 Correct 1454 ms 132940 KB Output is correct
35 Correct 1456 ms 139460 KB Output is correct
36 Correct 1257 ms 138476 KB Output is correct
37 Correct 896 ms 120292 KB Output is correct
38 Correct 777 ms 120000 KB Output is correct
39 Correct 504 ms 101708 KB Output is correct
40 Correct 544 ms 106500 KB Output is correct
41 Correct 722 ms 101704 KB Output is correct
42 Correct 712 ms 102896 KB Output is correct
43 Correct 60 ms 66504 KB Output is correct
44 Correct 692 ms 99840 KB Output is correct
45 Correct 564 ms 92920 KB Output is correct
46 Correct 329 ms 80068 KB Output is correct
47 Correct 216 ms 79032 KB Output is correct
48 Correct 195 ms 76740 KB Output is correct
49 Correct 321 ms 84864 KB Output is correct
50 Correct 492 ms 96912 KB Output is correct
51 Correct 292 ms 81132 KB Output is correct
52 Correct 990 ms 155992 KB Output is correct
53 Correct 1056 ms 150116 KB Output is correct
54 Correct 882 ms 152332 KB Output is correct
55 Correct 1014 ms 158536 KB Output is correct
56 Correct 984 ms 147980 KB Output is correct
57 Correct 1043 ms 148160 KB Output is correct
58 Correct 817 ms 153104 KB Output is correct
59 Correct 929 ms 158304 KB Output is correct
60 Correct 981 ms 159784 KB Output is correct
61 Correct 934 ms 158272 KB Output is correct
62 Correct 612 ms 151896 KB Output is correct
63 Correct 678 ms 157368 KB Output is correct
64 Execution timed out 5077 ms 338992 KB Time limit exceeded
65 Halted 0 ms 0 KB -