Submission #83151

# Submission time Handle Problem Language Result Execution time Memory
83151 2018-11-05T19:00:36 Z fredbr Pictionary (COCI18_pictionary) C++17
0 / 140
153 ms 23452 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+1)/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;
            } 
        }

        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";
}
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 5496 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 6520 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 40 ms 10052 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 55 ms 12324 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 48 ms 12324 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 58 ms 13000 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 82 ms 15872 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 59 ms 15872 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 120 ms 20464 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 153 ms 23452 KB Output isn't correct
2 Halted 0 ms 0 KB -