├── Bear and House Queries(algorithm).txt └── Bear and House Queries.cpp /Bear and House Queries(algorithm).txt: -------------------------------------------------------------------------------- 1 | The algorithm: 2 | 3 | 1. The code starts with some preprocessor directives and macro definitions, including data types, constants, and shorthand notations. 4 | 5 | 2. The main code starts with the `solve()` function. It initializes three variables `th`, `sw`, and `tw` with initial values of -1. 6 | 7 | 3. It defines three conditions: `cond1`, `cond2`, and `cond3`. These conditions are used to perform binary searches in different directions by making queries to determine whether certain conditions are satisfied. 8 | 9 | 4. The binary search is performed using the variables `l` and `r`, which represent the left and right boundaries of the search range. The `cond1`, `cond2`, and `cond3` conditions are used to update the boundaries and narrow down the search range until the desired condition is met. 10 | 11 | 5. After the binary searches, the values of `th`, `sw`, and `tw` are determined. 12 | 13 | 6. Finally, the `give()` function is called to output the result. 14 | 15 | 7. The `main()` function initializes the necessary variables and calls the `solve()` function. 16 | 17 | -------------------------------------------------------------------------------- /Bear and House Queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define ll long long 3 | #define ull unsigned long long 4 | #define pb push_back 5 | #define eb emplace_back 6 | #define pyes cout << "YES" << '\n' 7 | #define pno cout << "NO" << '\n' 8 | #define ff first 9 | #define ss second 10 | #define int long long 11 | #define forn(i, n) for (int i = 0; i < (int)n; i++) 12 | const int MAXN = 2e5 + 20; 13 | const int LMAXN = 9223372036854775807; // the max value of long long int 14 | long long MOD = 1e9 + 7; 15 | // ll a[MAXN], pre[MAXN]; 16 | #define all(x) (x).begin(), (x).end() 17 | #define pii pair 18 | #define m_p make_pair 19 | #define vi vector 20 | 21 | #define lp(i, a, b) for (int i = a; i < b; i++) 22 | #define fastIO() \ 23 | ios_base::sync_with_stdio(0); \ 24 | cin.tie(0); \ 25 | cout.tie(0); 26 | 27 | using namespace std; 28 | template istream& operator>>(istream& in, vector& a) {for (auto &x : a) in >> x; return in;}; 29 | template ostream& operator<<(ostream& out, vector& a) {for (auto &x : a) out << x << ' '; return out;}; 30 | void print(int a) { 31 | cout << a << '\n'; 32 | } 33 | void print(vector &a) { 34 | for(auto it : a) { 35 | cout << it << ' '; 36 | } 37 | cout << '\n'; 38 | } 39 | void print(vector &a) { 40 | for(auto it : a) { 41 | for(auto e : it) { 42 | cout << e << ' '; 43 | } 44 | cout << '\n'; 45 | } 46 | } 47 | 48 | template 49 | void print1dvector(vector V) { 50 | for (auto it : V) { 51 | cout << it << ' '; 52 | } 53 | cout << '\n'; 54 | } 55 | 56 | template 57 | void print2dvector(vector> V) { 58 | for (auto it : V) { 59 | for (auto itr : it) { 60 | cout << itr << ' '; 61 | } 62 | cout << '\n'; 63 | } 64 | } 65 | 66 | struct custom_hash { 67 | static uint64_t splitmix64(uint64_t x) { 68 | // http://xorshift.di.unimi.it/splitmix64.c 69 | x += 0x9e3779b97f4a7c15; 70 | x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; 71 | x = (x ^ (x >> 27)) * 0x94d049bb133111eb; 72 | return x ^ (x >> 31); 73 | } 74 | 75 | size_t operator()(uint64_t x) const { 76 | static const uint64_t FIXED_RANDOM = 77 | chrono::steady_clock::now().time_since_epoch().count(); 78 | return splitmix64(x + FIXED_RANDOM); 79 | } 80 | }; 81 | 82 | template // change the value of bitest to any size (say nax) 83 | // that you need to assign 84 | bool operator<(const std::bitset& x, 85 | const std::bitset& y) { 86 | for (int i = MAXN - 1; i >= 0; i--) { 87 | if (x[i] ^ y[i]) return y[i]; 88 | } 89 | return false; 90 | } 91 | 92 | void makeUniq(vector& vec) { 93 | sort(all(vec)); 94 | vec.resize(unique(all(vec)) - vec.begin()); 95 | } 96 | 97 | int binaryStrToDecimal(string b) { return stol(b, nullptr, 2); } 98 | 99 | int nxt() { 100 | int x; 101 | cin >> x; 102 | return x; 103 | } 104 | 105 | int Sqrt(int n) { 106 | int l = 1, r = n, ans = n; 107 | while (r >= l) { 108 | int mid = (l + r) / 2; 109 | if (mid > n / mid) 110 | r = mid - 1; 111 | else { 112 | ans = mid; 113 | l = mid + 1; 114 | } 115 | } 116 | return ans; 117 | } 118 | 119 | bool isPerfectSquare(int n) { 120 | int x = Sqrt(n); 121 | return x * x == n; 122 | } 123 | 124 | ll accurateFloor(ll a, ll b) { // gives the accurate floor for -ve/+ve 125 | ll val = a / b; 126 | while (val * b > a) val--; 127 | return val; 128 | } 129 | 130 | ll GCD(ll a, ll b) { return (a) ? GCD(b % a, a) : b; } 131 | 132 | ll LCM(ll a, ll b) { return a * b / GCD(a, b); } 133 | 134 | bool isPowerOf2(ll n) { return (ceil(log2(n)) == floor(log2(n))); } 135 | /* 136 | unsigned int hibit(unsigned int x) { // return the position of highest set 137 | bit 138 | if(x == 0) { 139 | return 0; 140 | } else { 141 | return (32 - (__builtin_clz(x | 1))); 142 | } 143 | } 144 | */ 145 | ll fastpow(ll b, ll p) { 146 | if (!p) return 1; 147 | ll ret = fastpow(b, p >> 1); 148 | ret *= ret; 149 | if (p & 1) ret *= b; 150 | return ret; 151 | } 152 | int powermodulo(int a, int p, int m) { 153 | int ans = 1; 154 | a = a % m; 155 | while (p > 0) { 156 | if (p & 1) ans *= a; 157 | ans = ans % m; 158 | a = (a * a) % m; 159 | p = p >> 1; 160 | } 161 | return ans; 162 | } 163 | int inverse(int a, int p = MOD) { return (powermodulo(a, p - 2, p)) % p; } 164 | struct DSU { 165 | vector e; 166 | DSU(int N) { e = vector(N, -1); } 167 | // get representive component (uses path compression) 168 | int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); } 169 | bool same_set(int a, int b) { return get(a) == get(b); } 170 | int size(int x) { return -e[get(x)]; } 171 | bool unite(int x, int y) { // union by size 172 | x = get(x), y = get(y); 173 | if (x == y) return false; 174 | if (e[x] > e[y]) swap(x, y); 175 | e[x] += e[y]; 176 | e[y] = x; 177 | return true; 178 | } 179 | }; 180 | const int factnax=2e5 + 20; 181 | vector fact; 182 | void pre_calc_fact() { 183 | fact = vector(factnax); 184 | fact[0] = 1; 185 | for (int i = 1; i < factnax; i++) { 186 | fact[i] = ((fact[i - 1] % MOD) * i) % MOD; 187 | } 188 | } 189 | 190 | int nCr(int n, int r) { 191 | if(r > n) return 0; 192 | return ((fact[n] % MOD * inverse(fact[r]) % MOD) % MOD * 193 | inverse(fact[n - r]) % MOD); 194 | } 195 | 196 | int mod_add(int a, int b) { 197 | return (a+b)%MOD; 198 | } 199 | int mod_sub(int a, int b) { 200 | return (a-b+MOD)%MOD; 201 | } 202 | int mod_mult(int a, int b) { 203 | return (a*b)%MOD; 204 | } 205 | int mod_div(int a, int b) { 206 | return (mod_mult(a , inverse(b))); 207 | } 208 | vector bstov(string s){ 209 | int n = s.size(); 210 | vector ans(n); 211 | for(int i=0;i bit; // binary indexed tree 219 | int n; 220 | 221 | fTree(int n) { 222 | this->n = n + 1; 223 | bit.assign(n + 1, 0); 224 | } 225 | 226 | fTree(vector a) : fTree(a.size()) { 227 | for (size_t i = 0; i < a.size(); i++) add(i, a[i]); 228 | } 229 | 230 | int sum(int idx) { 231 | int ret = 0; 232 | for (++idx; idx > 0; idx -= idx & -idx) ret += bit[idx]; 233 | return ret; 234 | } 235 | 236 | int sum(int l, int r) { return sum(r) - sum(l - 1); } 237 | 238 | void add(int idx, int delta) { 239 | for (++idx; idx < n; idx += idx & -idx) bit[idx] += delta; 240 | } 241 | void setX(int indx, int x) { add(indx, x - sum(indx, indx)); } 242 | }; 243 | 244 | int ask(int a, int b) { 245 | cout << "? " << a << " " << b << endl; 246 | string s; 247 | cin >> s; 248 | if(s[0] == 'Y') return 1; 249 | else return 0; 250 | } 251 | 252 | void give(int ans) { 253 | cout << "! " << ans << endl; 254 | } 255 | 256 | void solve() { 257 | int th = -1, sw = -1, tw = -1; 258 | 259 | int l = 0, r = 1000; 260 | 261 | 262 | function cond1 = [&] (int m) { 263 | return (ask(0,m)); 264 | }; 265 | while(l <= r) { 266 | int m = l + (r - l) / 2; 267 | if(cond1(m)) l = m + 1; 268 | else { 269 | r = m - 1; 270 | } 271 | } 272 | th = r; 273 | l = 0, r = 1000; 274 | function cond2 = [&](int m) { 275 | return ask(m, 0); 276 | }; 277 | while(l <= r) { 278 | int m = l + (r - l) / 2; 279 | if(cond2(m)) l = m + 1; 280 | else { 281 | r = m - 1; 282 | } 283 | } 284 | sw = r; 285 | l = 0, r = 1000; 286 | function cond3 = [&](int m) { 287 | return ask(m, 2*sw); 288 | }; 289 | while(l <= r) { 290 | int m = l + (r - l) / 2; 291 | if(cond3(m)) l = m + 1; 292 | else { 293 | r = m - 1; 294 | } 295 | } 296 | tw = r; 297 | 298 | give((th-2*sw)*tw + 4*sw*sw); 299 | } 300 | 301 | signed main() { 302 | fastIO(); 303 | int t = 1; 304 | //cin >> t; 305 | while (t--) { 306 | solve(); 307 | } 308 | return 0; 309 | } 310 | --------------------------------------------------------------------------------