QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#800056#8252. Tip of Your TongueYarema#AC ✓341ms224076kbC++203.5kb2024-12-05 20:49:522024-12-05 20:49:54

Judging History

你现在查看的是最新测评结果

  • [2024-12-05 20:49:54]
  • 评测
  • 测评结果:AC
  • 用时:341ms
  • 内存:224076kb
  • [2024-12-05 20:49:52]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef double db;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const int N = 200'447;
const int AL = 26;

struct Node
{
	int cnt;
	int nxt[AL];
	
	Node()
	{
		cnt = 0;
		fill(nxt, nxt + AL, -1);
	}
};

struct Trie
{
	vector<Node> t;
	
	Trie(): t(1) {}
	
	void add(string s)
	{
		int v = 0;
		for (auto c : s)
		{
			if (t[v].nxt[c - 'a'] == -1)
			{
				t[v].nxt[c - 'a'] = SZ(t);
				t.PB(Node());
			}
			v = t[v].nxt[c - 'a'];
			t[v].cnt++;
		}
	}
	int get(string s)
	{
		int v = 0;
		for (auto c : s)
		{
			v = t[v].nxt[c - 'a'];
			if (v == -1)
				return 0;
		}
		return t[v].cnt;
	}
	
};

struct Segtree
{
	int n;
	vector<ordered_set> t;
	
	Segtree(int _n)
	{
		n = 1;
		while (n < _n)
			n *= 2;
		t.resize(2 * n - 1);
	}
	
	void upd(int v, int tl, int tr, int i, int x)
	{	
		t[v].insert(x);
		if (tl + 1 == tr)
			return;
		int tm = (tl + tr) / 2;
		if (i < tm)
			upd(v * 2 + 1, tl, tm, i, x);
		else
			upd(v * 2 + 2, tm, tr, i, x);
	}
	void upd(int i, int x)
	{
		upd(0, 0, n, i, x);
	}
	int query(int v, int tl, int tr, int l, int r, int L, int R)
	{
		if (l <= tl && tr <= r)
			return t[v].order_of_key(R + 1) - t[v].order_of_key(L);
		if (l >= tr || tl >= r)
			return 0;
		int tm = (tl + tr) / 2;
		return query(v * 2 + 1, tl, tm, l, r, L, R) + 
			   query(v * 2 + 2, tm, tr, l, r, L, R);
	}
	int query(int l, int r, int L, int R)
	{
		return query(0, 0, n, l, r, L, R);
	}
	
};

int lb(VI& idx, vector<string>& v, string& s)
{
	int l = -1;
	int r = SZ(v);
	while (l + 1 < r)
	{
		int m = (l + r) / 2;
		if (s <= v[idx[m]])
			r = m;
		else
			l = m;
	}
	return r;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n, q;
	cin >> n >> q;
	vector<string> v(n), r(n);
	Trie t1, t2;
	FOR (i, 0, n)
	{
		cin >> v[i];
		r[i] = v[i];
		reverse(ALL(r[i]));
		t1.add(v[i]);
		t2.add(r[i]);
	}
	VI idx(n);
	iota(ALL(idx), 0);
	sort(ALL(idx), [&](int i, int j)
	{
		return v[i] < v[j];
	});
	VI val(n);
	FOR (i, 0, n)
		val[idx[i]] = i;

	VI idxr(n);
	iota(ALL(idxr), 0);
	sort(ALL(idxr), [&](int i, int j)
	{
		return r[i] < r[j];
	});
	Segtree st(n + 1);
	FOR (i, 0, n)
	{
		st.upd(i, val[idxr[i]]);
	}
	
	
	FOR (i, 0, q)
	{
		string t, p, s;
		cin >> t >> p >> s;
		reverse(ALL(s));
		//cerr << t1.get(p) << ' ' << t2.get(s) << '\n';
		int plus = t1.get(p) + t2.get(s);
		int L = lb(idx, v, p);
		
		bool isPref = L == n ? false : p == v[idx[L]].substr(0, SZ(p));
		p[SZ(p) - 1]++;
		int R = lb(idx, v, p);
		
		int Lr = lb(idxr, r, s);
		s[SZ(s) - 1]++;
		int Rr = lb(idxr, r, s);
		
		
		int all = isPref ? st.query(Lr, Rr, L, R - 1) : 0;
		//cerr << L << ' ' << R << ' ' << Lr << ' ' << Rr << '\n';
		//cerr << plus << ' ' << all << '\n';
		if (t == "AND")
			cout << all << '\n';
		else if (t == "OR")
			cout << plus - all << '\n';
		else
			cout << plus - 2 * all << '\n';
		//cerr << "\n\n";
	}
	
	
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3900kb

input:

4 4
cat
catcat
octal
occidental
AND cat cat
OR oc at
AND ca at
XOR oc al

output:

2
4
2
0

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

26 36
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
AND b b
AND d d
XOR tk ce
AND w w
AND z z
XOR t t
OR a a
OR s s
AND p p
AND v v
AND pp kh
XOR j j
AND n n
OR f f
XOR vo mj
OR m m
XOR q q
XOR r r
AND i i
OR l l
OR jb cg
XOR x x
XOR nf ov
OR e e
XOR h h
XOR u u
XOR c c
OR y y
XOR nc ln
AND o ...

output:

1
1
0
1
1
0
1
1
1
1
0
0
1
1
0
1
0
0
1
1
0
0
0
1
0
0
0
1
0
1
0
0
0
0
1
1

result:

ok 36 lines

Test #3:

score: 0
Accepted
time: 20ms
memory: 46956kb

input:

18 18
a
ab
abba
abbabaab
abbabaabbaababba
abbabaabbaababbabaababbaabbabaab
abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababba
abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaab
abbabaabbaababbabaababbaabbabaa...

output:

18
17
8
8
14
7
6
11
10
5
4
3
3
2
2
0
1
0

result:

ok 18 lines

Test #4:

score: 0
Accepted
time: 43ms
memory: 224076kb

input:

1 1
ocyzfhhambhirbkeodnshorawhzsqakgywcadicyacxleobjmjrbgqrdqqvqecccxuahrkwnimglrcmiaujynldydopyasdbefpxmagfquhzrtfnuikojwpjocjwrogxhrquqruqqsunsjotsgeetddviaoswcavdswftyheurrclunactnwqhnqzrxjlsipyxxmmeiwxawqzvhtcmmadyvfzrhinphlibltwartaczraqkaaljefkksbawqmnsbquqxpbneshiuypfafihqtehavpdsoauwyvsqblxo...

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 2ms
memory: 5180kb

input:

1 1
a
OR zxuxomuitrpatgdidwnuxfrwultbimmesgzkgcdsvjzmanhyebwdzowthmiqlsagxoqvyciyslmoxvceppnkjuvzhszgcdnpmdqxhbinbknbbzfcnhizysyeaemkpeandwqutpmnxytmnkzfghmptiqnppuwndlxlhpimvmsaytdycezpodqcbddybswutmimtpgzhmijvpphelairwewqshektuldufwxuzirjkpemoafnpopqfgxefcxuzgbbhehfpbmkdqbxsdejspcydluchncbhbfghoys...

output:

0

result:

ok single line: '0'

Test #6:

score: 0
Accepted
time: 44ms
memory: 58480kb

input:

250 50000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1
250
249
1
1
1
1
250
250
1
1
250
249
1
1
249
250
250
1
250
250
1
249
1
1
249
1
1
1
250
249
250
250
250
250
1
250
249
249
249
1
250
1
250
249
249
250
249
250
250
1
250
250
250
250
250
1
1
249
249
250
1
1
250
1
250
249
250
1
249
1
249
1
249
1
1
1
250
250
249
249
250
250
1
250
249
250
1
1
250
1
250
25...

result:

ok 50000 lines

Test #7:

score: 0
Accepted
time: 24ms
memory: 83412kb

input:

10 10
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

10
10
0
10
10
10
10
0
0
10

result:

ok 10 lines

Test #8:

score: 0
Accepted
time: 90ms
memory: 53936kb

input:

10000 40000
lhheuleivzyldlchherj
phgmehaliwwulthedmop
ifszbvkgywuyemcxbvns
wycojxbttxudiiclvgvk
camfcfwvxlvyoyfyqsci
zqkxtcfhovfemzizxhev
ugbsvnxratulswilmekh
cvveyzynvzgnfukgorio
lsstnqurowhzvvzmvhwh
crhuqauvnvwdeyqlxchi
xvbybzkvzkjntwtdiglt
ylgvjsugwaicedszzhlv
jyswsdydzgicxheoqjfh
jnbmcjgbyfmxyae...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 40000 lines

Test #9:

score: 0
Accepted
time: 255ms
memory: 198704kb

input:

40000 10000
lynhbrgdcbqmazwibcti
xxbyaqngttjaadowpoxd
jyxvinberwqbykawbyvf
vayqjzfyjlvzsuwwrsvs
lukgakfkiiaxuedhtzpx
uwhoadeqqzhkwggfebkj
uyfreqcgwgtqvqlwitzf
zajebujpbhufhvyowsbh
sbnvcyswptsbbdvdxsxl
oozymjrfnjogbxlbptbw
vlxvuhfueomwhmjcddlc
szffcnpiwpgrymdqimnp
cesclsyvliesdrhoulim
ceryoczqhltomnm...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 10000 lines

Test #10:

score: 0
Accepted
time: 159ms
memory: 123676kb

input:

25000 25000
vdyrdfobpnmzkafujwav
sejhjjarcfatdrxcdhxg
tgvgnooftnykcynfbyxs
xtzqsasggmfouxwbsnhz
npwdngyaxjblnlqvgiqu
ivcevcmhsezxcqyhihvt
ilrztetzikmehuztbghe
vosgdubogglelqdgjsyf
gagqyoscjfirtpsdpcjy
wdrhtcvbelqkxxiymjif
iukqdbdggkpjftahbsuq
huvznzywxqpmpfnrjgkc
ieezfelxxquvridxswbl
vhtrgplvqgewjhk...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 25000 lines

Test #11:

score: 0
Accepted
time: 341ms
memory: 137860kb

input:

50000 50000
fhbzhhsbzj
iplzfbimxp
plobcikqkd
amxzydjuwi
xiublxnnoi
cpnwnulhjz
cpspkovuot
dvkiqopibs
deehvpeijn
cueklrdhay
ubcoawbhvd
irzkcpnyhv
yehoeolcbq
shxsfaekgn
ynqdlcblvc
avexwjuolf
oabfylgpjm
xkkqgisphg
jiokzejjof
ryobskxxtu
rotajeixrg
ctvimviwgz
smbimudyle
olnerzqovj
woslasgtbg
pewowwuuth
th...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 50000 lines

Test #12:

score: 0
Accepted
time: 44ms
memory: 174940kb

input:

20 20
gddaatgysreztfnjttlpsfhskbxjqltiokdxifltzipusrztchtejvziswzynfyytotyefjuseowxnakvzvzecyfxstsbtegvmtzjsdaixnnjvoyrqfmvnqcgiqznzzyosolumqhzuhknstpsindhxfctphufyebubprvlxoqmtfsbcnehyjewncdmiyxxrotihfxvdkinwtbtfqyncrypmbrbevsvofswrfvcuzcpwdaxsxjqgluuvvsteaobdlnlwboswqmvbexhpxzmktadrtwvcsdpfyluwhht...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 20 lines

Test #13:

score: 0
Accepted
time: 36ms
memory: 109664kb

input:

20 20
vieddzryrbrgvezcrdvtfujyllhusrcemmxlarpmwccaeuvjnxecjxyxhhilaiyqtisyxxtfjnlzvepxiivavycywkkxdrmifgxggcupbbkvlymnrrvfaauoxieymchvdyravgowdvaoqjnbojfpgmtieydackvuruzhnmcblxlmiauyrpmbntnejwprfoeylbpzrpnjoqxpafdkfrnhfcjtbzpekpxfnwvjkpnvdlfbvrblsysybxpeonxwaqaiuqswbfialryoxezgoijbktvrisrrgrndkmtibl...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 20 lines

Test #14:

score: 0
Accepted
time: 19ms
memory: 111768kb

input:

10 10
fxbapnhzxqgypinkcwvcdffncpofscfumrhdywwsndelaxgwwintmtrbwbfkmycuuofnpkxusaxushqptczpkvjzutdgjbjubkvwopzvduvinmixbozzyuwnozuwemctztrcyqdwqejqbnvrsuhoherqgjdxctzfrdzysinwauupiealofgezefjzubjycpxswstyqcohmllwtfmrdtqakfgopirtyjlwniezmllpfcwloczdfgtwkxumffysdtpiclykobzoqqzprsjqjjfkcuaeokdxhhqgnxjpb...

output:

0
0
0
0
0
0
0
0
0
0

result:

ok 10 lines

Test #15:

score: 0
Accepted
time: 143ms
memory: 90584kb

input:

20000 15000
ycpcujwskbndpkstgaznlbryxji
ycpcujwskbdseynvaaznlbryxji
ycpcujwskbygsrainaznlbryxji
ycpcujwskbnatcrctaznlbryxji
ycpcujwskbkaxnsymaznlbryxji
ycpcujwskbtpqpibsaznlbryxji
ycpcujwskbhmudkxraznlbryxji
ycpcujwskbueqmfflaznlbryxji
ycpcujwskbfkeudapaznlbryxji
ycpcujwskbjxnhhhsaznlbryxji
ycpcujws...

output:

20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
...

result:

ok 15000 lines

Test #16:

score: 0
Accepted
time: 3ms
memory: 4880kb

input:

10 2700
jmldwyxnouejzaqmujxahyissokibdqbgvqzauolmrjbzuibrkncaxfeepypxavmwqfntjzqszisvunypogzuiczxiaqkjlyzhmzirbnrsibwgyiqbvdqfccztiepuihkdqutzoctwnxfgixmawyyldzxrrtclpujhywsyidnetozusfvsfyrhfqfhufvlcwfwovdrlkczeinjygyjlijrygjplsssqhobmxdrgotnmwwwekgompkmfwjmvwgcsfrmmmlybrwcgnbsbxwpthpuwynlefetsygzln...

output:

10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
...

result:

ok 2700 lines

Test #17:

score: 0
Accepted
time: 6ms
memory: 4988kb

input:

10 2700
otckkjfmnhodtkatewfpeyxqurahqazyormsrsxhdioocfgslbxyzcnbiyvsgfxpfqcxkifjnzfuvhpytvyzqhnspltgahsczsnhdwbgwtfwjdhyisufyqupenclrlxwebpipopfmbpdjwelmiiocjddgyihlomaetukpbxxqpvjvqkfhupzhwdwdyshekxagurryjzaealcvqsomigcihlxtlgbiyutuqskwoofnqofgzkkgoncmkhjgijtlxfjewsampxorudxhlctcfspudtkrhnyexcyeoli...

output:

10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
...

result:

ok 2700 lines