# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
935672 | Matthewwww | Aliens (IOI07_aliens) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define f first
#define s second
#include "aliens.h"
using namespace std;
#define int long long
int clz(int a){
return a ? __builtin_clzll(a) : 63;
}
int n;
bool query (int a, int b){
return a > 0 && b > 0 && a <= n && b <= n && use_device(a, b);
}
pair<signed, signed> help_aliens(signed aoehtn, signed a, signed b){
n = aoehtn;
int lax = a, rax = a, lby = b, rby = b;
for (int i = 1;1; i *= 2){
if (query(a-i, b))
lax = a-i;
else break;
}
int val = a-lax;
for (int i = 1ll<<63-clz(val); i; i >>= 1)
if (query(lax-i, b))
lax -= i;
for (int i = 1;1; i *= 2){
if (query(a+i, b))
rax = a+i;
else break;
}
val = rax-a;
for (int i = 1ll<<63-clz(val); i; i >>= 1)
if (query(rax+i, b))
rax += i;
for (int i = 1;1; i *= 2){
if (query(a, b-i))
lby = b-i;
else break;
}
val = b-lby;
for (int i = 1ll<<63-clz(val); i; i >>= 1)
if (query(a, lby-i))
lby -= i;
for (int i = 1;1; i *= 2){
if (query(a, b+i))
rby = b+i;
else break;
}
val = rby-b;
for (int i = 1ll<<63-clz(val); i; i >>= 1)
if (query(a, rby+i))
rby += i;
assert(rax-lax == rby-lby);
int len = rax-lax+1;
pair<int, int> cenc = {lax+len/2, lby+len/2};
pair<int, int> ans = cenc;
for (int i = 1; i < 391; i++){
if (query(cenc.f+i*len, cenc.s+i%2*len) || query(cenc.f+i*len, cenc.s-i%2*len))
ans.f = cenc.f+i*len;
else
break;
}
for (int i = 1; i < 391; i++){
if (query(cenc.f+i%2*len, cenc.s+i*len) || query(cenc.f-i%2*len, cenc.s+i*len))
ans.s = cenc.s+i*len;
else
break;
}
return {ans.f-2*len, ans.s-2*len};
}
#undef int