답안 #83170

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
83170 2018-11-05T19:28:49 Z fredbr Pictionary (COCI18_pictionary) C++17
0 / 140
182 ms 25144 KB
#include <bits/stdc++.h>

using namespace std;

using ii = pair<int, int>;


template <int maxn>
struct Dsu {
    int n;
    int pai[maxn], w[maxn];

    int find(int x)
    {
        if (x == pai[x]) return x;
        return pai[x] = find(pai[x]);
    }

    bool find(int a, int b)
    {
        a = find(a), b= find(b);
        return a == b;
    }
    void join(int a, int b)
    {
        a = find(a), b = find(b);
    
        if (w[a] < w[b]) swap(a, b);

        pai[b] = a;
        w[a] += w[b];
    }

    void init(int n_) {
        n  = n_;
        fill(w+1, w+n+1, 1);
        iota(pai+1, pai+n+1, 1);
    }
};

struct Query
{
    int ff, ss, id;
};

int const maxn = 101010;

Dsu<maxn> dsu;
vector<Query> qr[maxn];
vector<Query> nqr[maxn];
int res[maxn];

int main()
{
    ios::sync_with_stdio(false), cin.tie(0);

    int n, m;
    int q;
    cin >> n >> m >> q;

    dsu.init(n);
    queue<ii> qe, nq;
    qe.push({1, m+1});

    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;

        qr[1].push_back({a, b, i});
    }
    int lk = m+1;
    while (!qe.empty()) {

        int l = qe.front().first, r = qe.front().second;
        int k = (l+r)/2;
        qe.pop();

        for (; lk > k;) {
            lk--;
            for (int j = lk+lk; j <= n; j += lk) {
                if (dsu.find(lk) != dsu.find(j)) {
                    dsu.join(lk, j);
                }
            }
        }

        // cout << l << " " << qr[l].size() << "\n";
        if (l+1 < r) {
            bool isl = false, isr = false;
            for (auto p : qr[l]) {
                if (!dsu.find(p.ff, p.ss)) {
                    if (!isl) isl = true, nq.push({l, k});
                    nqr[l].push_back(p);
                }
                else {
                    if (!isr) isr = true, nq.push({k, r});
                    nqr[k].push_back(p);
                }
            }
            qr[l].clear();
        }
        else {
            for (auto p : qr[l]) {
                res[p.id] = l -1 + int(dsu.find(p.ff, p.ss));
            } 
        }

        if (qe.empty()) {
            swap(qe, nq), swap(qr, nqr), lk = m+1, dsu.init(n);
        }
    }

    for (int i = 0; i < q; i++) cout << m-res[i]+1 << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 5624 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 18 ms 6648 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 42 ms 10916 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 55 ms 13800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 60 ms 13800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 60 ms 14000 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 114 ms 16968 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 64 ms 16968 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 131 ms 22080 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 182 ms 25144 KB Output isn't correct
2 Halted 0 ms 0 KB -