Submission #217039

# Submission time Handle Problem Language Result Execution time Memory
217039 2020-03-28T18:17:58 Z combi1k1 Sweeping (JOI20_sweeping) C++14
100 / 100
7908 ms 353044 KB
#include<bits/stdc++.h>

using namespace std;

#define ll  long long
#define ld  double

#define sz(x)   (int)x.size()
#define all(x)  x.begin(),x.end()

#define pb  emplace_back
#define X   first
#define Y   second

const int   N   = 1500005;

typedef pair<int,int>   ii;
typedef vector<int>     vi;

struct DSU  {
    vector<int> val;
    vector<vi>  vec;

    int par[N];

    void init() {
        val.clear();
        vec.clear();
    }
    int add(int x,int v)    {
        int idx = sz(val);

        val.push_back(x);
        vec.push_back({});

        if (v >= 0) {
            par[v] = idx;
            vec.back().pb(v);
        }
        return  idx;
    }
    int join(int x,int y)   {
        if (sz(vec[x]) < sz(vec[y]))
            swap(x,y);

        val[x] = max(val[x],val[y]);

        for(int u : vec[y])
            par[u] = x,
            vec[x].pb(u);

        return  x;
    }
}   fX, fY;

typedef array<int,3>    ar;

ii  ans[N];
ii  pos[N];
int n, m, q;

bool done[N];

void getReal(int i) {
    pos[i].X = fX.val[fX.par[i]];
    pos[i].Y = fY.val[fY.par[i]];
}

void calc(int x,int y,vector<ar> vec)   {
    if (vec.empty())
        return;

    int dif = n - x - y;

    if (dif == 0)   {
        for(ar  a : vec)
            if (a[0] == 1)
                ans[a[2]].X = x,
                ans[a[2]].Y = y;

        return;
    }
    fX.init();
    fY.init();

    int X = x + (dif + 1) / 2;
    int Y = y + (dif + 2) / 2;

    vector<ar>  vecU;
    vector<ar>  vecR;

    multiset<ii>    Sx;
    multiset<ii>    Sy;

    for(ar  a : vec)    {
        if (a[0] == 1)  {
            int i = a[1];
            if (pos[i].Y >= Y)  {   vecU.pb(a); continue;   }
            if (pos[i].X >= X)  {   vecR.pb(a); continue;   }

            ans[a[2]].X = fX.val[fX.par[i]];
            ans[a[2]].Y = fY.val[fY.par[i]];
        }
        if (a[0] == 2)  {
            if (a[1] >= Y)  {
                int nxt = n - a[1];
                int idx = fX.add(nxt,-1);

                while (Sx.size() && (*Sx.begin()).X <= nxt) {
                    int x = (*Sx.begin()).Y;
                    Sx.erase( Sx.begin());

                    idx = fX.join(idx,x);
                }
                Sx.insert(ii(fX.val[idx],idx));
                vecU.pb(a);
            }
            else    {
                while (Sy.size() && (*Sy.begin()).X <= a[1])    {
                    int x = (*Sy.begin()).Y;
                    Sy.erase( Sy.begin());

                    for(int u : fY.vec[x])
                        if(!done[u])    {
                            done[u] = 1;
                            getReal(u);
                            pos[u].X = n - a[1];
                            vecR.push_back({4,u,0});
                        }
                }
                vecR.pb(a);
            }
        }
        if (a[0] == 3)  {
            if (a[1] >= X)  {
                int nxt = n - a[1];
                int idx = fY.add(nxt,-1);

                while (Sy.size() && (*Sy.begin()).X <= nxt) {
                    int x = (*Sy.begin()).Y;
                    Sy.erase( Sy.begin());

                    idx = fY.join(idx,x);
                }
                Sy.insert(ii(fY.val[idx],idx));
                vecR.pb(a);
            }
            else    {
                while (Sx.size() && (*Sx.begin()).X <= a[1])    {
                    int x = (*Sx.begin()).Y;
                    Sx.erase( Sx.begin());

                    for(int u : fX.vec[x])
                        if(!done[u])    {
                            done[u] = 1;
                            getReal(u);
                            pos[u].Y = n - a[1];
                            vecU.push_back({4,u,0});
                        }
                }
                vecU.pb(a);
            }
        }
        if (a[0] == 4)  {
            int i = a[1];
            done[i] = 0;

            if (pos[i].Y >= Y)  {   done[i] = 1; vecU.pb(a); continue;  }
            if (pos[i].X >= X)  {   done[i] = 1; vecR.pb(a); continue;  }

            int xv = fX.add(pos[i].X,i);
            int yv = fY.add(pos[i].Y,i);

            Sx.insert(ii(fX.val[xv],xv));
            Sy.insert(ii(fY.val[yv],yv));
        }
    }
    calc(x,Y,vecU);
    calc(X,y,vecR);
}

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n >> m >> q;

    vector<ar>  Que;

    int tot = 0;

    for(int i = 0 ; i < m ; ++i)    {
        int x;  cin >> x;
        int y;  cin >> y;

        pos[++tot] = ii(x,y);
        Que.push_back({4,tot,0});
    }
    for(int i = 0 ; i < q ; ++i)    {
        ans[i] = ii(-1,-1);
        int t;  cin >> t;

        if (t < 4)  {
            int x;  cin >> x;
            Que.push_back({t,x,i});
        }
        else    {
            int x;  cin >> x;
            int y;  cin >> y;

            pos[++tot] = ii(x,y);
            Que.push_back({4,tot,i});
        }
    }
    calc(0,0,Que);

    for(int i = 0 ; i < q ; ++i)
        if (ans[i].X >= 0)
            cout << ans[i].X << " ",
            cout << ans[i].Y << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 1280 KB Output is correct
2 Correct 16 ms 896 KB Output is correct
3 Correct 10 ms 1280 KB Output is correct
4 Correct 19 ms 1280 KB Output is correct
5 Correct 16 ms 1408 KB Output is correct
6 Correct 7 ms 1280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3783 ms 121136 KB Output is correct
2 Correct 3651 ms 136020 KB Output is correct
3 Correct 3657 ms 136620 KB Output is correct
4 Correct 4530 ms 232824 KB Output is correct
5 Correct 7190 ms 193976 KB Output is correct
6 Correct 5385 ms 191188 KB Output is correct
7 Correct 3652 ms 132568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4229 ms 142548 KB Output is correct
2 Correct 4423 ms 146136 KB Output is correct
3 Correct 3757 ms 193172 KB Output is correct
4 Correct 4685 ms 293672 KB Output is correct
5 Correct 4583 ms 244784 KB Output is correct
6 Correct 3860 ms 171116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4229 ms 142548 KB Output is correct
2 Correct 4423 ms 146136 KB Output is correct
3 Correct 3757 ms 193172 KB Output is correct
4 Correct 4685 ms 293672 KB Output is correct
5 Correct 4583 ms 244784 KB Output is correct
6 Correct 3860 ms 171116 KB Output is correct
7 Correct 3944 ms 156432 KB Output is correct
8 Correct 3884 ms 161068 KB Output is correct
9 Correct 3887 ms 152272 KB Output is correct
10 Correct 4740 ms 226324 KB Output is correct
11 Correct 5015 ms 276816 KB Output is correct
12 Correct 6201 ms 207980 KB Output is correct
13 Correct 6031 ms 328028 KB Output is correct
14 Correct 164 ms 55496 KB Output is correct
15 Correct 1668 ms 141832 KB Output is correct
16 Correct 3770 ms 161612 KB Output is correct
17 Correct 3748 ms 162732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 1280 KB Output is correct
2 Correct 16 ms 896 KB Output is correct
3 Correct 10 ms 1280 KB Output is correct
4 Correct 19 ms 1280 KB Output is correct
5 Correct 16 ms 1408 KB Output is correct
6 Correct 7 ms 1280 KB Output is correct
7 Correct 3783 ms 121136 KB Output is correct
8 Correct 3651 ms 136020 KB Output is correct
9 Correct 3657 ms 136620 KB Output is correct
10 Correct 4530 ms 232824 KB Output is correct
11 Correct 7190 ms 193976 KB Output is correct
12 Correct 5385 ms 191188 KB Output is correct
13 Correct 3652 ms 132568 KB Output is correct
14 Correct 4229 ms 142548 KB Output is correct
15 Correct 4423 ms 146136 KB Output is correct
16 Correct 3757 ms 193172 KB Output is correct
17 Correct 4685 ms 293672 KB Output is correct
18 Correct 4583 ms 244784 KB Output is correct
19 Correct 3860 ms 171116 KB Output is correct
20 Correct 3944 ms 156432 KB Output is correct
21 Correct 3884 ms 161068 KB Output is correct
22 Correct 3887 ms 152272 KB Output is correct
23 Correct 4740 ms 226324 KB Output is correct
24 Correct 5015 ms 276816 KB Output is correct
25 Correct 6201 ms 207980 KB Output is correct
26 Correct 6031 ms 328028 KB Output is correct
27 Correct 164 ms 55496 KB Output is correct
28 Correct 1668 ms 141832 KB Output is correct
29 Correct 3770 ms 161612 KB Output is correct
30 Correct 3748 ms 162732 KB Output is correct
31 Correct 3896 ms 182500 KB Output is correct
32 Correct 4045 ms 144244 KB Output is correct
33 Correct 3717 ms 131588 KB Output is correct
34 Correct 4510 ms 155084 KB Output is correct
35 Correct 4319 ms 143372 KB Output is correct
36 Correct 5102 ms 224752 KB Output is correct
37 Correct 7908 ms 318152 KB Output is correct
38 Correct 6499 ms 353044 KB Output is correct
39 Correct 5486 ms 286944 KB Output is correct
40 Correct 3790 ms 165916 KB Output is correct