Submission #754610

# Submission time Handle Problem Language Result Execution time Memory
754610 2023-06-08T06:57:43 Z GrindMachine ICC (CEOI16_icc) C++17
61 / 100
165 ms 620 KB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
https://ivaniscoding.wordpress.com/2018/08/24/divide-and-conquer-4-icc/

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

#include "icc.h"

struct DSU {
    vector<int> par, rankk, siz;

    DSU() {

    }

    DSU(int n) {
        init(n);
    }

    void init(int n) {
        par = vector<int>(n + 1);
        rankk = vector<int>(n + 1);
        siz = vector<int>(n + 1);
        rep(i, n + 1) create(i);
    }

    void create(int u) {
        par[u] = u;
        rankk[u] = 0;
        siz[u] = 1;
    }

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

    bool same(int u, int v) {
        return find(u) == find(v);
    }

    void merge(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;

        if (rankk[u] == rankk[v]) rankk[u]++;
        if (rankk[u] < rankk[v]) swap(u, v);

        par[v] = u;
        siz[u] += siz[v];
    }
};

int my_query(vector<int> a, vector<int> b){
    int s1 = sz(a), s2 = sz(b);
    int a2[s1], b2[s2];
    rep(i,s1) a2[i] = a[i];
    rep(i,s2) b2[i] = b[i];
    return query(s1,s2,a2,b2);
}

void run(int n){
    DSU dsu(n);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> dist(1,2);

    rep1(iter,n-1){
        vector<int> roots;
        vector<int> guys[n+5];

        rep1(i,n){
            int p = dsu.find(i);
            guys[p].pb(i);
            if(p == i){
                roots.pb(i);
            }
        }

        while(true){
            vector<int> v1, v2;
            
            trav(r,roots){
                int t = dist(rng);
                if(t == 1){
                    trav(u,guys[r]){
                        v1.pb(u);
                    }
                }
                else{
                    trav(u,guys[r]){
                        v2.pb(u);
                    }
                }
            }

            int res = my_query(v1,v2);
            if(!res) conts;

            // fix right, b.s left
            int l = 0, r = sz(v1) - 1;
            int u = -1;
            
            while(l <= r){
                int mid = (l+r) >> 1;
                vector<int> v1_new, v2_new;
                rep(i,mid+1) v1_new.pb(v1[i]);
                v2_new = v2;

                int ok = my_query(v1_new, v2_new);

                if(ok){
                    u = v1[mid];
                    r = mid - 1;
                }               
                else{
                    l = mid + 1;
                } 
            }

            // fix left, b.s right
            l = 0, r = sz(v2) - 1;
            int v = -1;

            while(l <= r){
                int mid = (l+r) >> 1;
                vector<int> v1_new, v2_new;
                v1_new = v1;
                rep(i,mid+1) v2_new.pb(v2[i]);

                int ok = my_query(v1_new, v2_new);

                if(ok){
                    v = v2[mid];
                    r = mid - 1;
                }               
                else{
                    l = mid + 1;
                } 
            }

            assert(u != -1);
            assert(v != -1);

            setRoad(u,v);
            dsu.merge(u,v);

            break;  
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 468 KB Ok! 117 queries used.
2 Correct 5 ms 468 KB Ok! 110 queries used.
# Verdict Execution time Memory Grader output
1 Correct 32 ms 488 KB Ok! 580 queries used.
2 Correct 44 ms 468 KB Ok! 879 queries used.
3 Correct 42 ms 488 KB Ok! 818 queries used.
# Verdict Execution time Memory Grader output
1 Correct 114 ms 468 KB Ok! 1509 queries used.
2 Correct 165 ms 496 KB Ok! 2172 queries used.
3 Correct 124 ms 620 KB Ok! 1751 queries used.
4 Correct 125 ms 492 KB Ok! 1739 queries used.
# Verdict Execution time Memory Grader output
1 Correct 132 ms 496 KB Ok! 1701 queries used.
2 Correct 127 ms 500 KB Ok! 1768 queries used.
3 Correct 142 ms 508 KB Ok! 1908 queries used.
4 Correct 116 ms 508 KB Ok! 1615 queries used.
# Verdict Execution time Memory Grader output
1 Incorrect 145 ms 468 KB Too many queries! 1912 out of 1775
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 144 ms 508 KB Too many queries! 1925 out of 1625
2 Halted 0 ms 0 KB -