답안 #968765

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
968765 2024-04-24T03:58:35 Z socpite 새 집 (APIO18_new_home) C++14
57 / 100
5000 ms 544384 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, val, ty;
    ev_upd(int _l, int _r, int _val, int _ty): l(_l), r(_r), val(_val), ty(_ty){};
    bool operator<(const ev_upd &b)const {
        return tie(l, r, val, ty) < tie(b.l, b.r, b.val, b.ty);
    }
};

map<ev_upd, vector<int>> mp;
map<int, vector<pair<int, int>>> leaf_q;  
multiset<int> pos[maxn];

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 > 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.val){
                undo_stack[stksz++] = make_pair(make_pair(id, 0), st[id][0]);
                st[id][0] = ele.val;
                cnt++;
            }
        }
        else {
            if(st[id][1] < ele.val){
                undo_stack[stksz++] = make_pair(make_pair(id, 1), st[id][1]);
                st[id][1] = ele.val;
                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){
    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_st(int l, int r, int val, int ty){
    if(l > r)return;
    mp[ev_upd(l, r, val, ty)].push_back(crrt);
}

void del_st(int l, int r, int val, int ty){
    if(l > r)return;
    ev_upd ele(l, r, val, ty);
    time_add(mp[ele].back(), crrt - 1, ele);
    mp[ele].pop_back();
}

void add_range(int l, int r){
    if(l == -INF)add_st(-INF, r, r, 1);
    else if(r == INF)add_st(l+1, INF, l, 0);
    else {
        int mid = (l+r)/2;
        add_st(l+1, mid, l, 0);
        add_st(mid+1, r, r, 1);
    }
}

void del_range(int l, int r){
    if(l == -INF)del_st(-INF, r, r, 1);
    else if(r == INF)del_st(l+1, INF, l, 0);
    else {
        int mid = (l+r)/2;
        del_st(l+1, mid, l, 0);
        del_st(mid+1, r, r, 1);
    }
}

void add(int x, int col){
    auto it = pos[col].insert(x);
    if(pos[col].size() == 1){
        add_range(-INF, x);
        add_range(x, INF);
    }
    else {
        if(next(it) == pos[col].end()){
            del_range(*prev(it), INF);
            add_range(*prev(it), x);
            add_range(x, INF);
        }
        else if(it == pos[col].begin()){
            del_range(-INF, *next(it));
            add_range(-INF, x);
            add_range(x, *next(it));
        }
        else {
            del_range(*prev(it), *next(it));
            add_range(*prev(it), *it);
            add_range(*it, *next(it));
        }
    }
}

void del(int x, int col){
    auto it = pos[col].erase(pos[col].find(x));
    if(pos[col].size() == 0){
        del_range(-INF, x);
        del_range(x, INF);
    }
    else {
        if(it == pos[col].end()){
            del_range(x, INF);
            del_range(*prev(it), x);
            add_range(*prev(it), INF);
        }
        else if(it == pos[col].begin()){
            del_range(-INF, x);
            del_range(x, *it);
            add_range(-INF, *it);
        }
        else {
            del_range(x, *it);
            del_range(*prev(it), x);
            add_range(*prev(it), *it);
        }
    }
}

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(p[v.second.second], ty[v.second.second]);
        else add(p[v.second.second], ty[v.second.second]);
    }
    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:45: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
   20 |     ev_upd(int _l, int _r, int _val, int _ty): l(_l), r(_r), val(_val), ty(_ty){};
      |                                             ^
new_home.cpp:21:36: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
   21 |     bool operator<(const ev_upd &b)const {
      |                                    ^~~~~
new_home.cpp:39:84: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
   39 | void upd_st(const ev_upd &ele, int &cnt, int l = 0, int r = cp.size()-1, int id = 1){
      |                                                                                    ^
new_home.cpp:64:65: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
   64 | int query_st(int pos, int l = 0, int r = cp.size()-1, int id = 1){
      |                                                                 ^
new_home.cpp:74:93: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
   74 | 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:87:58: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
   87 | 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:93: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]
   93 |         while(ptr < ev.size() && ev[ptr].first <= cpy[l]){
      |               ~~~~^~~~~~~~~~~
new_home.cpp: At global scope:
new_home.cpp:120:42: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
  120 | void add_st(int l, int r, int val, int ty){
      |                                          ^
new_home.cpp:125:42: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
  125 | void del_st(int l, int r, int val, int ty){
      |                                          ^
new_home.cpp:132:28: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
  132 | void add_range(int l, int r){
      |                            ^
new_home.cpp:142:28: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
  142 | void del_range(int l, int r){
      |                            ^
new_home.cpp:152:24: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
  152 | void add(int x, int col){
      |                        ^
new_home.cpp:177:24: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
  177 | void del(int x, int col){
      |                        ^
new_home.cpp:202:10: warning: bad option '-farch=skylake' to attribute 'optimize' [-Wattributes]
  202 | int main() {
      |          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 57688 KB Output is correct
2 Correct 13 ms 57864 KB Output is correct
3 Correct 13 ms 57692 KB Output is correct
4 Correct 13 ms 57948 KB Output is correct
5 Correct 13 ms 57832 KB Output is correct
6 Correct 15 ms 58456 KB Output is correct
7 Correct 14 ms 58104 KB Output is correct
8 Correct 15 ms 58204 KB Output is correct
9 Correct 14 ms 58204 KB Output is correct
10 Correct 16 ms 58460 KB Output is correct
11 Correct 14 ms 58184 KB Output is correct
12 Correct 15 ms 58204 KB Output is correct
13 Correct 13 ms 57984 KB Output is correct
14 Correct 14 ms 57948 KB Output is correct
15 Correct 14 ms 58204 KB Output is correct
16 Correct 16 ms 58344 KB Output is correct
17 Correct 15 ms 58352 KB Output is correct
18 Correct 15 ms 58320 KB Output is correct
19 Correct 14 ms 58200 KB Output is correct
20 Correct 15 ms 58200 KB Output is correct
21 Correct 13 ms 57944 KB Output is correct
22 Correct 14 ms 58204 KB Output is correct
23 Correct 15 ms 58176 KB Output is correct
24 Correct 15 ms 58204 KB Output is correct
25 Correct 15 ms 58188 KB Output is correct
26 Correct 14 ms 58204 KB Output is correct
27 Correct 14 ms 57948 KB Output is correct
28 Correct 14 ms 58204 KB Output is correct
29 Correct 14 ms 58204 KB Output is correct
30 Correct 13 ms 57948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 57688 KB Output is correct
2 Correct 13 ms 57864 KB Output is correct
3 Correct 13 ms 57692 KB Output is correct
4 Correct 13 ms 57948 KB Output is correct
5 Correct 13 ms 57832 KB Output is correct
6 Correct 15 ms 58456 KB Output is correct
7 Correct 14 ms 58104 KB Output is correct
8 Correct 15 ms 58204 KB Output is correct
9 Correct 14 ms 58204 KB Output is correct
10 Correct 16 ms 58460 KB Output is correct
11 Correct 14 ms 58184 KB Output is correct
12 Correct 15 ms 58204 KB Output is correct
13 Correct 13 ms 57984 KB Output is correct
14 Correct 14 ms 57948 KB Output is correct
15 Correct 14 ms 58204 KB Output is correct
16 Correct 16 ms 58344 KB Output is correct
17 Correct 15 ms 58352 KB Output is correct
18 Correct 15 ms 58320 KB Output is correct
19 Correct 14 ms 58200 KB Output is correct
20 Correct 15 ms 58200 KB Output is correct
21 Correct 13 ms 57944 KB Output is correct
22 Correct 14 ms 58204 KB Output is correct
23 Correct 15 ms 58176 KB Output is correct
24 Correct 15 ms 58204 KB Output is correct
25 Correct 15 ms 58188 KB Output is correct
26 Correct 14 ms 58204 KB Output is correct
27 Correct 14 ms 57948 KB Output is correct
28 Correct 14 ms 58204 KB Output is correct
29 Correct 14 ms 58204 KB Output is correct
30 Correct 13 ms 57948 KB Output is correct
31 Correct 2185 ms 197332 KB Output is correct
32 Correct 82 ms 62660 KB Output is correct
33 Correct 1740 ms 190328 KB Output is correct
34 Correct 2033 ms 187844 KB Output is correct
35 Correct 2111 ms 198508 KB Output is correct
36 Correct 1821 ms 196904 KB Output is correct
37 Correct 1213 ms 173016 KB Output is correct
38 Correct 1050 ms 173228 KB Output is correct
39 Correct 721 ms 148584 KB Output is correct
40 Correct 780 ms 155300 KB Output is correct
41 Correct 1064 ms 140876 KB Output is correct
42 Correct 1074 ms 142488 KB Output is correct
43 Correct 55 ms 65456 KB Output is correct
44 Correct 1033 ms 138892 KB Output is correct
45 Correct 906 ms 129008 KB Output is correct
46 Correct 652 ms 112260 KB Output is correct
47 Correct 361 ms 107236 KB Output is correct
48 Correct 345 ms 104900 KB Output is correct
49 Correct 480 ms 117352 KB Output is correct
50 Correct 705 ms 132992 KB Output is correct
51 Correct 471 ms 112836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4189 ms 287780 KB Output is correct
2 Correct 4703 ms 336024 KB Output is correct
3 Correct 1850 ms 222264 KB Output is correct
4 Correct 3681 ms 271584 KB Output is correct
5 Correct 3487 ms 327440 KB Output is correct
6 Correct 4309 ms 332720 KB Output is correct
7 Correct 1763 ms 221796 KB Output is correct
8 Correct 2978 ms 269852 KB Output is correct
9 Correct 3662 ms 306112 KB Output is correct
10 Correct 4401 ms 342944 KB Output is correct
11 Correct 1790 ms 328992 KB Output is correct
12 Correct 2009 ms 332544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5047 ms 544384 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 57688 KB Output is correct
2 Correct 13 ms 57864 KB Output is correct
3 Correct 13 ms 57692 KB Output is correct
4 Correct 13 ms 57948 KB Output is correct
5 Correct 13 ms 57832 KB Output is correct
6 Correct 15 ms 58456 KB Output is correct
7 Correct 14 ms 58104 KB Output is correct
8 Correct 15 ms 58204 KB Output is correct
9 Correct 14 ms 58204 KB Output is correct
10 Correct 16 ms 58460 KB Output is correct
11 Correct 14 ms 58184 KB Output is correct
12 Correct 15 ms 58204 KB Output is correct
13 Correct 13 ms 57984 KB Output is correct
14 Correct 14 ms 57948 KB Output is correct
15 Correct 14 ms 58204 KB Output is correct
16 Correct 16 ms 58344 KB Output is correct
17 Correct 15 ms 58352 KB Output is correct
18 Correct 15 ms 58320 KB Output is correct
19 Correct 14 ms 58200 KB Output is correct
20 Correct 15 ms 58200 KB Output is correct
21 Correct 13 ms 57944 KB Output is correct
22 Correct 14 ms 58204 KB Output is correct
23 Correct 15 ms 58176 KB Output is correct
24 Correct 15 ms 58204 KB Output is correct
25 Correct 15 ms 58188 KB Output is correct
26 Correct 14 ms 58204 KB Output is correct
27 Correct 14 ms 57948 KB Output is correct
28 Correct 14 ms 58204 KB Output is correct
29 Correct 14 ms 58204 KB Output is correct
30 Correct 13 ms 57948 KB Output is correct
31 Correct 2185 ms 197332 KB Output is correct
32 Correct 82 ms 62660 KB Output is correct
33 Correct 1740 ms 190328 KB Output is correct
34 Correct 2033 ms 187844 KB Output is correct
35 Correct 2111 ms 198508 KB Output is correct
36 Correct 1821 ms 196904 KB Output is correct
37 Correct 1213 ms 173016 KB Output is correct
38 Correct 1050 ms 173228 KB Output is correct
39 Correct 721 ms 148584 KB Output is correct
40 Correct 780 ms 155300 KB Output is correct
41 Correct 1064 ms 140876 KB Output is correct
42 Correct 1074 ms 142488 KB Output is correct
43 Correct 55 ms 65456 KB Output is correct
44 Correct 1033 ms 138892 KB Output is correct
45 Correct 906 ms 129008 KB Output is correct
46 Correct 652 ms 112260 KB Output is correct
47 Correct 361 ms 107236 KB Output is correct
48 Correct 345 ms 104900 KB Output is correct
49 Correct 480 ms 117352 KB Output is correct
50 Correct 705 ms 132992 KB Output is correct
51 Correct 471 ms 112836 KB Output is correct
52 Correct 635 ms 120188 KB Output is correct
53 Correct 591 ms 113600 KB Output is correct
54 Correct 1432 ms 157604 KB Output is correct
55 Correct 956 ms 140992 KB Output is correct
56 Correct 887 ms 137416 KB Output is correct
57 Correct 1037 ms 139024 KB Output is correct
58 Correct 979 ms 135568 KB Output is correct
59 Correct 884 ms 132544 KB Output is correct
60 Correct 1036 ms 139216 KB Output is correct
61 Correct 54 ms 67520 KB Output is correct
62 Correct 628 ms 124884 KB Output is correct
63 Correct 1114 ms 144832 KB Output is correct
64 Correct 1252 ms 150780 KB Output is correct
65 Correct 1412 ms 157972 KB Output is correct
66 Correct 1170 ms 145460 KB Output is correct
67 Correct 138 ms 66756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 57688 KB Output is correct
2 Correct 13 ms 57864 KB Output is correct
3 Correct 13 ms 57692 KB Output is correct
4 Correct 13 ms 57948 KB Output is correct
5 Correct 13 ms 57832 KB Output is correct
6 Correct 15 ms 58456 KB Output is correct
7 Correct 14 ms 58104 KB Output is correct
8 Correct 15 ms 58204 KB Output is correct
9 Correct 14 ms 58204 KB Output is correct
10 Correct 16 ms 58460 KB Output is correct
11 Correct 14 ms 58184 KB Output is correct
12 Correct 15 ms 58204 KB Output is correct
13 Correct 13 ms 57984 KB Output is correct
14 Correct 14 ms 57948 KB Output is correct
15 Correct 14 ms 58204 KB Output is correct
16 Correct 16 ms 58344 KB Output is correct
17 Correct 15 ms 58352 KB Output is correct
18 Correct 15 ms 58320 KB Output is correct
19 Correct 14 ms 58200 KB Output is correct
20 Correct 15 ms 58200 KB Output is correct
21 Correct 13 ms 57944 KB Output is correct
22 Correct 14 ms 58204 KB Output is correct
23 Correct 15 ms 58176 KB Output is correct
24 Correct 15 ms 58204 KB Output is correct
25 Correct 15 ms 58188 KB Output is correct
26 Correct 14 ms 58204 KB Output is correct
27 Correct 14 ms 57948 KB Output is correct
28 Correct 14 ms 58204 KB Output is correct
29 Correct 14 ms 58204 KB Output is correct
30 Correct 13 ms 57948 KB Output is correct
31 Correct 2185 ms 197332 KB Output is correct
32 Correct 82 ms 62660 KB Output is correct
33 Correct 1740 ms 190328 KB Output is correct
34 Correct 2033 ms 187844 KB Output is correct
35 Correct 2111 ms 198508 KB Output is correct
36 Correct 1821 ms 196904 KB Output is correct
37 Correct 1213 ms 173016 KB Output is correct
38 Correct 1050 ms 173228 KB Output is correct
39 Correct 721 ms 148584 KB Output is correct
40 Correct 780 ms 155300 KB Output is correct
41 Correct 1064 ms 140876 KB Output is correct
42 Correct 1074 ms 142488 KB Output is correct
43 Correct 55 ms 65456 KB Output is correct
44 Correct 1033 ms 138892 KB Output is correct
45 Correct 906 ms 129008 KB Output is correct
46 Correct 652 ms 112260 KB Output is correct
47 Correct 361 ms 107236 KB Output is correct
48 Correct 345 ms 104900 KB Output is correct
49 Correct 480 ms 117352 KB Output is correct
50 Correct 705 ms 132992 KB Output is correct
51 Correct 471 ms 112836 KB Output is correct
52 Correct 4189 ms 287780 KB Output is correct
53 Correct 4703 ms 336024 KB Output is correct
54 Correct 1850 ms 222264 KB Output is correct
55 Correct 3681 ms 271584 KB Output is correct
56 Correct 3487 ms 327440 KB Output is correct
57 Correct 4309 ms 332720 KB Output is correct
58 Correct 1763 ms 221796 KB Output is correct
59 Correct 2978 ms 269852 KB Output is correct
60 Correct 3662 ms 306112 KB Output is correct
61 Correct 4401 ms 342944 KB Output is correct
62 Correct 1790 ms 328992 KB Output is correct
63 Correct 2009 ms 332544 KB Output is correct
64 Execution timed out 5047 ms 544384 KB Time limit exceeded
65 Halted 0 ms 0 KB -