結果

問題 No.3853 Unique Letters between Equal Letters
コンテスト
ユーザー askr58
提出日時 2026-12-04 22:40:05
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 5,757 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,457 ms
コンパイル使用メモリ 351,240 KB
実行使用メモリ 14,268 KB
最終ジャッジ日時 2026-12-04 22:40:50
合計ジャッジ時間 20,209 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge6_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 2 WA * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// SPDX-License-Identifier: CC0-1.0
//
// [English]
// To the extent possible under law, the author(s) have dedicated all copyright
// and related and neighboring rights to this software to the public domain
// worldwide. This software is distributed without any warranty.
// See <https://creativecommons.org/publicdomain/zero/1.0/> for details.
//
// [日本語]
// 法律上可能な範囲で、著作者はこのソフトウェアに関する著作権および
// 著作隣接権を含むすべての関連する権利を、全世界でパブリックドメインに
// 提供しています。本ソフトウェアは一切の保証なく配布されます。
// 自由にコピー・改変・再配布・商用利用できます(クレジット表記は不要です)。
// 詳細は <https://creativecommons.org/publicdomain/zero/1.0/deed.ja> を参照してください。
// ※ 英語の文面および CC0 1.0 の正式な条文(英語)が優先されます。

#include <iostream>
#include <bitset>
#include <random>
#include <chrono>
#include <iomanip>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <ranges>
#include <algorithm>
#include <vector>
using namespace std;
using ll=long long;

#include <atcoder/all>
using mint=atcoder::modint998244353;

ostream& operator<<(ostream& os,const mint& x){
	os<<x.val();
	return os;
}
istream& operator>>(istream& is,mint& x){
	int t;
	is>>t;
	x=t;
	return is;
}

template <typename S,typename T>
ostream& operator<<(ostream& os,const pair<S,T>& p);
template <typename S,typename T>
istream& operator>>(istream& is,pair<S,T>& p);
template <typename T,size_t n>
ostream& operator<<(ostream& os,const array<T,n>& arr);
template <typename T,size_t n>
istream& operator>>(istream& is,array<T,n>& arr);
template <typename T>
ostream& operator<<(ostream& os,const vector<T>& vec);
template <typename T>
istream& operator>>(istream& is,vector<T>& vec);

template <typename S,typename T>
ostream& operator<<(ostream& os,const pair<S,T>& p){
	os<<p.first<<" "<<p.second;
	return os;
}
template <typename S,typename T>
istream& operator>>(istream& is,pair<S,T>& p){
	is>>p.first>>p.second;
	return is;
}

template <typename T,size_t n>
ostream& operator<<(ostream& os,const array<T,n>& arr){
	for(int i=0;i<n;i++)os<<arr[i]<<(i+1==n?"":" ");
	return os;
}
template <typename T,size_t n>
istream& operator>>(istream& is,array<T,n>& arr){
	for(int i=0;i<n;i++)is>>arr[i];
	return is;
}
template <typename T>
ostream& operator<<(ostream& os,const vector<T>& vec){
	for(int i=0;i<(int)vec.size();i++)os<<vec[i]<<(i+1==(int)vec.size()?"":" ");
	return os;
}
template <typename T>
istream& operator>>(istream& is,vector<T>& vec){
	for(int i=0;i<(int)vec.size();i++)is>>vec[i];
	return is;
}

template<class... Vecs>
void input_vec(Vecs&... vs) {
    const auto n = get<0>(tie(vs...)).size();

    for (size_t i = 0; i < n; ++i)
        ((cin >> vs[i]), ...);
}

template <class... Vecs>
void output_vec(const Vecs&... vs) {
    const auto n = get<0>(tie(vs...)).size();

    for (size_t i = 0; i < n; ++i) {
        bool first = true;
        (((cout << (exchange(first, false) ? "" : " ") << vs[i])), ...);
        cout << endl;
    }
}

template <typename C>
C make_unique(C vec){
	ranges::sort(vec);
	vec.erase(unique(vec.begin(),vec.end()),vec.end());
	return vec;
}

vector<int> Iota(int n,int s=0){
	vector<int> res(n);
	iota(res.begin(),res.end(),s);
	return res;
}

vector<ll> Iotall(int n,ll s=0){
	vector<ll> res(n);
	iota(res.begin(),res.end(),s);
	return res;
}
void YESNO(bool f){
	if(f)cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
}

using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using vvvl=vector<vector<vector<ll>>>; 
using vi=vector<int>;
using vvi=vector<vector<int>>;
using vvvi=vector<vector<vector<int>>>;
using vmi=vector<mint>;
using vvmi=vector<vector<mint>>;
using vvvmi=vector<vector<vector<mint>>>;
using S=pair<int,int>;
S op(S x,S y){
	if((x.second&y.second)>0)x.first|=1;
	return make_pair(x.first|y.first,x.second|y.second);
}
S e(){
	return make_pair(0,0);
}
S op2(S x,S y){
	return make_pair(max(x.first,y.first),min(x.second,y.second));
}
S e2(){
	return make_pair(-1,1e9);
}
bool f(S x){
	return x.first<x.second;
}
int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	int n,q;
	cin>>n>>q;
	vi a(n);
	{
		string s;
		cin>>s;
		for(int i=0;i<n;i++)a[i]=s[i]-'0';
	}
	vector<S> vec(n);
	for(int i=0;i<n;i++)vec[i]=make_pair(0,1<<a[i]);
	atcoder::segtree<S,op,e> seg(vec);
	atcoder::segtree<S,op2,e2> seg2(n);

	vector<set<int>> s(10);
	for(int i=0;i<10;i++){
		s[i].insert(-1);
		s[i].insert(-2);
		s[i].insert(-3);
		s[i].insert(n+2);
		s[i].insert(n+3);
		s[i].insert(n+4);
	}
	for(int i=0;i<n;i++){
		s[a[i]].insert(i);
	}
	for(int i=0;i<n;i++){
		auto itr=s[a[i]].lower_bound(i);
		for(int j=0;j<3;j++){
			itr++;
			int r=min(n,*itr);
			if(j==2){
				seg2.set(i,make_pair(i,n));
				break;
			}
			if(seg.prod(i+1,r).first){
				seg2.set(i,make_pair(i,r));
				break;
			}
		}
	}
	vl v(n);
	ll ans=0;
	for(int i=0;i<n;i++){
		int r=seg2.max_right<f>(i);			
		v[i]=r;
		ans+=r-i;
	}

	while(q--){
		int I,c;
		cin>>I>>c;
		I--;
		s[a[I]].erase(I);
		a[I]=c;
		s[a[I]].insert(I);
		seg.set(I,make_pair(0,1<<a[I]));
		for(int i=I-60;i<=I;i++){
			if(i<0)continue;
			auto itr=s[a[i]].lower_bound(i);
			for(int j=0;j<3;j++){
				itr++;
				int r=min(n,*itr);
				if(j==2){
					seg2.set(i,make_pair(i,n));
					break;
				}
				if(seg.prod(i+1,r).first){
					seg2.set(i,make_pair(i,r));
					break;
				}
			}
		}
		for(int i=I-60;i<=I;i++){
			if(i<0)continue;
			ans-=v[i]-i;
			int r=seg2.max_right<f>(i);			
			v[i]=r;
			ans+=r-i;
		}
		cout<<ans-2*n+1<<endl;
	}
}
0