Submission #781965

# Submission time Handle Problem Language Result Execution time Memory
781965 2023-07-13T14:13:45 Z doowey Sweeping (JOI20_sweeping) C++17
100 / 100
3233 ms 413536 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int M = (int)1e6 + 10;
int sol[M][2];

int n;

int val[2 * M];
int par[2 * M];
vector<int> lis[2 * M];
int c;

int id_x[M];
int id_y[M];
int away[M];

int fin(int u){
    if(par[u] == u) return u;
    return par[u]=fin(par[u]);
}

int get_val(int u){
    return val[fin(u)];
}

void unite(int x, int y){
    x=fin(x);
    y=fin(y);
    if(x==y) return;
    par[x]=y;
    val[y]=max(val[y],val[x]);
    if(lis[x].size() > lis[y].size()) swap(lis[x], lis[y]);
    for(auto p : lis[x]) lis[y].push_back(p);
    lis[x].clear();
}

bool yessir(int node){
    return par[node] == node;
}

void solve(int x, int y, vector<array<int, 3>> qr){
    if(qr.empty()) return;
    int sz = (n - x - y);
    if(sz == 0){
        for(auto p : qr){
            if(p[0] >= 1){
                id_x[p[0]] = p[1];
                id_y[p[0]] = p[2];
            }
            else if(p[0] == -1){
                sol[p[2]][0] = id_x[p[1]];
                sol[p[2]][1] = id_y[p[1]];
            }
        }
        return;
    }
    c = 0;
    int X = x + (sz + 1) / 2;
    int Y = y + sz / 2 + 1;
    vector<array<int, 3>> rig, up;
    priority_queue<pii, vector<pii>, greater<pii>> xc; // {x, comp}
    priority_queue<pii, vector<pii>, greater<pii>> yc; // {y, comp}

    int nw;
    int L;
    for(auto p : qr){
        if(p[0] >= 1){
            if(p[1] >= X){
                rig.push_back(p);
                away[p[0]] = 0;
                continue;
            }
            if(p[2] >= Y){
                up.push_back(p);
                away[p[0]] = 1;
                continue;
            }
            away[p[0]]=-1;
            id_x[p[0]] = c;
            val[c] = p[1];
            par[c] = c;
            lis[c].clear();
            lis[c].push_back(p[0]);
            xc.push(mp(val[c], c));
            c ++ ;
            id_y[p[0]] = c;
            val[c] = p[2];
            par[c] = c;
            lis[c].clear();
            lis[c].push_back(p[0]);
            yc.push(mp(val[c], c));
            c ++ ;
        }
        else if(p[0] == -1){
            if(away[p[1]] == 0) {
                rig.push_back(p);
            }
            else if(away[p[1]] == 1){
                up.push_back(p);
            }
            else if(get_val(id_x[p[1]]) >= X){
                rig.push_back(p);
            }
            else if(get_val(id_y[p[1]]) >= Y){
                up.push_back(p);
            }
            else{
                sol[p[2]][0] = get_val(id_x[p[1]]);
                sol[p[2]][1] = get_val(id_y[p[1]]);
            }
        }
        else if(p[0] == -2){
            L = p[1];
            if(L >= Y){
                int nw = c;
                lis[c].clear();
                par[c] = c;
                val[c] = n - L;
                c ++ ;
                while(!xc.empty() && xc.top().fi < n - L){
                    unite(xc.top().se, nw);
                    xc.pop();
                }
                xc.push(mp(val[nw], nw));
                up.push_back(p);
            }
            else{
                rig.push_back(p);
                while(!yc.empty() && yc.top().fi <= L){
                    for(auto v : lis[yc.top().se]){
                        if(away[v] != -1) continue;
                        rig.push_back({v, n - L, yc.top().fi});
                        away[v]=0;
                    }
                    yc.pop();
                }
            }
        }
        else{ // p[0] == -3
            L = p[1];
            if(L >= X){
                int nw = c;
                lis[c].clear();
                par[c] = c;
                val[c] = n - L;
                c ++ ;
                while(!yc.empty() && yc.top().fi < n - L){
                    unite(yc.top().se, nw);
                    yc.pop();
                }
                yc.push(mp(val[nw], nw));
                rig.push_back(p);
            }
            else{
                up.push_back(p);
                while(!xc.empty() && xc.top().fi <= L){
                    for(auto v : lis[xc.top().se]){
                        if(away[v] != -1) continue;
                        up.push_back({v, xc.top().fi, n - L});
                        away[v]=1;
                    }
                    xc.pop();
                }
            }
        }
    }
    solve(x, Y, up);
    solve(X, y, rig);
}

int main(){
    //fastIO;
    int m, q;
    cin >> n >> m >> q;
    vector<array<int, 3>> query;
    int x, y;
    int c = 1;
    for(int i = 1; i <= m; i ++ ){
        cin >> x >> y;
        query.push_back({c, x, y});
        c ++ ;
    }
    int typ;
    for(int iq = 1; iq <= q; iq ++ ){
        cin >> typ;
        sol[iq][0] = sol[iq][1] = -1;
        if(typ <= 3){
            cin >> x;
            query.push_back({-typ,x,iq});
        }
        else{
            cin >> x >> y;
            query.push_back({c, x, y});
            c ++ ;
        }
    }
    solve(0, 0, query);
    for(int iq = 1; iq <= q; iq ++ ){
        if(sol[iq][0] == -1) continue;
        cout << sol[iq][0] << " " << sol[iq][1] << "\n";
    }
    return 0;
}

Compilation message

sweeping.cpp: In function 'void solve(int, int, std::vector<std::array<int, 3> >)':
sweeping.cpp:74:9: warning: unused variable 'nw' [-Wunused-variable]
   74 |     int nw;
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 48204 KB Output is correct
2 Correct 30 ms 47908 KB Output is correct
3 Correct 30 ms 47948 KB Output is correct
4 Correct 31 ms 48288 KB Output is correct
5 Correct 28 ms 48304 KB Output is correct
6 Correct 24 ms 47828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2322 ms 194192 KB Output is correct
2 Correct 2280 ms 194332 KB Output is correct
3 Correct 2337 ms 194820 KB Output is correct
4 Correct 2053 ms 263308 KB Output is correct
5 Correct 2746 ms 235084 KB Output is correct
6 Correct 2557 ms 250376 KB Output is correct
7 Correct 2266 ms 193964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2273 ms 219740 KB Output is correct
2 Correct 2302 ms 228416 KB Output is correct
3 Correct 1979 ms 254708 KB Output is correct
4 Correct 2412 ms 350444 KB Output is correct
5 Correct 2594 ms 301796 KB Output is correct
6 Correct 2225 ms 225788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2273 ms 219740 KB Output is correct
2 Correct 2302 ms 228416 KB Output is correct
3 Correct 1979 ms 254708 KB Output is correct
4 Correct 2412 ms 350444 KB Output is correct
5 Correct 2594 ms 301796 KB Output is correct
6 Correct 2225 ms 225788 KB Output is correct
7 Correct 2492 ms 205520 KB Output is correct
8 Correct 2497 ms 209952 KB Output is correct
9 Correct 2477 ms 202388 KB Output is correct
10 Correct 2308 ms 249140 KB Output is correct
11 Correct 2559 ms 303768 KB Output is correct
12 Correct 2764 ms 273324 KB Output is correct
13 Correct 2772 ms 401428 KB Output is correct
14 Correct 266 ms 102380 KB Output is correct
15 Correct 839 ms 161740 KB Output is correct
16 Correct 2297 ms 209864 KB Output is correct
17 Correct 2278 ms 217580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 48204 KB Output is correct
2 Correct 30 ms 47908 KB Output is correct
3 Correct 30 ms 47948 KB Output is correct
4 Correct 31 ms 48288 KB Output is correct
5 Correct 28 ms 48304 KB Output is correct
6 Correct 24 ms 47828 KB Output is correct
7 Correct 2322 ms 194192 KB Output is correct
8 Correct 2280 ms 194332 KB Output is correct
9 Correct 2337 ms 194820 KB Output is correct
10 Correct 2053 ms 263308 KB Output is correct
11 Correct 2746 ms 235084 KB Output is correct
12 Correct 2557 ms 250376 KB Output is correct
13 Correct 2266 ms 193964 KB Output is correct
14 Correct 2273 ms 219740 KB Output is correct
15 Correct 2302 ms 228416 KB Output is correct
16 Correct 1979 ms 254708 KB Output is correct
17 Correct 2412 ms 350444 KB Output is correct
18 Correct 2594 ms 301796 KB Output is correct
19 Correct 2225 ms 225788 KB Output is correct
20 Correct 2492 ms 205520 KB Output is correct
21 Correct 2497 ms 209952 KB Output is correct
22 Correct 2477 ms 202388 KB Output is correct
23 Correct 2308 ms 249140 KB Output is correct
24 Correct 2559 ms 303768 KB Output is correct
25 Correct 2764 ms 273324 KB Output is correct
26 Correct 2772 ms 401428 KB Output is correct
27 Correct 266 ms 102380 KB Output is correct
28 Correct 839 ms 161740 KB Output is correct
29 Correct 2297 ms 209864 KB Output is correct
30 Correct 2278 ms 217580 KB Output is correct
31 Correct 2152 ms 239384 KB Output is correct
32 Correct 2319 ms 202312 KB Output is correct
33 Correct 2442 ms 180648 KB Output is correct
34 Correct 2476 ms 219832 KB Output is correct
35 Correct 2470 ms 210672 KB Output is correct
36 Correct 2371 ms 248928 KB Output is correct
37 Correct 3233 ms 407740 KB Output is correct
38 Correct 2867 ms 413536 KB Output is correct
39 Correct 2698 ms 358452 KB Output is correct
40 Correct 2272 ms 218900 KB Output is correct