0pts trie求助!
查看原帖
0pts trie求助!
1023793
yaodiguoan楼主2024/8/15 14:47

与题解思路无差,讨论区里的hack数据可过

#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(2)
//#pragma GCC optimize(3, "Ofast", "inline")
#define hh putchar('\n')
#define kg putchar(' ')
#define debug puts("debug")
//#define int long long
//#define int __int128
namespace quickread{
	template<typename T> void read(T &x){
		x=0;char c=getchar();T neg=0;
		while(!isdigit(c)) neg|=!(c^'-'),c=getchar();
		while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
		if(neg) x=(~x)+1;
	}
	template<typename T> void write(T x){
		if(x<0) putchar('-'),x=~x+1;
		if(x>9) write(x/10);
		putchar(x%10^48);
	}
	inline string readstr(){
    	char ch=getchar();string str="";
		while(!isalnum(ch)) ch=getchar();
		while(isalnum(ch)){str+=ch;ch=getchar();}
		return str;
	}
	inline char readchar(){
		char ch=getchar();
		while(!isalnum(ch)) ch=getchar();
		return ch;
	}
}
using namespace quickread;
const int N=5e5+10,inf=2147483647;
int n,m;
namespace trie{
	int cnt=1;
	struct node{int nex[2],sum,tag;node(){nex[1]=nex[2]=sum=tag=0;};};
	node tree[N];
	void update(int len){
		int p=1;
		for(int i=1;i<=len;++i){
			int num;read(num);
			if(!tree[p].nex[num]) tree[p].nex[num]=++cnt;
			p=tree[p].nex[num];
			++tree[p].sum;
		}
		++tree[p].tag;
	}
	int query(int len){
		int p=1,ans=0;
		for(int i=1;i<=len;++i){
			int num;read(num);
			if(!tree[p].nex[num]) return ans;
			p=tree[p].nex[num];
			ans+=tree[p].tag;
		}
		return ans+tree[p].sum-tree[p].tag;
	}
}using namespace trie;
signed main(){
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	read(n),read(m);
	for(int i=1;i<=n;++i){
		int x;read(x);
		update(x);
	}
	for(int i=1;i<=m;++i){
		int x;read(x);
		write(query(x)),hh;
	}
	return 0;
}

2024/8/15 14:47
加载中...