O(26nqlogn) 可以过?
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
string s;
int n,q,c[maxn][26],l[maxn],r[maxn];
inline int read(){
int res=0;
char ch=getchar();
while(ch<'0'||ch>'9')
ch=getchar();
while(ch>='0'&&ch<='9'){
res=(res<<1)+(res<<3)+(ch^'0');
ch=getchar();
}
return res;
}
namespace Tree{
#define lowbit(x) x&-x
inline void add(int x,int y,int z){
for(;x<=n;x+=lowbit(x))
c[x][y]+=z;
}
inline int query(int x,int y){
int ans=0;
for(;x;x-=lowbit(x))
ans+=c[x][y];
return ans;
}
}
using namespace Tree;
inline bool check(){
for(int i=1;i<=q;++i)
for(int j=0;j<26;++j)
if(query(r[i],j)-query(l[i]-1,j)>1)
return 0;
return 1;
}
int main(){
cin>>s;
n=s.size(),q=read();
s=' '+s;
for(int i=1;i<=n;++i)
add(i,s[i]-'a',1);
for(int i=1;i<=q;++i)
l[i]=read(),r[i]=read();
for(int i=1;i<=n;++i){
if(check()){
printf("%d\n",i-1);
return 0;
}
int k=read();
add(k,s[k]-'a',-1);
}
return 0;
}