結果
| 問題 | No.2992 Range ABCD String Query |
| コンテスト | |
| ユーザー |
Taiki0715
|
| 提出日時 | 2026-11-05 23:56:58 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 430 ms / 6,000 ms |
| + 857µs | |
| コード長 | 16,848 bytes |
| 記録 | |
| コンパイル時間 | 3,383 ms |
| コンパイル使用メモリ | 348,148 KB |
| 実行使用メモリ | 49,040 KB |
| 最終ジャッジ日時 | 2026-11-05 23:57:25 |
| 合計ジャッジ時間 | 23,099 ms |
|
ジャッジサーバーID (参考情報) |
judge4_0 / judge7_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 41 |
ソースコード
#include<cassert>
#include <bits/stdc++.h>
#ifndef IO_HPP
#define IO_HPP
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<array>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
using namespace std;
template<typename T1,typename T2>istream &operator>>(istream&,pair<T1,T2>&);
template<typename...Args>istream &operator>>(istream&,tuple<Args...>&a);
template<typename T>istream &operator>>(istream&is,vector<T>&a);
template<typename T,size_t N>istream &operator>>(istream&is,array<T,N>&a);
template<typename T1,typename T2>
istream &operator>>(istream&is,pair<T1,T2>&a){
is>>a.first>>a.second;
return is;
}
template<size_t pos,typename...Args>
void read_tuple(istream&is,tuple<Args...>&a){
if constexpr(pos<tuple_size<tuple<Args...>>::value){
is>>get<pos>(a);
read_tuple<pos+1>(is,a);
}
}
template<typename...Args>
istream &operator>>(istream&is,tuple<Args...>&a){
read_tuple<0>(is,a);
return is;
}
template<typename T>
istream &operator>>(istream&is,vector<T>&a){
for(T&x:a)is>>x;
return is;
}
template<typename T,size_t N>
istream &operator>>(istream&is,array<T,N>&a){
for(T&x:a)is>>x;
return is;
}
template<typename T1,typename T2>ostream &operator<<(ostream&os,const pair<T1,T2>&);
template<typename...Args>ostream &operator<<(ostream&os,const tuple<Args...>&);
template<typename T>ostream &operator<<(ostream&os,const vector<T>&);
template<typename T,typename Seq,typename Comp>ostream &operator<<(ostream&os,priority_queue<T,Seq,Comp>);
template<typename T>ostream &operator<<(ostream&os,queue<T>);
template<typename T>ostream &operator<<(ostream&os,deque<T>);
template<typename T>ostream &operator<<(ostream&os,stack<T>);
template<typename T,size_t N>ostream &operator<<(ostream&os,const array<T,N>&);
template<typename Key,typename Val,typename Comp>ostream &operator<<(ostream&os,const map<Key,Val,Comp>&);
template<typename Key,typename Val,typename Hash>ostream &operator<<(ostream&os,const unordered_map<Key,Val,Hash>&);
template<typename T,typename Comp>ostream &operator<<(ostream&os,const set<T,Comp>&);
template<typename T,typename Comp>ostream &operator<<(ostream&os,const multiset<T,Comp>&);
template<typename T,typename Hash>ostream &operator<<(ostream&os,const unordered_set<T,Hash>&);
template<typename T1,typename T2>
ostream &operator<<(ostream&os,const pair<T1,T2>&a){
os<<a.first<<' '<<a.second;
return os;
}
template<size_t pos,typename...Args>
void write_tuple(ostream&os,const tuple<Args...>&a){
if constexpr(pos<tuple_size<tuple<Args...>>::value){
if constexpr(pos>0)os<<' ';
os<<get<pos>(a);
write_tuple<pos+1>(os,a);
}
}
template<typename...Args>
ostream &operator<<(ostream&os,const tuple<Args...>&a){
write_tuple<0>(os,a);
return os;
}
template<typename T>
ostream &operator<<(ostream&os,const vector<T>&a){
os<<'{';
for(int i=0;i<(int)a.size();i++){
os<<a[i];
if(i+1!=a.size())os<<',';
}
os<<'}';
return os;
}
template<typename T,typename Seq,typename Comp>
ostream &operator<<(ostream&os,priority_queue<T,Seq,Comp>a){
os<<'{';
if(!a.empty()){
os<<a.top();a.pop();
while(!a.empty()){
os<<',';
os<<a.top();
a.pop();
}
}
os<<'}';
return os;
}
template<typename T>
ostream &operator<<(ostream&os,queue<T>a){
os<<'{';
if(!a.empty()){
os<<a.front();a.pop();
while(!a.empty()){
os<<',';
os<<a.front();
a.pop();
}
}
os<<'}';
return os;
}
template<typename T>
ostream &operator<<(ostream&os,deque<T>a){
os<<'{';
if(!a.empty()){
os<<a.front();a.pop_front();
while(!a.empty()){
os<<',';
os<<a.front();
a.pop_front();
}
}
os<<'}';
return os;
}
template<typename T>
ostream &operator<<(ostream&os,stack<T>a){
os<<'{';
if(!a.empty()){
os<<a.top();a.pop();
while(!a.empty()){
os<<',';
os<<a.top();
a.pop();
}
}
os<<'}';
return os;
}
template<typename T,size_t N>
ostream &operator<<(ostream&os,const array<T,N>&a){
os<<'{';
for(int i=0;i<(int)a.size();i++){
os<<a[i];
if(i+1!=a.size())os<<',';
}
os<<'}';
return os;
}
template<typename Key,typename Val,typename Comp>
ostream &operator<<(ostream&os,const map<Key,Val,Comp>&a){
if(a.empty()){
os<<"{}";
return os;
}
auto itr=a.begin();
os<<"{["<<itr->first<<","<<itr->second<<']';
while(++itr!=a.end())os<<",["<<itr->first<<','<<itr->second<<']';
os<<'}';
return os;
}
template<typename Key,typename Val,typename Hash>
ostream &operator<<(ostream&os,const unordered_map<Key,Val,Hash>&a){
if(a.empty()){
os<<"{}";
return os;
}
auto itr=a.begin();
os<<"{["<<itr->first<<","<<itr->second<<']';
while(++itr!=a.end())os<<",["<<itr->first<<','<<itr->second<<']';
os<<'}';
return os;
}
template<typename T,typename Comp>
ostream &operator<<(ostream&os,const set<T,Comp>&a){
if(a.empty()){
os<<"{}";
return os;
}
auto itr=a.begin();
os<<'{'<<*itr;
while(++itr!=a.end())os<<','<<*itr;
os<<'}';
return os;
}
template<typename T,typename Comp>
ostream &operator<<(ostream&os,const multiset<T,Comp>&a){
if(a.empty()){
os<<"{}";
return os;
}
auto itr=a.begin();
os<<'{'<<*itr;
while(++itr!=a.end())os<<','<<*itr;
os<<'}';
return os;
}
template<typename T,typename Hash>
ostream &operator<<(ostream&os,const unordered_set<T,Hash>&a){
if(a.empty()){
os<<"{}";
return os;
}
auto itr=a.begin();
os<<'{'<<*itr;
while(++itr!=a.end())os<<','<<*itr;
os<<'}';
return os;
}
#endif
using namespace std;
using ll=long long;
using ull=unsigned long long;
using P=pair<ll,ll>;
template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;
template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}
template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}
template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int){a.first++,a.second++;}
template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int){a.first--,a.second--;}
template<typename T>void operator++(vector<T>&a,int){for(auto &i:a)i++;}
template<typename T>void operator--(vector<T>&a,int){for(auto &i:a)i--;}
using vref=typename vector<bool>::reference;
vref operator|=(vref a,bool b){a=a|b;return a;}
vref operator&=(vref a,bool b){a=a&b;return a;}
vref operator^=(vref a,bool b){a=a^b;return a;}
#define overload3(_1,_2,_3,name,...) name
#define rep1(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,l,r) for(int i=(int)(l);i<(int)(r);i++)
#define rep(...) overload3(__VA_ARGS__,rep2,rep1)(__VA_ARGS__)
#define reps(i,l,r) rep2(i,l,r)
#define all(x) x.begin(),x.end()
#define pcnt(x) __builtin_popcountll(x)
#define fin(x) return cout<<(x)<<'\n',static_cast<void>(0)
#define yn(x) cout<<((x)?"Yes\n":"No\n")
#define uniq(x) sort(all(x)),x.erase(unique(all(x)),x.end())
template<typename T>
inline int fkey(vector<T>&z,T key){return lower_bound(z.begin(),z.end(),key)-z.begin();}
ll myceil(ll a,ll b){return (a+b-1)/b;}
template<typename T,size_t n,size_t id=0>
auto vec(const int (&d)[n],const T &init=T()){
if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));
else return init;
}
#ifdef LOCAL
#include<debug.h>
#define SWITCH(a,b) (a)
#else
#define debug(...) static_cast<void>(0)
#define debugg(...) static_cast<void>(0)
#define SWITCH(a,b) (b)
#endif
struct Timer{
clock_t start;
Timer(){
start=clock();
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout<<fixed<<setprecision(16);
}
inline double now(){return (double)(clock()-start)/1000;}
#ifdef LOCAL
~Timer(){
cerr<<"time:";
cerr<<now();
cerr<<"ms\n";
}
#endif
}timer;
void SOLVE();
int main(){
int testcase=1;
//cin>>testcase;
for(int i=0;i<testcase;i++){
SOLVE();
}
}
#include<type_traits>
#include<concepts>
template<typename T>
constexpr std::enable_if_t<std::numeric_limits<T>::digits<=32,int>msb(T n){return n==0?-1:31-__builtin_clz(n);}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),int>msb(T n){return n==0?-1:63-__builtin_clzll(n);}
template<typename T>
constexpr std::enable_if_t<std::numeric_limits<T>::digits<=32,int>lsb(T n){return n==0?-1:__builtin_ctz(n);}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),int>lsb(T n){return n==0?-1:__builtin_ctzll(n);}
template<typename T>
constexpr std::enable_if_t<std::is_integral_v<T>,T>floor_pow2(T n){return n==0?0:T(1)<<msb(n);}
template<typename T>
constexpr std::enable_if_t<std::is_integral_v<T>,T>ceil_pow2(T n){return n<=1?1:T(1)<<(msb(n-1)+1);}
template<std::integral T>
constexpr T safe_div(T a,T b){return a/b-(a%b&&(a^b)<0);}
template<std::integral T>
constexpr T safe_ceil(T a,T b){return a/b+(a%b&&(a^b)>0);}
template<typename M>
struct SegmentTree{
using S=typename M::S;
private:
int n,z;
std::vector<S>dat;
public:
SegmentTree():n(0),z(0),dat(){}
explicit SegmentTree(int n):n(n),z(ceil_pow2(n)),dat(ceil_pow2(n)*2,M::e()){}
explicit SegmentTree(const std::vector<S>&a):n(a.size()),z(ceil_pow2((int)a.size())){
dat.resize(z*2,M::e());
for(int i=0;i<n;i++)dat[i+z]=a[i];
for(int i=z-1;i>=1;i--)dat[i]=M::op(dat[i*2],dat[i*2+1]);
}
SegmentTree(int n,S init):SegmentTree(std::vector<S>(n,init)){}
inline S get(int i)const{return dat[i+z];}
void set(int i,S x){
assert(0<=i&&i<n);
i+=z;
dat[i]=x;
i>>=1;
while(i){
dat[i]=M::op(dat[i*2],dat[i*2+1]);
i>>=1;
}
}
S prod(int l,int r)const{
assert(0<=l&&l<=r&&r<=n);
if(r==n)r=z;
l+=z,r+=z;
S left=M::e(),right=M::e();
while(l<r){
if(l&1)left=M::op(left,dat[l++]);
if(r&1)right=M::op(dat[--r],right);
l>>=1,r>>=1;
}
return M::op(left,right);
}
inline S all_prod()const{return dat[1];}
template<typename Func>
int max_right(int l,const Func&f)const{
if(l==n)return n;
l+=z;
S now=M::e();
do{
while((~l)&1)l>>=1;
S nxt=M::op(now,dat[l]);
if(f(nxt))now=nxt,l++;
else{
while(l<z){
l<<=1;
nxt=M::op(now,dat[l]);
if(f(nxt))now=nxt,l++;
}
return l-z;
}
}while(l!=(l&-l));
return n;
}
template<typename Func>
int min_left(int r,const Func&f)const{
if(r==0)return 0;
r+=z;
S now=M::e();
do{
r--;
while(r>1&&(r&1))r>>=1;
S nxt=M::op(dat[r],now);
if(f(nxt))now=nxt;
else{
while(r<z){
r=(r<<1)+1;
nxt=M::op(dat[r],now);
if(f(nxt))now=nxt,r--;
}
return r-z+1;
}
}while(r!=(r&-r));
return 0;
}
friend std::ostream &operator<<(std::ostream &os,const SegmentTree&seg){
os<<"{";
for(int i=0;i<seg.n;i++)os<<seg.dat[i+seg.z]<<",}"[i+1==seg.n];
if(seg.n==0)os<<"}";
return os;
}
};
template<typename T,bool MIN=true>
struct min_plus_semiring{
private:
T v;
public:
min_plus_semiring():v(0){}
min_plus_semiring(T v):v(v){}
static constexpr bool comp(T a,T b){
if constexpr(MIN)return a<b;
else return a>b;
}
static constexpr T zero(){
if constexpr(MIN)return std::numeric_limits<T>::max()/2;
else return std::numeric_limits<T>::min()/2;
}
static constexpr T one(){return 0;}
T val()const{return v;}
min_plus_semiring &operator+=(const min_plus_semiring&b){
if(comp(b.v,v))v=b.v;
return *this;
}
min_plus_semiring &operator*=(const min_plus_semiring&b){
v+=b.v;
return *this;
}
friend min_plus_semiring operator+(const min_plus_semiring&a,const min_plus_semiring&b){return min_plus_semiring(a)+=b;}
friend min_plus_semiring operator*(const min_plus_semiring&a,const min_plus_semiring&b){return min_plus_semiring(a)*=b;}
friend std::istream &operator>>(std::istream&is,min_plus_semiring&b){
is>>b.v;
return is;
}
friend std::ostream &operator<<(std::ostream&os,const min_plus_semiring&b){
os<<b.v;
return os;
}
auto operator<=>(const min_plus_semiring&)const=default;
};
template<typename T,int N,int M=N>
struct ConstantMatrix{
private:
std::array<std::array<T,M>,N>dat;
static constexpr T zero(){
if constexpr(requires(){T::zero();})return T::zero();
else return T(0);
}
static constexpr T one(){
if constexpr(requires(){T::one();})return T::one();
else return T(1);
}
public:
ConstantMatrix():ConstantMatrix(T()){}
explicit ConstantMatrix(T x){
dat.fill([&x](){std::array<T,M>res;res.fill(x);return res;}());
}
static ConstantMatrix E(){
static_assert(N==M);
ConstantMatrix res(zero());
for(int i=0;i<N;i++)res[i][i]=one();
return res;
}
inline ConstantMatrix &operator+=(const ConstantMatrix&rhs){
for(int i=0;i<N;i++)for(int j=0;j<M;j++)(*this)[i][j]+=rhs[i][j];
return *this;
}
inline ConstantMatrix &operator-=(const ConstantMatrix&rhs){
for(int i=0;i<N;i++)for(int j=0;j<M;j++)(*this)[i][j]-=rhs[i][j];
return *this;
}
inline ConstantMatrix &operator*=(const ConstantMatrix&rhs){
static_assert(N==M);
ConstantMatrix res;
for(int i=0;i<N;i++)for(int j=0;j<M;j++)for(int k=0;k<M;k++)res[i][k]+=(*this)[i][j]*rhs[j][k];
(*this)=std::move(res);
return *this;
}
friend ConstantMatrix operator+(const ConstantMatrix&lhs,const ConstantMatrix&rhs){return ConstantMatrix(lhs)+=rhs;}
friend ConstantMatrix operator-(const ConstantMatrix&lhs,const ConstantMatrix&rhs){return ConstantMatrix(lhs)-=rhs;}
template<int K>friend ConstantMatrix<T,N,K> operator*(const ConstantMatrix&lhs,const ConstantMatrix<T,M,K>&rhs){
ConstantMatrix<T,N,K>res(zero());
for(int i=0;i<N;i++)for(int j=0;j<M;j++)for(int k=0;k<K;k++)res[i][k]+=lhs[i][j]*rhs[j][k];
return res;
}
std::array<T,M> &operator[](int i){return dat[i];}
const std::array<T,M> &operator[](int i)const{return dat[i];}
friend bool operator==(const ConstantMatrix&lhs,const ConstantMatrix&rhs){
for(int i=0;i<N;i++)for(int j=0;j<M;j++)if(lhs[i][j]!=rhs[i][j])return false;
return true;
}
friend bool operator!=(const ConstantMatrix&lhs,const ConstantMatrix&rhs){
for(int i=0;i<N;i++)for(int j=0;j<M;j++)if(lhs[i][j]!=rhs[i][j])return true;
return false;
}
T det()const{
static_assert(N==M);
T res=1;
ConstantMatrix mat(*this);
for(int i=0;i<M;i++){
int idx=-1;
for(int j=i;j<N;j++)if(mat[j][i]!=T()){
idx=j;
break;
}
if(idx==-1)return T();
if(i!=idx)std::swap(mat[i],mat[idx]),res=-res;
res*=mat[i][i];
T inv_ii=mat[i][i].inv();
for(int j=i+1;j<M;j++)mat[i][j]*=inv_ii;
for(int j=i+1;j<N;j++){
T coef=-mat[j][i];
for(int k=i+1;k<M;k++)mat[j][k]+=coef*mat[i][k];
}
}
return res;
}
ConstantMatrix inv()const{
static_assert(N==M);
ConstantMatrix res=E(),mat=(*this);
for(int i=0;i<N;i++){
int id=-1;
for(int j=i;j<N;j++)if(mat[j][i]!=T()){
id=j;
break;
}
if(id==-1)return ConstantMatrix();
if(i!=id){
std::swap(mat[i],mat[id]);
std::swap(res[i],res[id]);
}
T inv=mat[i][i].inv();
for(int j=0;j<N;j++)if(i!=j){
T x=mat[j][i];
for(int k=0;k<N;k++){
mat[j][k]-=mat[i][k]*x;
res[j][k]-=res[i][k]*x;
}
}
}
return res;
}
template<std::integral U>
ConstantMatrix pow(U k)const{
static_assert(N==M);
ConstantMatrix res=ConstantMatrix::E(),a(*this);
while(k){
if(k&1)res*=a;
a*=a;
k>>=1;
}
return res;
}
std::vector<std::vector<T>>to_vector()const{
std::vector<std::vector<T>>res(N,std::vector<T>(M));
for(int i=0;i<N;i++)for(int j=0;j<M;j++)res[i][j]=dat[i][j];
return res;
}
friend std::istream &operator>>(std::istream&is,ConstantMatrix&rhs){
for(int i=0;i<N;i++)for(int j=0;j<M;j++)is>>rhs[i][j];
return is;
}
friend std::ostream &operator<<(std::ostream &os,const ConstantMatrix&rhs){
for(int i=0;i<N;i++){
for(int j=0;j<M;j++)os<<rhs[i][j]<<" \n"[j+1==M];
}
return os;
}
};
struct M{
using S=ConstantMatrix<min_plus_semiring<int>,4>;
static S op(S x,S y){return x*y;}
static S e(){return S::E();}
};
void SOLVE(){
int n,q;
cin>>n>>q;
string s;
cin>>s;
auto get=[](char c)->M::S {
M::S res;
rep(i,4)rep(j,4){
if(i<=j){
res[i][j]=c==j+'A'?0:1;
}
else res[i][j]=1e9;
}
return res;
};
vector<M::S>init(n);
rep(i,n)init[i]=get(s[i]);
SegmentTree<M>seg(init);
while(q--){
int t;
cin>>t;
if(t==1){
int x;
char c;
cin>>x>>c;
x--;
seg.set(x,get(c));
}
else{
int l,r;
cin>>l>>r;
l--;
auto mat=seg.prod(l,r);
int ans=1e9;
rep(i,4)rep(j,4)chmin(ans,mat[i][j].val());
cout<<ans<<'\n';
}
}
}
Taiki0715