結果
| 問題 |
No.1742 Binary Indexed Train
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2021-11-12 22:41:32 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 257 ms / 3,000 ms |
| コード長 | 1,031 bytes |
| コンパイル時間 | 405 ms |
| コンパイル使用メモリ | 30,208 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-25 20:21:09 |
| 合計ジャッジ時間 | 5,983 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
#include <stdio.h>
long long bit[61];
long long recursion(int N, long long S, long long T, int type, int ks, int kt)
{
if (S == T) return 0;
else if (type == 1) {
if (ks == kt) return (T - S) / bit[ks];
else if (ks < kt) {
for (; S > T - bit[kt]; kt--);
return recursion(N, S, T - bit[kt], 1, ks, kt) + 1;
} else {
for (; S + bit[ks] > T; ks--);
return recursion(N, S + bit[ks], T, 1, ks, kt) + 1;
}
}
int k;
long long X, Y;
for (k = N; (S + bit[k] - 1) / bit[k] > T / bit[k]; k--);
X = (S + bit[k] - 1) / bit[k] * bit[k];
Y = T / bit[k] * bit[k];
return recursion(k, S, X, 1, ks, k) + recursion(k, Y, T, 1, k, kt) + (Y - X) / bit[k];
}
int main()
{
int i, N, Q, ks, kt;
long long S, T;
scanf("%d %d", &N, &Q);
for (i = 1, bit[0] = 1; i <= N; i++) bit[i] = bit[i-1] << 1;
for (i = 1; i <= Q; i++) {
scanf("%lld %lld", &S, &T);
for (ks = N; S % bit[ks] != 0; ks--);
for (kt = N; T % bit[kt] != 0; kt--);
printf("%lld\n", recursion(N, S, T, 0, ks, kt));
}
fflush(stdout);
return 0;
}