F2. Guess the K-th Zero (Hard version)(分块)

52 篇文章 1 订阅

https://codeforces.com/contest/1520/problem/F2


思路:先分块,再块中二分。暴力修改400ms

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
struct P{
   LL l,r,sum;
}block[maxn];
LL ask(LL l,LL r){
   cout<<"? "<<l<<" "<<r<<endl;
   LL num;cin>>num;
   cout.flush();
   return num;
}
bool check(LL mid){
     LL res=block[mid].r;///区间长度
     LL num=res-block[mid].sum;///0的总数
     return num;
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n,t;cin>>n>>t;
   LL p=20;
   LL cnt=(n+p-1)/p;///要分成的总块数
   bool flag=1;
   while(t--){
      LL pos;cin>>pos;
      if(flag==1){
         flag=0;
         for(LL i=1;i<=cnt;i++){
             LL l=(i-1)*p+1;
             LL r=(i)*p;
             block[i].l=l;block[i].r=min(r,n);
             block[i].sum=ask(1,min(r,n));///记录块的前缀和
         }
      }
      LL l=1;LL r=cnt;
//      while(l<r){
//          LL mid=(l+r)>>1;
//          if(check(mid)>=pos){///1到mid的0的个数
//             r=mid;
//          }
//          else l=mid;
//      }
      LL choice=l;
      for(LL i=l;i<=r;i++){
          LL res=block[i].r;///1~r的区间长度
          LL num1=block[i].sum;///1~i块的区间1数量
          if(res-num1>=pos){
             choice=i;
             break;
          }
      }
      ///choice此时为找到的块
      LL L=block[choice].l;LL R=min(n,block[choice].r);
      while(L<R){
            LL mid=(L+R)>>1;

            LL num=ask(1,mid);///1的个数

            LL cnt=(mid-num);///1~mid的0的个数

            if(cnt>=pos) R=mid;
            else L=mid+1;
      }
      cout<<"! "<<L<<endl;
      cout.flush();
      ///暴力修改
      for(LL i=choice;i<=cnt;i++){
          block[i].sum++;
      }

   }
   return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值