1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
| #include <bits/stdc++.h>
using namespace std;
const int MX = 200005;
struct oper_t {
int type; // 0 for modify, 1 for query
tuple<int, int, int> info; // (x, y, delta) for modify, (x, y, ctrb) for query
oper_t(int t, tuple<int, int, int> i) : type(t), info(i) {}
};
struct Trie {
shared_ptr<Trie> son[26];
vector<int> labels;
int dfn, end;
};
void Trie_insert(shared_ptr<Trie> o, const char *s, int label) {
while(*s != '\0') {
if(o->son[*s - 'a'] == nullptr) {
o->son[*s - 'a'] = make_shared<Trie>();
}
o = o->son[*s - 'a'];
s++;
}
o->labels.push_back(label);
}
pair<int, int> Trie_query(shared_ptr<Trie> o, const char *s, int k) {
for(int i = 0; i < k; i++) {
o = o->son[s[i] - 'a'];
if(o == nullptr) {
return {-1, -1};
}
}
return {o->dfn, o->end};
}
int dfn_str[MX];
void dfs(shared_ptr<Trie> u) {
static int cnt = 0;
u->dfn = ++cnt;
for(int id : u->labels) {
dfn_str[id] = cnt;
}
for(shared_ptr<Trie> cur : u->son) {
if(cur != nullptr) {
dfs(cur);
}
}
u->end = cnt;
}
void solve(oper_t *begin, oper_t *end, vector<int> &ans) {
size_t n = (end - begin);
if(n == 1) {
return;
}
oper_t *mid = begin + n / 2;
vector<oper_t *> modify;
for(oper_t *i = begin; i != mid; i++) {
if(i->type == 0) {
modify.push_back(i);
}
}
sort(modify.begin(), modify.end(), [&] (oper_t * u, oper_t * v) {
return get<0>(u->info) < get<0>(v->info);
});
vector<oper_t *> query;
for(oper_t *i = mid; i < end; i++) {
if(i->type == 1) {
query.push_back(i);
}
}
sort(query.begin(), query.end(), [&] (oper_t * u, oper_t * v) {
return get<0>(u->info) < get<0>(v->info);
});
static int bit[MX + 10];
auto bit_insert = [&] (int u, int val) {
while(u <= MX) {
bit[u] += val;
u += u & -u;
}
};
auto bit_query = [&] (int u) {
int res = 0;
while(u) {
res += bit[u];
u -= u & -u;
}
return res;
};
size_t it = 0;
for(oper_t *cur_query : query) {
auto [cur_x, cur_y, ctrb] = cur_query->info;
while(it != modify.size()) {
auto [xx, yy, delta] = modify[it]->info;
if(xx > cur_x) {
break;
}
bit_insert(yy, delta);
it++;
}
if(ctrb > 0) {
ans[ctrb] += bit_query(cur_y);
} else {
ans[-ctrb] -= bit_query(cur_y);
}
}
for(size_t i = 0; i < it; i++) {
auto [xx, yy, delta] = modify[i]->info;
bit_insert(yy, -delta);
}
solve(begin, mid, ans);
solve(mid, end, ans);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
shared_ptr<Trie> o = make_shared<Trie>();
for(int i = 1; i <= n; i++) {
string str;
cin >> str;
Trie_insert(o, str.c_str(), i);
}
dfs(o);
vector<oper_t> ops;
for(int i = 1; i <= n; i++) {
ops.emplace_back(0, make_tuple(dfn_str[i], i, 1));
}
int qid = 0;
for(int i = 1; i <= m; i++) {
int op;
cin >> op;
if(op == 1) {
int x, y;
cin >> x >> y;
ops.emplace_back(0, make_tuple(dfn_str[x], x, -1));
ops.emplace_back(0, make_tuple(dfn_str[y], y, -1));
swap(dfn_str[x], dfn_str[y]);
ops.emplace_back(0, make_tuple(dfn_str[x], x, 1));
ops.emplace_back(0, make_tuple(dfn_str[y], y, 1));
} else {
string str;
int k, l, r;
cin >> str >> k >> l >> r;
++qid;
pair<int, int> dfn_rg = Trie_query(o, str.c_str(), k);
if(dfn_rg.first != -1) {
ops.emplace_back(1, make_tuple(dfn_rg.second, r, qid));
ops.emplace_back(1, make_tuple(dfn_rg.first - 1, r, -qid));
ops.emplace_back(1, make_tuple(dfn_rg.second, l - 1, -qid));
ops.emplace_back(1, make_tuple(dfn_rg.first - 1, l - 1, qid));
}
}
}
vector<int> ans(qid + 1);
solve(ops.data(), ops.data() + ops.size(), ans);
for(int i = 1; i <= qid; i++) {
cout << ans[i] << "\n";
}
return 0;
}
|