QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#828109#4496. Shinobu Loves Segment TreehotdogsellerAC ✓339ms4040kbC++142.0kb2024-12-23 13:22:312024-12-23 13:22:31

Judging History

你现在查看的是最新测评结果

  • [2024-12-23 13:22:31]
  • 评测
  • 测评结果:AC
  • 用时:339ms
  • 内存:4040kb
  • [2024-12-23 13:22:31]
  • 提交

answer

#include<bits/stdc++.h>

#define int long long
#define pii pair<int,int>
#define maxn 1005
#define INF 1e18

using namespace std;

inline int read(){
    int lre=0,f=1;
    char c=getchar();
    while(!isdigit(c)){
        if(c=='-'){
            f=-1;
        }
        c=getchar();
    }
    while(isdigit(c)){
        lre=(lre<<3)+(lre<<1)+(c-'0');
        c=getchar();
    }
    return lre*f;
}

int n,x;
vector<char> v;//L或R 

int k=0,ans;

int calc(int x,bool f=false){
	if(f)cout<<x;
	for(char c:v){
		if(x==1){
			x=0;
		}else if(c=='L'){
			x=(x+1)/2;
		}else{
			x=x/2; 
		}
		if(f)cout<<"->"<<x;
	}
	if(f)cout<<endl;
	return x;
}

int leap(int x){
	int l=x,r=n,mid,lre=-1;
	while(l<=r){
		mid=l+r>>1;
		if(calc(mid)==calc(x)){
			lre=mid;
			l=mid+1;
		}else{
			r=mid-1;
		}
	}
	//[x,lre]的值全都是一样的
	ans+=(lre-x+1)*calc(x); 
	return lre+1;
}//查询跳的步数并返回新的位置 

int figure(int N){
	return(N-1)*N/2;
}//[0,1...,N-1]之和 

void solve(){
	n=read();x=read();
	v.clear();k=35;ans=0;
	while(!((x>>k)&1))k--;
//	cout<<"k="<<k<<endl;
	for(int i=k-1;i>=0;i--){
		if((x>>i)&1){
			v.push_back('R');
		}else{
			v.push_back('L');
		}
	}
	int L=pow(2,v.size());
//	cout<<"v:";
//	for(char c:v)cout<<c;
//	cout<<endl;
	//在log次二分之后必然会进入每2^v.size()一次的规律 
	int i=1;
	while(i<=n){
		int j=leap(i);
	//	cout<<i<<"->"<<j<<" ans="<<ans<<endl;
		if(j-i==L){
			i=j;
			break;
		}
		i=j;
	//	calc(i,true);
	}
	//从i开始进入循环节 
	//以calc(i)为首项,公差为1,(n-i+1)/L项的等比数列
	if(i<=n){
		int leap_L=(n-i+1)/L;
		ans+=L*(calc(i)*leap_L+figure(leap_L));
		int rest_L=(n-i+1)%L;
		ans+=calc(n)*rest_L; 
	}
	printf("%lld\n",ans);
}
//根节点:1~n的等差数列
//左儿子:每一位除以2,上取整
//右儿子:同上,但是下取整 
//等于1直接强制等于0 

signed main(){
	
	int t=read();
	while(t--){
		solve();
	}
	
	return 0;
}
/*
1
1000000000 4000000000

*/

詳細信息

Test #1:

score: 100
Accepted
time: 339ms
memory: 4040kb

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