QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#55825 | #4496. Shinobu Loves Segment Tree | zzxzzx123 | AC ✓ | 22ms | 3596kb | C++ | 1.6kb | 2022-10-15 12:46:53 | 2022-10-15 12:46:55 |
Judging History
answer
#include <bits/stdc++.h>
#define frein(f) freopen(f ".in", "r", stdin)
#define freout(f) freopen(f ".out", "w", stdout)
using namespace std;
void solve()
{
long long n, x, len, ans = 0;
cin >> n >> x;
if (x == 1)
ans = (n + n * n) / 2;
else
{
len = 1;
while ((len << 1) <= x)
len <<= 1;
long long l1 = 2, l2 = 2, r, tx;
tx = x >> 1; //计算1出现的最小值
while (tx > 1)
{
if (tx & 1)
l1 = l1 << 1;
else
l1 = (l1 << 1) - 1;
tx >>= 1;
}
if (l1 > n) //全都是0的情况
{
cout << "0\n";
return;
}
tx = x; //计算2出现的最小值
while (tx > 1)
{
if (tx & 1)
l2 = l2 << 1;
else
l2 = (l2 << 1) - 1;
tx >>= 1;
}
if (l2 > n) //全都是1的情况
{
cout << n - l1 + 1 << "\n";
return;
}
ans = l2 - l1; //左侧1的区间累加
r = 1 + (n - l2 + 1) / len; //计算连续区间右端最大值
ans += len * (2 + r) * (r - 2 + 1) / 2; //连续区间累加和
ans += (n - l2 + 1) % len * (r + 1); //剩余右端值累加
}
cout << ans << "\n";
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 22ms
memory: 3596kb
input:
100000 249 707 360 1429 151 380 118 103 221 711 88 79 471 1668 222 377 481 676 483 921 326 558 347 1151 104 220 91 97 258 250 446 122 368 1335 242 335 395 470 180 669 99 222 342 979 345 431 119 97 283 781 325 643 488 1413 285 868 205 723 118 115 397 526 432 1557 197 761 145 287 304 270 331 243 98 36...
output:
0 0 0 61 0 28 0 64 151 176 0 0 11 58 230 1585 0 0 192 0 0 0 100 108 0 0 0 0 0 70 0 0 0 0 64 328 0 808 390 0 0 0 35 0 63 128 405 0 0 52 0 0 146 0 48 662 0 0 0 0 72 6757 0 15 63 150 30 6 66 0 0 236 0 459 100 0 63 0 0 105 0 81 34 0 0 208 0 0 2484 0 63 71 198 89 172 83 19 160 0 237 0 0 0 28 423 32 0 220...
result:
ok 100000 lines