結果

問題 No.3266 岩井星人は見ずにはいられない
ユーザー kotatsugame
提出日時 2025-09-06 13:26:34
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,177 bytes
コンパイル時間 618 ms
コンパイル使用メモリ 70,184 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-06 13:26:59
合計ジャッジ時間 1,898 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<cassert>
using namespace std;
//0-indexed
#include<vector>
#include<cassert>
template<typename T>
struct BIT{
	int N;
	vector<T>bit;
	BIT(int N_=0):N(N_),bit(N_){}
	T sum(int i)
	{
		assert(0<=i&&i<=N);
		T ans=0;
		for(;i>0;i-=i&-i)ans+=bit[i-1];
		return ans;
	}
	void add(int i,T a)
	{
		assert(0<=i&&i<N);
		i++;
		for(;i<=N;i+=i&-i)bit[i-1]+=a;
	}
	int lower_bound(T k)//k<=sum(ret)
	{
		if(k<=0)return 0;
		int ret=0,i=1;
		while((i<<1)<=N)i<<=1;
		for(;i;i>>=1)
		{
			if(ret+i<=N&&bit[ret+i-1]<k)
			{
				ret+=i;
				k-=bit[ret-1];
			}
		}
		return ret+1;
		//ret+1 == N+1 <=> sum<k
	}
};
int N;
long A;
string S;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>N>>A>>S;
	int cur=0;
	vector<int>E;
	BIT<int>bit(N);
	for(int i=0;i<N;i++)
	{
		if(S[i]=='0')cur--;
		else
		{
			if(cur==0)E.push_back(i);
			else cur++,bit.add(i,1);
		}
	}
	A--;
	long pos=0;
	if(A<bit.sum(N))
	{
		cout<<pos+bit.lower_bound(A+1)<<endl;
		return 0;
	}
	A-=bit.sum(N);
	for(int t=0;t<-cur&&t<E.size();t++)bit.add(E[t],1);
	pos+=N;
	long t=A/bit.sum(N);
	A-=t*bit.sum(N);
	pos+=t*N;
	cout<<pos+bit.lower_bound(A+1)<<endl;
}
0