답안 #968894

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

const int maxn = 6e5+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), ty(_ty), val(_val){};
};

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


vector<ev_upd> st_time[4*maxn];

int crrt;

long long undo_stack[maxn*200];
int stksz = 0;

// 0 is min, 1 is max

void upd_st(int ql, int qr, int val, int ty, int &cnt, int l = 0, int r = cp.size()-1, int id = 1){
    if(ql > qr || ql > r || qr < l)return;
    if(ql <= l && r <= qr){
        if(ty == 0){
            if(st[id][0] > val){
                undo_stack[stksz++] = ((long long)id<<30)|st[id][0];
                // cout << undo_stack[stksz-1] << endl;
                st[id][0] = val;  
                cnt++;
            }
        }
        else {
            if(st[id][1] < val){
                undo_stack[stksz++] = (1LL<<60)|(((long long)id<<30)|st[id][1]);
                // cout << undo_stack[stksz-1] << endl;
                st[id][1] = val;
                cnt++;
            }
        }
    }
    else {
        int mid = (l+r)>>1;
        upd_st(ql, qr, val, ty, cnt, l, mid, id<<1);
        upd_st(ql, qr, val, ty, 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;
}

int cnt_segment;

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);
        cnt_segment++;
    }
    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.l, ele.r, ele.val, ele.ty, 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[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--;
        long long tmp = undo_stack[stksz];
        // cout << tmp << " " << ((tmp&((1LL<<60) - 1)) >> 30) << endl; 
        st[(tmp&((1LL<<60) - 1)) >> 30][tmp>>60] = tmp&((1<<30) - 1); 
    }
}


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

int lb(int x){
    return lower_bound(cp.begin(), cp.end(), x) - cp.begin();
}

int ub(int x){
    return upper_bound(cp.begin(), cp.end(), x) - cp.begin() - 1;
}

void del_range(int l, int r){
    // return;
    if(l == -INF){
        ev_upd ele(lb(-INF), ub(p[r]), 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(lb(p[l]), ub(INF), p[l], 0));
        last_time[l][0] = 0;
    }
    else {
        int mid = (p[l]+p[r])/2;
        time_add(last_time[l][0], crrt-1, ev_upd(lb(p[l]), ub(mid), p[l], 0));
        time_add(last_time[r][1], crrt-1, ev_upd(lb(mid+1), ub(p[r]), 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);
    auto t1 = clock();
    // freopen("test_input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    cin >> n >> k >> q;
    for(int i = 0; i < 4*maxn; i++)st[i][0] = INF, st[i][1] = 0;
    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]);
    }
    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(int i = 1; i <= q; i++)leaf_q[lower_bound(cpy.begin(), cpy.end(), Y[i]) - cpy.begin()].push_back({L[i], i});
    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;
    // cout << cnt_segment << endl;
    // return 0;
    time_dfs();
    // cout << clock() - t1 << endl;
    for(int i = 1; i <= q; i++)cout << ans[i] << "\n";
    
}

Compilation message

new_home.cpp: In constructor 'ev_upd::ev_upd(int, int, int, int)':
new_home.cpp:19:20: warning: 'ev_upd::ty' will be initialized after [-Wreorder]
   19 |     int l, r, val, ty;
      |                    ^~
new_home.cpp:19:15: warning:   'int ev_upd::val' [-Wreorder]
   19 |     int l, r, val, ty;
      |               ^~~
new_home.cpp:20:5: warning:   when initialized here [-Wreorder]
   20 |     ev_upd(int _l, int _r, int _val, int _ty): l(_l), r(_r), ty(_ty), val(_val){};
      |     ^~~~~~
new_home.cpp: In function 'void time_dfs(int, int, int)':
new_home.cpp:99: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]
   99 |         while(ptr < ev.size() && ev[ptr].first <= cpy[l]){
      |               ~~~~^~~~~~~~~~~
new_home.cpp: In function 'int main()':
new_home.cpp:225:10: warning: unused variable 't1' [-Wunused-variable]
  225 |     auto t1 = clock();
      |          ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 120912 KB Output is correct
2 Correct 44 ms 120620 KB Output is correct
3 Correct 44 ms 120656 KB Output is correct
4 Correct 44 ms 119132 KB Output is correct
5 Correct 48 ms 119052 KB Output is correct
6 Correct 48 ms 119376 KB Output is correct
7 Correct 44 ms 119120 KB Output is correct
8 Correct 47 ms 119416 KB Output is correct
9 Correct 45 ms 119120 KB Output is correct
10 Correct 46 ms 119456 KB Output is correct
11 Correct 46 ms 119132 KB Output is correct
12 Correct 45 ms 119288 KB Output is correct
13 Correct 44 ms 119132 KB Output is correct
14 Correct 44 ms 119116 KB Output is correct
15 Correct 44 ms 119124 KB Output is correct
16 Correct 45 ms 119124 KB Output is correct
17 Correct 45 ms 119120 KB Output is correct
18 Correct 48 ms 119216 KB Output is correct
19 Correct 48 ms 119252 KB Output is correct
20 Correct 50 ms 119120 KB Output is correct
21 Correct 44 ms 119124 KB Output is correct
22 Correct 44 ms 119120 KB Output is correct
23 Correct 46 ms 119124 KB Output is correct
24 Correct 46 ms 119380 KB Output is correct
25 Correct 45 ms 119120 KB Output is correct
26 Correct 45 ms 119028 KB Output is correct
27 Correct 50 ms 119124 KB Output is correct
28 Correct 49 ms 119120 KB Output is correct
29 Correct 45 ms 119120 KB Output is correct
30 Correct 48 ms 119136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 120912 KB Output is correct
2 Correct 44 ms 120620 KB Output is correct
3 Correct 44 ms 120656 KB Output is correct
4 Correct 44 ms 119132 KB Output is correct
5 Correct 48 ms 119052 KB Output is correct
6 Correct 48 ms 119376 KB Output is correct
7 Correct 44 ms 119120 KB Output is correct
8 Correct 47 ms 119416 KB Output is correct
9 Correct 45 ms 119120 KB Output is correct
10 Correct 46 ms 119456 KB Output is correct
11 Correct 46 ms 119132 KB Output is correct
12 Correct 45 ms 119288 KB Output is correct
13 Correct 44 ms 119132 KB Output is correct
14 Correct 44 ms 119116 KB Output is correct
15 Correct 44 ms 119124 KB Output is correct
16 Correct 45 ms 119124 KB Output is correct
17 Correct 45 ms 119120 KB Output is correct
18 Correct 48 ms 119216 KB Output is correct
19 Correct 48 ms 119252 KB Output is correct
20 Correct 50 ms 119120 KB Output is correct
21 Correct 44 ms 119124 KB Output is correct
22 Correct 44 ms 119120 KB Output is correct
23 Correct 46 ms 119124 KB Output is correct
24 Correct 46 ms 119380 KB Output is correct
25 Correct 45 ms 119120 KB Output is correct
26 Correct 45 ms 119028 KB Output is correct
27 Correct 50 ms 119124 KB Output is correct
28 Correct 49 ms 119120 KB Output is correct
29 Correct 45 ms 119120 KB Output is correct
30 Correct 48 ms 119136 KB Output is correct
31 Correct 1506 ms 217712 KB Output is correct
32 Correct 134 ms 132552 KB Output is correct
33 Correct 1155 ms 211100 KB Output is correct
34 Correct 1405 ms 210628 KB Output is correct
35 Correct 1540 ms 217928 KB Output is correct
36 Correct 1249 ms 217404 KB Output is correct
37 Correct 926 ms 195536 KB Output is correct
38 Correct 831 ms 195264 KB Output is correct
39 Correct 535 ms 171352 KB Output is correct
40 Correct 568 ms 177492 KB Output is correct
41 Correct 740 ms 175204 KB Output is correct
42 Correct 724 ms 172996 KB Output is correct
43 Correct 88 ms 131524 KB Output is correct
44 Correct 693 ms 168728 KB Output is correct
45 Correct 563 ms 159564 KB Output is correct
46 Correct 361 ms 142728 KB Output is correct
47 Correct 246 ms 141504 KB Output is correct
48 Correct 224 ms 138176 KB Output is correct
49 Correct 341 ms 148672 KB Output is correct
50 Correct 503 ms 165156 KB Output is correct
51 Correct 380 ms 144252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1099 ms 205492 KB Output is correct
2 Correct 1242 ms 198640 KB Output is correct
3 Correct 746 ms 201096 KB Output is correct
4 Correct 1010 ms 205084 KB Output is correct
5 Correct 1249 ms 196032 KB Output is correct
6 Correct 1253 ms 196708 KB Output is correct
7 Correct 709 ms 201612 KB Output is correct
8 Correct 916 ms 205236 KB Output is correct
9 Correct 1018 ms 208164 KB Output is correct
10 Correct 1112 ms 206908 KB Output is correct
11 Correct 775 ms 202248 KB Output is correct
12 Correct 854 ms 206380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5104 ms 448484 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 120912 KB Output is correct
2 Correct 44 ms 120620 KB Output is correct
3 Correct 44 ms 120656 KB Output is correct
4 Correct 44 ms 119132 KB Output is correct
5 Correct 48 ms 119052 KB Output is correct
6 Correct 48 ms 119376 KB Output is correct
7 Correct 44 ms 119120 KB Output is correct
8 Correct 47 ms 119416 KB Output is correct
9 Correct 45 ms 119120 KB Output is correct
10 Correct 46 ms 119456 KB Output is correct
11 Correct 46 ms 119132 KB Output is correct
12 Correct 45 ms 119288 KB Output is correct
13 Correct 44 ms 119132 KB Output is correct
14 Correct 44 ms 119116 KB Output is correct
15 Correct 44 ms 119124 KB Output is correct
16 Correct 45 ms 119124 KB Output is correct
17 Correct 45 ms 119120 KB Output is correct
18 Correct 48 ms 119216 KB Output is correct
19 Correct 48 ms 119252 KB Output is correct
20 Correct 50 ms 119120 KB Output is correct
21 Correct 44 ms 119124 KB Output is correct
22 Correct 44 ms 119120 KB Output is correct
23 Correct 46 ms 119124 KB Output is correct
24 Correct 46 ms 119380 KB Output is correct
25 Correct 45 ms 119120 KB Output is correct
26 Correct 45 ms 119028 KB Output is correct
27 Correct 50 ms 119124 KB Output is correct
28 Correct 49 ms 119120 KB Output is correct
29 Correct 45 ms 119120 KB Output is correct
30 Correct 48 ms 119136 KB Output is correct
31 Correct 1506 ms 217712 KB Output is correct
32 Correct 134 ms 132552 KB Output is correct
33 Correct 1155 ms 211100 KB Output is correct
34 Correct 1405 ms 210628 KB Output is correct
35 Correct 1540 ms 217928 KB Output is correct
36 Correct 1249 ms 217404 KB Output is correct
37 Correct 926 ms 195536 KB Output is correct
38 Correct 831 ms 195264 KB Output is correct
39 Correct 535 ms 171352 KB Output is correct
40 Correct 568 ms 177492 KB Output is correct
41 Correct 740 ms 175204 KB Output is correct
42 Correct 724 ms 172996 KB Output is correct
43 Correct 88 ms 131524 KB Output is correct
44 Correct 693 ms 168728 KB Output is correct
45 Correct 563 ms 159564 KB Output is correct
46 Correct 361 ms 142728 KB Output is correct
47 Correct 246 ms 141504 KB Output is correct
48 Correct 224 ms 138176 KB Output is correct
49 Correct 341 ms 148672 KB Output is correct
50 Correct 503 ms 165156 KB Output is correct
51 Correct 380 ms 144252 KB Output is correct
52 Correct 506 ms 166588 KB Output is correct
53 Correct 464 ms 163008 KB Output is correct
54 Correct 1008 ms 194500 KB Output is correct
55 Correct 712 ms 179876 KB Output is correct
56 Correct 685 ms 178860 KB Output is correct
57 Correct 713 ms 174588 KB Output is correct
58 Correct 681 ms 174800 KB Output is correct
59 Correct 646 ms 174024 KB Output is correct
60 Correct 712 ms 174760 KB Output is correct
61 Correct 89 ms 133864 KB Output is correct
62 Correct 528 ms 170168 KB Output is correct
63 Correct 818 ms 185200 KB Output is correct
64 Correct 990 ms 188948 KB Output is correct
65 Correct 992 ms 191680 KB Output is correct
66 Correct 806 ms 179304 KB Output is correct
67 Correct 164 ms 136128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 120912 KB Output is correct
2 Correct 44 ms 120620 KB Output is correct
3 Correct 44 ms 120656 KB Output is correct
4 Correct 44 ms 119132 KB Output is correct
5 Correct 48 ms 119052 KB Output is correct
6 Correct 48 ms 119376 KB Output is correct
7 Correct 44 ms 119120 KB Output is correct
8 Correct 47 ms 119416 KB Output is correct
9 Correct 45 ms 119120 KB Output is correct
10 Correct 46 ms 119456 KB Output is correct
11 Correct 46 ms 119132 KB Output is correct
12 Correct 45 ms 119288 KB Output is correct
13 Correct 44 ms 119132 KB Output is correct
14 Correct 44 ms 119116 KB Output is correct
15 Correct 44 ms 119124 KB Output is correct
16 Correct 45 ms 119124 KB Output is correct
17 Correct 45 ms 119120 KB Output is correct
18 Correct 48 ms 119216 KB Output is correct
19 Correct 48 ms 119252 KB Output is correct
20 Correct 50 ms 119120 KB Output is correct
21 Correct 44 ms 119124 KB Output is correct
22 Correct 44 ms 119120 KB Output is correct
23 Correct 46 ms 119124 KB Output is correct
24 Correct 46 ms 119380 KB Output is correct
25 Correct 45 ms 119120 KB Output is correct
26 Correct 45 ms 119028 KB Output is correct
27 Correct 50 ms 119124 KB Output is correct
28 Correct 49 ms 119120 KB Output is correct
29 Correct 45 ms 119120 KB Output is correct
30 Correct 48 ms 119136 KB Output is correct
31 Correct 1506 ms 217712 KB Output is correct
32 Correct 134 ms 132552 KB Output is correct
33 Correct 1155 ms 211100 KB Output is correct
34 Correct 1405 ms 210628 KB Output is correct
35 Correct 1540 ms 217928 KB Output is correct
36 Correct 1249 ms 217404 KB Output is correct
37 Correct 926 ms 195536 KB Output is correct
38 Correct 831 ms 195264 KB Output is correct
39 Correct 535 ms 171352 KB Output is correct
40 Correct 568 ms 177492 KB Output is correct
41 Correct 740 ms 175204 KB Output is correct
42 Correct 724 ms 172996 KB Output is correct
43 Correct 88 ms 131524 KB Output is correct
44 Correct 693 ms 168728 KB Output is correct
45 Correct 563 ms 159564 KB Output is correct
46 Correct 361 ms 142728 KB Output is correct
47 Correct 246 ms 141504 KB Output is correct
48 Correct 224 ms 138176 KB Output is correct
49 Correct 341 ms 148672 KB Output is correct
50 Correct 503 ms 165156 KB Output is correct
51 Correct 380 ms 144252 KB Output is correct
52 Correct 1099 ms 205492 KB Output is correct
53 Correct 1242 ms 198640 KB Output is correct
54 Correct 746 ms 201096 KB Output is correct
55 Correct 1010 ms 205084 KB Output is correct
56 Correct 1249 ms 196032 KB Output is correct
57 Correct 1253 ms 196708 KB Output is correct
58 Correct 709 ms 201612 KB Output is correct
59 Correct 916 ms 205236 KB Output is correct
60 Correct 1018 ms 208164 KB Output is correct
61 Correct 1112 ms 206908 KB Output is correct
62 Correct 775 ms 202248 KB Output is correct
63 Correct 854 ms 206380 KB Output is correct
64 Execution timed out 5104 ms 448484 KB Time limit exceeded
65 Halted 0 ms 0 KB -