Submission #754621

# Submission time Handle Problem Language Result Execution time Memory
754621 2023-06-08T07:09:37 Z GrindMachine ICC (CEOI16_icc) C++17
100 / 100
116 ms 628 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);

    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);
            }
        }

        int root_cnt = sz(roots);
        vector<int> bit_order;

        for(int bit = 0; ; ++bit){
            if((1 << bit) >= root_cnt) break;
            bit_order.pb(bit);
        }

        mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
        shuffle(all(bit_order), rng);

        rep(ind, sz(bit_order)){
            int bit = bit_order[ind];
            vector<int> v1, v2;

            rep(i,sz(roots)){
                int r = roots[i];
                if(i & (1 << bit)){
                    trav(u,guys[r]){
                        v1.pb(u);
                    }
                }
                else{
                    trav(u,guys[r]){
                        v2.pb(u);
                    }
                }
            }

            int res = 0;

            if(ind != sz(bit_order) - 1){
                res = my_query(v1,v2);
            }
            else{
                res = 1;
            }

            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;
        }
    }
}

Compilation message

icc.cpp: In function 'void run(int)':
icc.cpp:29:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 | #define rep(i, n) for(int i = 0; i < n; ++i)
      |                                    ^
icc.cpp:143:9: note: in expansion of macro 'rep'
  143 |         rep(ind, sz(bit_order)){
      |         ^~~
icc.cpp:29:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 | #define rep(i, n) for(int i = 0; i < n; ++i)
      |                                    ^
icc.cpp:147:13: note: in expansion of macro 'rep'
  147 |             rep(i,sz(roots)){
      |             ^~~
icc.cpp:163:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  163 |             if(ind != sz(bit_order) - 1){
      |                ~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 500 KB Ok! 107 queries used.
2 Correct 5 ms 468 KB Ok! 105 queries used.
# Verdict Execution time Memory Grader output
1 Correct 35 ms 500 KB Ok! 545 queries used.
2 Correct 34 ms 500 KB Ok! 649 queries used.
3 Correct 33 ms 468 KB Ok! 647 queries used.
# Verdict Execution time Memory Grader output
1 Correct 102 ms 516 KB Ok! 1448 queries used.
2 Correct 116 ms 492 KB Ok! 1606 queries used.
3 Correct 111 ms 504 KB Ok! 1581 queries used.
4 Correct 105 ms 504 KB Ok! 1542 queries used.
# Verdict Execution time Memory Grader output
1 Correct 108 ms 520 KB Ok! 1528 queries used.
2 Correct 107 ms 496 KB Ok! 1532 queries used.
3 Correct 109 ms 492 KB Ok! 1599 queries used.
4 Correct 105 ms 596 KB Ok! 1512 queries used.
# Verdict Execution time Memory Grader output
1 Correct 111 ms 628 KB Ok! 1578 queries used.
2 Correct 110 ms 504 KB Ok! 1600 queries used.
3 Correct 112 ms 404 KB Ok! 1590 queries used.
4 Correct 108 ms 496 KB Ok! 1597 queries used.
5 Correct 103 ms 500 KB Ok! 1517 queries used.
6 Correct 113 ms 508 KB Ok! 1590 queries used.
# Verdict Execution time Memory Grader output
1 Correct 111 ms 488 KB Ok! 1599 queries used.
2 Correct 115 ms 504 KB Ok! 1602 queries used.