rt,我的提交不开O2的话最后几个点总是超0.07左右,求大佬帮忙看看怎么优化?
思路:Trie树+单调栈维护(单调栈维护用了二分
//P5768 [CQOI2016]路由表
#include<iostream>
#include<cstring>
using namespace std;
const int N=1e6+1,M=2;
int m,cnt,tot=1;
int e[N][M],c[N];
void change(string s,int &l,string &ans){
int k2[10],d=0,num=0;
l=0;
for(int i=0;i<s.size();i++){
if(s[i]=='.'||s[i]=='/'){
for(int j=1;j<=8;j++){
k2[j]=num%2;
num/=2;
}
for(int j=8;j>=1;j--){
char t=k2[j]+'0';
ans=ans+t;
}
num=0;
if(s[i]=='/')d=1;
continue;
}
if(d){
l=l*10+(s[i]-'0');
}
else{
num=num*10+(s[i]-'0');
}
}
}
void ins(string s,int ti){
int len,root=1;
string s1;
s1.clear();
change(s,len,s1);
for(int i=0;i<len;i++){
int id=s1[i]-'0';
if(!e[root][id]) e[root][id]=++tot;
if(i==len-1) c[e[root][id]]=ti;
root=e[root][id];
}
}
int a[N];
void ask(string s,int l,int r){
int top=0;
int len,root=1;
string s1;
s1.clear();
s=s+"/32";
change(s,len,s1);
for(int i=0;i<len;i++){
int id=s1[i]-'0';
if(!e[root][id]) break;
if(c[e[root][id]]&&c[e[root][id]]<=r){
int t=c[e[root][id]];
if(!top)a[++top]=t;
else{
int ll=0,rr=top;
while(ll<rr){
int mid=(ll+rr+1)>>1;
if(a[mid]<=t) ll=mid;
else rr=mid-1;
}
top=ll;
a[++top]=t;
}
}
root=e[root][id];
}
int ans=top;
int ll=0,rr=top;
while(ll<rr){
int mid=(ll+rr+1)>>1;
if(a[mid]<l) ll=mid;
else rr=mid-1;
}
ans=ans-ll;
cout<<ans<<'\n';
return ;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>m;
while(m--){
char op;
string s;
int l,r;
cin>>op>>s;
if(op=='A')
cnt++,ins(s,cnt);
if(op=='Q'){
cin>>l>>r;
ask(s,l,r);
}
}
return 0;
}