CQOI216路由表



题目有些绕口,求查询字符串在字符串a至字符串b间匹配项更改了几次

添加时,掩码长度是多少就只添加那么多位

用trie树进行匹配,每次匹配到新项时加入维护递增的单调栈

(若又短又靠后,根本没机会与查询串匹配)

最后的答案即为单调栈的大小

#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int x=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
	return f==1?x:-x;
}
#define ll long long
const int N=1e7;
int ch[N][2],tg[N],tot=1,id=0;
inline void insert(ll x,int len){
	int p=1;
	for(int i=31,c;len;i--,len--){
		c=(x>>i)&1;
		if(!ch[p][c])ch[p][c]=++tot;
		p=ch[p][c];
	}
	tg[p]=++id;
}
inline int query(ll x,int t){
	stack<int>s;
	while(!s.empty())s.pop();
	int p=1;
	for(int i=31,c;i>=0;i--){
		c=(x>>i)&1;
		if(!ch[p][c])break;
		p=ch[p][c];
		if(tg[p]&&tg[p]<=t){
			while(!s.empty()&&tg[p]<s.top())s.pop();
			s.push(tg[p]);
		}
	}
	return s.size();
}
int main(){
	int Q=read();
	while(Q--){
		static char op[4];
		static ll x;
		static int a,b;
		scanf("%s",op);
		if(op[0]=='A'){
			x=0;
			a=read();x=(x<<8)+a; 
			a=read();x=(x<<8)+a; 
			a=read();x=(x<<8)+a; 
			a=read();x=(x<<8)+a; 
			insert(x,read());
		}
		else{
			x=0;
			a=read();x=(x<<8)+a; 
			a=read();x=(x<<8)+a; 
			a=read();x=(x<<8)+a; 
			a=read();x=(x<<8)+a; 
			a=read();b=read();
			cout<<query(x,b)-query(x,a-1)<<"\n";
		}
	} 
	return (0-0);
}

猜你喜欢

转载自www.cnblogs.com/aurora2004/p/12572542.html
今日推荐