답안 #218752

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
218752 2020-04-02T17:07:28 Z Vimmer Matching (COCI20_matching) C++14
58 / 110
2199 ms 524288 KB
#include <bits/stdc++.h>

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 100001

using namespace std;

int x[N], y[N], a[N][2], n, xr[N], yr[N];

set <int> tx[N * 4], ty[N * 4], alone;

set <pair <int, int> > tx_del[N * 4], ty_del[N * 4];

vector <pair <int, char> > psh[N * 4];

vector <pair <pair <int, int>, char>  > psh_del[N * 4];

bool mk[N], mkr[N][2];

vector <pair <int, int> > g, gr;

void Push(int v, int tl, int tr)
{
    while (sz(psh[v]) > 0)
    {
        pair <int, char> val = psh[v].back();

        psh[v].pop_back();

        if (val.S == 'a') tx[v].insert(val.F); else ty[v].insert(val.F);

        if (tl != tr) {psh[v + v].pb(val); psh[v + v + 1].pb(val);}
    }

}
void upd(int v, int tl, int tr, int l, int r, int val, bool f)
{
    Push(v, tl, tr);

    if (tl > tr || l > r || r < tl || tr < l) return;

    if (l <= tl && tr <= r) {if (!f) psh[v].pb({val, 'a'}); else psh[v].pb({val, 'b'}); Push(v, tl, tr); return;}

    int md = (tl + tr) >> 1;

    upd(v + v, tl, md, l, r, val, f);

    upd(v + v + 1, md + 1, tr, l, r, val, f);
}

void Push_del(int v, int tl, int tr)
{
    while (sz(psh_del[v]) > 0)
    {
        pair <pair <int, int>, char>  pt = psh_del[v].back();

        psh_del[v].pop_back();

        if (pt.S == 'a') tx_del[v].insert(pt.F); else if (pt.S == 'b') ty_del[v].insert(pt.F);
          else if (pt.S == 'c') tx_del[v].erase(pt.F); else ty_del[v].erase(pt.F);

        if (tl != tr) {psh_del[v + v].pb(pt); psh_del[v + v + 1].pb(pt); }
    }
}

void upd_del(int v, int tl, int tr, int l, int r, pair <int, int> val, bool f)
{
    if (tl > tr || l > r || r < tl || tr < l) return;

    if (l <= tl && tr <= r) {if (!f) psh_del[v].pb({val, 'a'}); else psh_del[v].pb({val, 'b'}); return;}

    int md = (tl + tr) >> 1;

    upd_del(v + v, tl, md, l, r, val, f);

    upd_del(v + v + 1, md + 1, tr, l, r, val, f);
}

void upd_remove(int v, int tl, int tr, int l, int r, pair <int, int> val, bool f)
{
    if (tl > tr || l > r || r < tl || tr < l) return;

    if (l <= tl && tr <= r) {if (!f) psh_del[v].pb({val, 'c'}); else psh_del[v].pb({val, 'd'}); return;}

    int md = (tl + tr) >> 1;

    upd_remove(v + v, tl, md, l, r, val, f);

    upd_remove(v + v + 1, md + 1, tr, l, r, val, f);
}

bool good(int v, int tl, int tr, int pos, int l, int r, bool f)
{
    Push(v, tl, tr);

    if (tl == tr)
    {
        set <int> :: iterator it;

        if (f)
        {
            it = ty[v].lower_bound(l);

            return (it == ty[v].end() ? 1 : *it > r);
        }
        else
        {
            it = tx[v].lower_bound(l);

            return (it == tx[v].end() ? 1 : *it > r);
        }
    }
    else
    {
        int md = (tl + tr) >> 1;

        if (pos <= md) return good(v + v, tl, md, pos, l, r, f);

        return good(v + v + 1, md + 1, tr, pos, l, r, f);
    }
}

int good_del(int v, int tl, int tr, int pos, int l, int r, bool f)
{
    Push_del(v, tl, tr);

    if (tl == tr)
    {
        set <pair <int, int> > :: iterator it;

        if (f)
        {
            it = ty_del[v].lower_bound({l, -1e9});

            if (it == ty_del[v].end() || (*it).F > r) return -1;

            return (*it).S;
        }
        else
        {
            it = tx_del[v].lower_bound({l, -1e9});

            if (it == tx_del[v].end() || (*it).F > r) return -1;

            return (*it).S;
        }
    }
    else
    {
        int md = (tl + tr) >> 1;

        if (pos <= md) return good_del(v + v, tl, md, pos, l, r, f);

        return good_del(v + v + 1, md + 1, tr, pos, l, r, f);
    }
}

bool gd(int fr, int sc)
{
    if (x[fr] == x[sc])
    {
        if (y[fr] > y[sc]) swap(fr, sc);

        return good(1, 1, N - 1, x[fr], y[fr], y[sc], 0);
    }
    else
    {
        if (x[fr] > x[sc]) swap(fr, sc);

        return good(1, 1, N - 1, y[fr], x[fr], x[sc], 1);
    }
}

void add_remove(int fr, int sc, int nm)
{
    if (!mk[fr]) alone.insert(fr);

    if (!mk[sc]) alone.insert(sc);

    if (x[fr] == x[sc])
    {
        if (y[fr] > y[sc]) swap(fr, sc);

        upd_remove(1, 1, N, y[fr], y[sc], {x[fr], nm }, 1);
    }
    else
    {
        if (x[fr] > x[sc]) swap(fr, sc);

        upd_remove(1, 1, N, x[fr], x[sc], {y[fr], nm }, 0);
    }
}

void seacrh(int fr, int sc)
{
    if (x[fr] == x[sc])
    {
        if (y[fr] > y[sc]) swap(fr, sc);

        int pt = good_del(1, 1, N, x[fr], y[fr], y[sc], 0);

        while (pt != -1)
        {
            add_remove(gr[pt].F, gr[pt].S, pt);

            pt = good_del(1, 1, N, x[fr], y[fr], y[sc], 0);
        }
    }
    else
    {
        if (x[fr] > x[sc]) swap(fr, sc);

        int pt = good_del(1, 1, N, y[fr], x[fr], x[sc], 1);

        while (pt != -1)
        {
            add_remove(gr[pt].F, gr[pt].S, pt);

            pt = good_del(1, 1, N, y[fr], x[fr], x[sc], 1);
        }
    }
}

void add(int fr, int sc)
{
    if (x[fr] == x[sc])
    {
        g.pb({fr, sc});

        if (y[fr] > y[sc]) swap(fr, sc);

        upd(1, 1, N - 1, y[fr], y[sc], x[fr], 1);

        seacrh(fr, sc);
    }
    else
    {
        g.pb({fr, sc});

        if (x[fr] > x[sc]) swap(fr, sc);

        upd(1, 1, N - 1, x[fr], x[sc], y[fr], 0);

        seacrh(fr, sc);
    }
}

void clr(int x)
{
    mk[x] = 1;

    if (a[x][1] != -1) {if (!mk[a[x][1]])  alone.insert(a[x][1]); a[x][1] = a[a[x][1]][1] = -1;}

    if (a[x][0] != -1) {if (!mk[a[x][0]])  alone.insert(a[x][0]); a[x][0] = a[a[x][0]][0] = -1;}
}


void fnd()
{
    while (sz(alone) > 0)
    {
        int v = *alone.begin();

        alone.erase(alone.begin());

        if (mk[v]) continue;

        if (a[v][1] != -1 && gd(v, a[v][1])) {add(v, a[v][1]); clr(a[v][1]); clr(v); continue;}

        if (a[v][0] != -1 && gd(v, a[v][0])) {add(v, a[v][0]); clr(a[v][0]); clr(v); continue;}

        cout << "NE" << endl;

        exit(0);
    }

    for (int i = 0; i < n; i++)
    {
        if (mk[i]) continue;

        g.pb({i, a[i][0]});

        clr(a[i][0]);

        clr(i);
    }
}

void add_del(int fr, int sc)
{
    if (x[fr] == x[sc])
    {
        if (mkr[fr][0]) return;

        mkr[fr][0] = 1;

        mkr[sc][0] = 1;

        if (y[fr] > y[sc]) swap(fr, sc);

        upd_del(1, 1, N, y[fr], y[sc], {x[fr], sz(gr) }, 1);

        gr.pb({fr, sc});
    }
    else
    {
        if (mkr[fr][1]) return;

        mkr[fr][1] = 1;

        mkr[sc][1] = 1;

        if (x[fr] > x[sc]) swap(fr, sc);

        upd_del(1, 1, N, x[fr], x[sc], {y[fr], sz(gr) }, 0);

        gr.pb({fr, sc});
    }
}
int main()
{
    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n;

    for (int i = 1; i < N; i++) xr[i] = yr[i] = -1;

    for (int i = 0; i < n; i++) {cin >> x[i] >> y[i]; a[i][0] = a[i][1] = -1;}

    for (int i = 0; i < n; i++)
        {
            int X = x[i], Y = y[i];

            if (xr[X] == -1) xr[X] = i; else {a[i][0] = xr[X]; a[xr[X]][0] = i;}

            if (yr[Y] == -1) yr[Y] = i; else {a[i][1] = yr[Y]; a[yr[Y]][1] = i;}
        }

    for (int i = 0; i < n; i++)
    {
        if (a[i][0] == a[i][1] && a[i][1] == -1) {cout << "NE" << endl; exit(0);}

        if (a[i][0] == -1 || a[i][1] == -1) alone.insert(i); else {add_del(i, a[i][0]); add_del(i, a[i][1]);}
    }

    fnd();

    if (sz(g) != n / 2) {cout << "NE" << endl; exit(0);}

    cout << "DA" << endl;

    for (auto it : g) cout << it.F + 1 << " " << it.S + 1 << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 95104 KB Output is correct
2 Correct 60 ms 95096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 95104 KB Output is correct
2 Correct 60 ms 95096 KB Output is correct
3 Correct 59 ms 95096 KB Output is correct
4 Correct 60 ms 95096 KB Output is correct
5 Correct 60 ms 95096 KB Output is correct
6 Correct 63 ms 95352 KB Output is correct
7 Correct 59 ms 95096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 95104 KB Output is correct
2 Correct 60 ms 95096 KB Output is correct
3 Correct 59 ms 95096 KB Output is correct
4 Correct 60 ms 95096 KB Output is correct
5 Correct 60 ms 95096 KB Output is correct
6 Correct 63 ms 95352 KB Output is correct
7 Correct 59 ms 95096 KB Output is correct
8 Correct 60 ms 95260 KB Output is correct
9 Correct 59 ms 95096 KB Output is correct
10 Correct 59 ms 95096 KB Output is correct
11 Correct 62 ms 95100 KB Output is correct
12 Correct 60 ms 95096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 95104 KB Output is correct
2 Correct 60 ms 95096 KB Output is correct
3 Correct 59 ms 95096 KB Output is correct
4 Correct 60 ms 95096 KB Output is correct
5 Correct 60 ms 95096 KB Output is correct
6 Correct 63 ms 95352 KB Output is correct
7 Correct 59 ms 95096 KB Output is correct
8 Correct 60 ms 95260 KB Output is correct
9 Correct 59 ms 95096 KB Output is correct
10 Correct 59 ms 95096 KB Output is correct
11 Correct 62 ms 95100 KB Output is correct
12 Correct 60 ms 95096 KB Output is correct
13 Correct 68 ms 96376 KB Output is correct
14 Correct 72 ms 97016 KB Output is correct
15 Correct 72 ms 97016 KB Output is correct
16 Correct 72 ms 97276 KB Output is correct
17 Correct 76 ms 97912 KB Output is correct
18 Correct 67 ms 96248 KB Output is correct
19 Correct 69 ms 96760 KB Output is correct
20 Correct 67 ms 95992 KB Output is correct
21 Correct 67 ms 95608 KB Output is correct
22 Correct 64 ms 95480 KB Output is correct
23 Correct 215 ms 138616 KB Output is correct
24 Correct 206 ms 136696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 95104 KB Output is correct
2 Correct 60 ms 95096 KB Output is correct
3 Correct 59 ms 95096 KB Output is correct
4 Correct 60 ms 95096 KB Output is correct
5 Correct 60 ms 95096 KB Output is correct
6 Correct 63 ms 95352 KB Output is correct
7 Correct 59 ms 95096 KB Output is correct
8 Correct 60 ms 95260 KB Output is correct
9 Correct 59 ms 95096 KB Output is correct
10 Correct 59 ms 95096 KB Output is correct
11 Correct 62 ms 95100 KB Output is correct
12 Correct 60 ms 95096 KB Output is correct
13 Correct 68 ms 96376 KB Output is correct
14 Correct 72 ms 97016 KB Output is correct
15 Correct 72 ms 97016 KB Output is correct
16 Correct 72 ms 97276 KB Output is correct
17 Correct 76 ms 97912 KB Output is correct
18 Correct 67 ms 96248 KB Output is correct
19 Correct 69 ms 96760 KB Output is correct
20 Correct 67 ms 95992 KB Output is correct
21 Correct 67 ms 95608 KB Output is correct
22 Correct 64 ms 95480 KB Output is correct
23 Correct 215 ms 138616 KB Output is correct
24 Correct 206 ms 136696 KB Output is correct
25 Correct 1525 ms 353776 KB Output is correct
26 Correct 2199 ms 501096 KB Output is correct
27 Correct 1251 ms 306024 KB Output is correct
28 Correct 1484 ms 356588 KB Output is correct
29 Correct 894 ms 252648 KB Output is correct
30 Correct 894 ms 253416 KB Output is correct
31 Correct 1614 ms 398824 KB Output is correct
32 Correct 1271 ms 327648 KB Output is correct
33 Correct 326 ms 135656 KB Output is correct
34 Correct 343 ms 142312 KB Output is correct
35 Correct 151 ms 104948 KB Output is correct
36 Correct 362 ms 124660 KB Output is correct
37 Runtime error 1623 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
38 Halted 0 ms 0 KB -