思路:
先观察数据范围,n < = 1 e 5 ,但是l , r < = 1 e 18 l,也就是说在[ l , r ]的数转化为二进制数后最多有64位,所以每次改变影响的只有64位。
可以先统计一遍第一次改变后的答案。对于后面的每次改变,假设当前改变的位置为x,消除[ x − 63 , x ]的贡献,改变后加上[ x − 63 , x ]的贡献。
在统计答案的时候,枚举起点和位数,如果大于r直接跳出即可。
还要注意不能含有前导零,枚举起点的时候遇到0应该直接跳过
代码:
// Problem: 零一奇迹 // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/11214/G // Memory Limit: 524288 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) #pragma GCC optimize(1) #pragma GCC optimize(2) #pragma GCC optimize(3,"Ofast","inline") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> //#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll>PLL; typedef pair<int, int>PII; typedef pair<double, double>PDD; #define I_int ll inline ll read(){ll x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;} inline void write(ll x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');} #define read read() #define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define multiCase int T;cin>>T;for(int t=1;t<=T;t++) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i<(b);i++) #define per(i,a,b) for(int i=(a);i>=(b);i--) #define perr(i,a,b) for(int i=(a);i>(b);i--) ll ksm(ll a, ll b,ll mod){ll res = 1;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;} const int maxn=2e5+100,inf=0x3f3f3f3f; const double eps=1e-5; string s; ll tr[maxn]; int n; ll l,r; bool check(string t){ ll ans=0; for(int i=0;t[i];i++) ans=ans*2+(t[i]-'0'); if(ans>=l&&ans<=r) return 1; return 0; } ll cul(int L,int R){ ll ans=0; for(int i=L;i<=R;i++){ if(s[i]=='0') continue; ll tmp=0; for(int j=1;j<=64&&i+j-1<=n;j++){ int now=i+j-1; tmp=tmp*2+(s[now]-'0'); if(tmp>=l&&tmp<=r) ans++; if(tmp>r) break; } } return ans; } int main(){ #ifdef LOCAL freopen("in.in","r",stdin); freopen("out.out","w",stdout); #endif n=read; l=read,r=read; cin>>s; s=" "+s; int q=read; ll ans=0; for(int Case=1;Case<=q;Case++){ int x=read; if(Case==1){ if(s[x]=='0') s[x]='1'; else s[x]='0'; ans=cul(1,n); } else{ ll t1=cul(max(1,x-63),x); if(s[x]=='0') s[x]='1'; else s[x]='0'; ll t2=cul(max(1,x-63),x); ans=ans-t1+t2; } printf("%lld\n",ans); } return 0; }