# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
754617 |
2023-06-08T07:03:36 Z |
GrindMachine |
ICC (CEOI16_icc) |
C++17 |
|
139 ms |
632 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);
}
}
for(int bit = 0; ; ++bit){
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:134:13: note: in expansion of macro 'rep'
134 | rep(i,sz(roots)){
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
468 KB |
Ok! 105 queries used. |
2 |
Correct |
5 ms |
468 KB |
Ok! 100 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
500 KB |
Ok! 547 queries used. |
2 |
Correct |
52 ms |
496 KB |
Ok! 694 queries used. |
3 |
Correct |
43 ms |
500 KB |
Ok! 687 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
496 KB |
Ok! 1478 queries used. |
2 |
Correct |
114 ms |
484 KB |
Ok! 1710 queries used. |
3 |
Correct |
113 ms |
488 KB |
Ok! 1675 queries used. |
4 |
Correct |
123 ms |
496 KB |
Ok! 1565 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
488 KB |
Ok! 1548 queries used. |
2 |
Correct |
128 ms |
496 KB |
Ok! 1619 queries used. |
3 |
Correct |
117 ms |
492 KB |
Ok! 1710 queries used. |
4 |
Correct |
117 ms |
488 KB |
Ok! 1566 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
484 KB |
Ok! 1691 queries used. |
2 |
Correct |
115 ms |
508 KB |
Ok! 1709 queries used. |
3 |
Correct |
114 ms |
492 KB |
Ok! 1706 queries used. |
4 |
Correct |
119 ms |
632 KB |
Ok! 1682 queries used. |
5 |
Correct |
114 ms |
500 KB |
Ok! 1514 queries used. |
6 |
Correct |
139 ms |
508 KB |
Ok! 1577 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
118 ms |
500 KB |
Too many queries! 1709 out of 1625 |
2 |
Halted |
0 ms |
0 KB |
- |