牛客小白月赛37 G.零一奇迹(思维 枚举)

简介: 牛客小白月赛37 G.零一奇迹(思维 枚举)

原题链接

20200401134307494.png

思路:

先观察数据范围,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;
}
目录
打赏
0
0
0
0
108
分享
相关文章
斯坦福李飞飞团队祭出“灵巧手”,泡茶剪纸炫技
【2月更文挑战第26天】斯坦福李飞飞团队祭出“灵巧手”,泡茶剪纸炫技
124 5
斯坦福李飞飞团队祭出“灵巧手”,泡茶剪纸炫技
程序员在七夕如何用各大编程语言写浪漫情书呢?
程序员在七夕如何用各大编程语言写浪漫情书呢?
281 6
牛客寒假算法集训营 2 感想
【【题目讲解】2023牛客寒假算法基础集训营2】
152 0
牛客寒假算法集训营 2 感想
『中秋赏月』程序员用文心大模型带你玩转不一样的中秋
『中秋赏月』程序员用文心大模型带你玩转不一样的中秋
166 1
『中秋赏月』程序员用文心大模型带你玩转不一样的中秋
ChatGPT 大智近妖,从宇宙人生到手搓光刻机,从哄女朋友到写年终总结我们聊得非常开心,反而让人越来越忧心
都说 ChatGPT 要干掉程序员,清理搜索引擎,取代 Stack Overflow,还能消灭人类,这些有些言过其实了。ChatGPT 的定位是一个人工智能助理,它说,它的主要目的是通过回答用户的问题,为用户提供帮助。在体验了一天后,我相信对它的调教是成为一个正直的人,它也是这样做的。 它谦虚有礼,无疑是一个合格的助理,确实可以为我们提供很大的帮助。生成的回答条理清晰,思路明确,令人信服。但是从刚接触的惊奇开心过后,随着时间推移,我却越来越忧心了。 以下的内容是我的体验过程和其中的思考,其中引用的部分为 ChatGPT 的回答内容。希望对你了解它有一些帮助。
367 1
ChatGPT 大智近妖,从宇宙人生到手搓光刻机,从哄女朋友到写年终总结我们聊得非常开心,反而让人越来越忧心
百度飞桨世界冠军带你从零实践强化学习第五天(三岁白话时间)
百度飞桨世界冠军带你从零实践强化学习第五天(三岁白话时间)
237 0
百度飞桨世界冠军带你从零实践强化学习第五天(三岁白话时间)
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等