Submission #754618

# Submission time Handle Problem Language Result Execution time Memory
754618 2023-06-08T07:06:27 Z GrindMachine ICC (CEOI16_icc) C++17
90 / 100
135 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());

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

        shuffle(all(bit_order), rng);

        trav(bit,bit_order){
            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 = 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;
        }
    }
}

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:145:13: note: in expansion of macro 'rep'
  145 |             rep(i,sz(roots)){
      |             ^~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 468 KB Ok! 108 queries used.
2 Correct 5 ms 468 KB Ok! 107 queries used.
# Verdict Execution time Memory Grader output
1 Correct 28 ms 468 KB Ok! 555 queries used.
2 Correct 34 ms 468 KB Ok! 692 queries used.
3 Correct 35 ms 496 KB Ok! 686 queries used.
# Verdict Execution time Memory Grader output
1 Correct 106 ms 504 KB Ok! 1481 queries used.
2 Correct 123 ms 508 KB Ok! 1711 queries used.
3 Correct 116 ms 496 KB Ok! 1645 queries used.
4 Correct 120 ms 496 KB Ok! 1591 queries used.
# Verdict Execution time Memory Grader output
1 Correct 113 ms 492 KB Ok! 1581 queries used.
2 Correct 126 ms 588 KB Ok! 1579 queries used.
3 Correct 135 ms 468 KB Ok! 1653 queries used.
4 Correct 115 ms 504 KB Ok! 1517 queries used.
# Verdict Execution time Memory Grader output
1 Correct 128 ms 620 KB Ok! 1646 queries used.
2 Correct 118 ms 508 KB Ok! 1659 queries used.
3 Correct 123 ms 500 KB Ok! 1687 queries used.
4 Correct 121 ms 496 KB Ok! 1656 queries used.
5 Correct 108 ms 596 KB Ok! 1522 queries used.
6 Correct 129 ms 496 KB Ok! 1592 queries used.
# Verdict Execution time Memory Grader output
1 Incorrect 124 ms 492 KB Too many queries! 1672 out of 1625
2 Halted 0 ms 0 KB -