(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
Catalog
Problem:Portal传送门
原题目描述在最下面。
每次把\(n\)变成\(⌊ \frac{n}{2}⌋,n\%2, ⌊ \frac{n}{2}⌋\),直至 \(n\) 为\(1\) 或\(0\),问区间\(1\)的数量。
Solution:
类似线段树的结构,模拟线段树即可。可以枚举加单点更新也可以区间更新。
\(R-L \leq 1e5, \; n \leq 2^{50}\)
单点更新复杂度:\(log(n)\times1e5\)
区间更新复杂度:\(log(n)\)
AC_Code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<bitset>
#include<cassert>
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define mme(a,b) memset((a),(b),sizeof((a)))
#define fuck(x) cout<<"* "<<x<<"\n"
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef pair<int,int> pii;
typedef long long LL;
const int MXN = 1e5 + 7;
const int MXE = 1e6 + 7;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
LL n, l ,r;
/*****区间更新*****/
LL query(LL L,LL R,LL l,LL r,LL rt){
if(L>r||R<l)return 0;
if(rt<2||l==r)return rt%2;
LL mid = (l + r)/2, ans = 0;
if(L>mid){
ans = query(L,R,mid+1,r,rt/2);
}else if(R<mid){
ans = query(L,R,l,mid-1,rt/2);
}else{
ans = query(L,mid-1,l,mid-1,rt/2)+query(mid+1,R,mid+1,r,rt/2)+query(mid,mid,mid,mid,rt%2);
}
return ans;
}
int main(){
while(~scanf("%lld%lld%lld", &n, &l, &r)){
LL ans = 0, m = n, len = 1;
while(m > 1){
len = len<<1|1;
m >>= 1;
}
printf("%lld\n", query(l,r,1,len,n));
}
return 0;
}
/*****枚举+单点更新*****/
int query(LL p,LL l,LL r,LL rt){
if(rt<2||l==r){
return rt;
}
LL mid = (l + r)/2;
if(p==mid)return query(p,mid,mid,rt%2);
else if(p<mid)return query(p,l,mid-1,rt/2);
return query(p,mid+1,r,rt/2);
}
int main(){
while(~scanf("%lld%lld%lld", &n, &l, &r)){
LL ans = 0, m = n, len = 1;
while(m>1){
len = len<<1|1;
m >>= 1;
}
for(LL i = l; i <= r; ++i){
ans += query(i,1,len,n);
}
printf("%lld\n", ans);
}
return 0;
}
Problem Description: