QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#43295 | #4496. Shinobu Loves Segment Tree | BobWang | AC ✓ | 23ms | 1772kb | C++ | 1.1kb | 2022-08-08 21:30:19 | 2022-08-08 21:30:20 |
Judging History
answer
#include<cstdio>
#define ll long long
using namespace std;
ll t,n,x;
ll find(ll t,ll x)
{
if(x==1)
return t;
return (2*(x>>1)==x)?(find(t,x>>1)+1)/2:find(t,x>>1)/2;
}
ll get_0(ll x)
{
ll ret=2;
x>>=1;
while(x>1)
{
if(2*(x>>1)==x)
ret=ret*2-1;
else ret=ret*2;
x>>=1;
}
return ret-1;
}
ll get_2(ll x)
{
ll ret=1;
while(x>1)
{
ret*=2;
x>>=1;
}
return ret;
}//统计2的个数
int main()
{
scanf("%lld",&t);
while(t--)
{
ll val,cnt,res,ans=0;
scanf("%lld%lld",&n,&x);
if(x==1)
{
printf("%lld\n",(n+1)*n/2);
continue;
}
res=n-get_0(x);//减去0的个数
if(res<=0)
{
printf("0\n");
continue;
}
val=find(n,x);
cnt=get_2(x>>1);
if(2*(x>>1)+1==x)
{
if((cnt<<1)>=res)
ans=res;
else
ans+=cnt<<1;//统计1的个数
res-=cnt<<1;
}
else
{
if(cnt>=res)
ans=res;
else
ans+=cnt;
res-=cnt;
}
if(res<=0)
{
printf("%lld\n",ans);
continue;
}
cnt<<=1;
ans+=((val+1)*(val-2)/2)*cnt;
res-=(val-2)*cnt;
ans+=res*val;
printf("%lld\n",ans);
// printf("%lld\n",val);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 23ms
memory: 1772kb
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