QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#83996 | #2135. How Many Strings Are Less | Zuqa# | TL | 697ms | 100932kb | C++20 | 8.3kb | 2023-03-04 21:06:46 | 2023-03-04 21:06:49 |
Judging History
answer
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define el '\n'
#define FIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> pt;
typedef unsigned long long ull;
template<typename T, typename X>
using hashTable = gp_hash_table<T, X>;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
// mt19937_64 for long long
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());
const int N = 1e6 + 5, P1 = 29, P2 = 31, mod = 1e9 + 7;
int pw1[N], inv1[N];
int prefixPW1[N];
int add(int a, int b)
{
return (a + b) % mod;
}
int sub(int a, int b)
{
return ((a - b) % mod + mod) % mod;
}
int mul(int a, int b)
{
return 1ll * a * b % mod;
}
int fp(int base, int power)
{
if(!power)
return 1;
int ret = fp(base, power >> 1);
ret = mul(ret, ret);
if(power & 1)
ret = mul(ret, base);
return ret;
}
struct Hash
{
vector<int> prefix;
Hash(const string &s)
{
prefix.resize(s.size() + 1);
for(int i = 0; i < int(s.size()); ++i)
prefix[i + 1] = add(prefix[i], mul(pw1[i], s[i] - 'a' + 1));
}
int size() const
{
return prefix.size() - 1;
}
int getHash() const
{
return prefix.back();
}
int getRange(int l, int r) const
{
return mul(inv1[l], sub(prefix[r + 1], prefix[l]));
}
static void prepare()
{
pw1[0] = inv1[0] = prefixPW1[0] = 1;
int iv1 = fp(P1, mod - 2);
for(int i = 1; i < N; ++i)
{
pw1[i] = mul(pw1[i - 1], P1);
prefixPW1[i] = add(pw1[i], prefixPW1[i - 1]);
inv1[i] = mul(inv1[i - 1], iv1);
}
}
};
struct Node
{
int hash;
};
struct segTree
{
int len;
vector<Node> tree;
Node neutral = Node({0});
vector<int> lazy;
segTree(int n)
{
len = n;
int sz = 1;
while(sz < n)
sz *= 2;
tree = vector<Node>(2 * sz);
lazy = vector<int>(2 * sz, -1);
}
Node single(int s, int e, char val)
{
Node ret;
ret.hash = mul(val - 'a' + 1, pw1[s]);
return ret;
}
Node merge(Node a, Node b)
{
Node ret;
ret.hash = add(a.hash, b.hash);
return ret;
}
void propagate(int node, int s, int e)
{
if(lazy[node] == -1)
return;
tree[node].hash = mul(lazy[node], sub(prefixPW1[e], prefixPW1[s - 1]));
if(s != e)
{
lazy[node * 2] = lazy[node];
lazy[node * 2 + 1] = lazy[node];
}
lazy[node] = -1;
}
void build(int node, int s, int e, string &st)
{
if(s == e)
{
tree[node] = single(s, e, st[s - 1]);
return;
}
int mid = (s + e) >> 1;
build(node * 2, s, mid, st);
build(node * 2 + 1, mid + 1, e, st);
tree[node] = merge(tree[node * 2], tree[node * 2 + 1]);
}
void update(int node, int s, int e, int l, int r, int val)
{
propagate(node, s, e);
if(s > r || e < l)
return;
if(s >= l && e <= r)
{
tree[node].hash = mul(val, sub(prefixPW1[e], prefixPW1[s - 1]));
if(s != e)
{
lazy[node * 2] = val;
lazy[node * 2 + 1] = val;
}
lazy[node] = -1;
return;
}
int mid = (s + e) >> 1;
update(node * 2, s, mid, l, r, val);
update(node * 2 + 1, mid + 1, e, l, r, val);
tree[node] = merge(tree[node * 2], tree[node * 2 + 1]);
}
pair<Node, int> query(int node, int s, int e, Hash &tHash)
{
if(s > tHash.size())
return {neutral, 0};
propagate(node, s, e);
if(s == e)
{
auto A = tHash.getRange(s - 1, e - 1);
auto B = tree[node].hash;
B = mul(B, inv1[s]);
if(A == B)
return {tree[node], 1};
else
return {neutral, 0};
}
int m = (s + e) >> 1;
auto u = query(node * 2, s, m, tHash);
auto A = tHash.getRange(s - 1, m - 1);
auto B = u.first.hash;
B = mul(B, inv1[s]);
if(A == B)
{
auto v = query(2 * node + 1, m + 1, e, tHash);
return make_pair(merge(u.first, v.first), u.second + v.second);
}
else
return u;
}
Node query2(int node, int s, int e, int l, int r)
{
if(s > r || e < l)
return neutral;
propagate(node, s, e);
if(s >= l && e <= r)
return tree[node];
int m = (s + e) >> 1;
Node u = query2(node * 2, s, m, l, r);
Node v = query2(node * 2 + 1, m + 1, e, l, r);
return merge(u, v);
}
} st(0);
vector<Hash> v;
bool cmp(int idx)
{
Hash &t = v[idx];
auto x = st.query(1, 1, st.len, t);
int res = x.second + 1;
if(res <= min(st.len, t.size()))
{
auto A = t.getRange(res - 1, res - 1);
auto B = st.query2(1, 1, st.len, res, res).hash;
B = mul(B, inv1[res]);
if(A > B)
return false;
return true;
}
else
{
if(st.len > t.size())
return true;
return false;
}
}
int getAns()
{
int s = 0, e = v.size() - 1, mid, res = -1;
while(s <= e)
{
mid = (s + e) >> 1;
if(cmp(mid))
s = mid + 1, res = mid;
else
e = mid - 1;
}
return res + 1;
}
void doWork()
{
int n, q;
string str;
cin >> n >> q >> str;
st = segTree(str.length());
st.build(1, 1, str.length(), str);
vector<string> x(n);
for(int i = 0; i < n; i++)
cin >> x[i];
sort(x.begin(), x.end());
for(int i = 0; i < n; i++)
v.emplace_back(Hash(x[i]));
cout << getAns() << '\n';
char c;
int idx;
while(q--)
{
cin >> idx >> c;
st.update(1, 1, str.length(), idx, str.length(), c - 'a' + 1);
cout << getAns() << '\n';
}
}
signed main()
{
FIO
Hash::prepare();
int T = 1;
// cin >> T;
for(int i = 1; i <= T; i++)
doWork();
}
详细
Test #1:
score: 100
Accepted
time: 5ms
memory: 15256kb
input:
4 3 anatoly boris anatooo anbbbbu anba 5 o 3 b 7 x
output:
0 0 2 3
result:
ok 4 number(s): "0 0 2 3"
Test #2:
score: 0
Accepted
time: 10ms
memory: 15124kb
input:
5 5 abcde buz ababa build a aba 1 b 3 z 2 u 4 z 1 a
output:
3 3 3 4 4 1
result:
ok 6 numbers
Test #3:
score: 0
Accepted
time: 11ms
memory: 15212kb
input:
1 1 abababababababababababab ababababababababababababab 23 b
output:
0 1
result:
ok 2 number(s): "0 1"
Test #4:
score: 0
Accepted
time: 5ms
memory: 15128kb
input:
4 100 b dd ds ss sd 1 d 1 s 1 s 1 d 1 s 1 s 1 s 1 s 1 s 1 d 1 s 1 s 1 d 1 d 1 s 1 d 1 d 1 d 1 s 1 d 1 s 1 d 1 s 1 s 1 d 1 d 1 d 1 d 1 s 1 s 1 d 1 s 1 s 1 d 1 d 1 s 1 s 1 s 1 s 1 s 1 s 1 s 1 d 1 d 1 s 1 d 1 s 1 s 1 s 1 s 1 d 1 s 1 s 1 s 1 s 1 s 1 s 1 s 1 d 1 d 1 s 1 d 1 s 1 s 1 d 1 d 1 d 1 d 1 s 1 d ...
output:
0 0 2 2 0 2 2 2 2 2 0 2 2 0 0 2 0 0 0 2 0 2 0 2 2 0 0 0 0 2 2 0 2 2 0 0 2 2 2 2 2 2 2 0 0 2 0 2 2 2 2 0 2 2 2 2 2 2 2 0 0 2 0 2 2 0 0 0 0 2 0 2 2 0 0 2 2 2 0 2 0 0 2 2 2 2 0 0 0 0 2 2 0 2 2 2 2 0 2 2 2
result:
ok 101 numbers
Test #5:
score: 0
Accepted
time: 5ms
memory: 15128kb
input:
10 10 lvv lvvl lll ll vvll vl vllvv vll vllvl llvl vv 2 l 1 l 3 v 1 v 1 v 3 l 3 l 1 l 2 v 1 v
output:
3 1 1 2 10 10 9 9 1 3 10
result:
ok 11 numbers
Test #6:
score: 0
Accepted
time: 0ms
memory: 15180kb
input:
20 20 ffffqqqfqq fffqfff fq qqfqff fqfqqqf fqfqf fqfffffqfq fqffffq qfffqqfq f qq f fffffqq q qqqqffffq qfqqqfff ffqff qqfqfqf qqfq qqqqfqqqf ffqqfffqf 8 f 5 q 8 f 2 q 2 f 3 f 6 f 4 q 2 f 10 f 9 q 10 q 2 q 10 f 5 f 6 f 5 f 7 f 6 f 3 q
output:
3 3 3 3 11 2 2 2 4 2 2 2 2 11 11 11 11 11 11 11 11
result:
ok 21 numbers
Test #7:
score: 0
Accepted
time: 7ms
memory: 15208kb
input:
4 100 enf gggppp ppggpg pggpgp gppgpg 2 g 2 p 2 p 3 p 1 p 1 g 3 p 1 g 3 p 2 g 3 g 2 p 3 p 3 p 3 p 3 p 2 g 3 g 1 g 2 p 1 g 3 p 1 p 1 p 1 g 2 g 2 g 1 g 1 p 3 g 1 g 3 p 1 g 2 g 1 g 3 g 1 p 3 p 1 p 2 g 2 g 2 g 1 p 3 p 1 p 2 g 2 g 2 p 2 p 2 g 2 p 2 p 2 p 1 g 2 p 1 g 3 p 2 g 3 g 1 g 2 g 1 g 1 p 2 p 1 g 3 ...
output:
0 0 0 0 0 4 0 1 0 1 0 0 1 1 1 1 1 0 0 0 1 0 1 4 4 0 0 0 0 4 3 0 1 0 0 0 0 4 4 4 2 2 2 4 4 4 2 2 4 4 2 4 4 4 0 1 0 1 0 0 0 0 0 4 4 0 1 0 1 0 1 0 0 4 3 4 4 4 4 2 0 0 1 0 0 0 0 1 0 1 0 1 1 1 0 0 1 1 0 1 4
result:
ok 101 numbers
Test #8:
score: 0
Accepted
time: 7ms
memory: 15224kb
input:
50 50 yyy iyiyiyyyiiii yiiiyyyiiiyi iiiiiiyyyyiiiyiyii yiyyiyyiiy yyyiiyiyiiyiiiyyyyiyy iiyyyyyiiiiiiiiyyiyiyii iy iiyiyiiii yyiiiiiyyyy yiyiyiiiiiyiyyyiiyiy iiiiiyyyy yyiiiiyiiiyyiiy iiyyiyyiyyiiyyyyiiiiyiiyiiyiyii iiiiyyyiiyii i iiyyyyyyiiyiyii iiyiiyyiyyyyyiiiiyiy yyiyiyyyyiiyyiiyyiyyiyi yyyiiyiy...
output:
45 45 22 22 45 45 45 45 2 45 37 22 22 22 33 22 22 22 22 33 45 2 2 45 45 45 45 45 45 45 45 45 37 45 45 45 37 45 2 2 19 45 37 45 37 45 45 2 2 7 7
result:
ok 51 numbers
Test #9:
score: 0
Accepted
time: 7ms
memory: 15272kb
input:
250 250 sbabbsb bsbbb asssaasbssa sbaabbabaasbabaaabasaasbbsabbaaasasbsbbbaabs sasasbbbbaassssabssaabasababssaaabaabssbsass b basasabbsbasaasbabsabbsabassbabssbasbbsaa ssbassbaasbsabssssssasbsassabsasbsbsbsaasbsb baabbsaabsbbassasasssbabsaaabababbsb sababssaabababaaa sbba basaassbbsbaaaasbssbbbsbsbb...
output:
203 203 201 209 2 8 2 4 2 2 48 48 42 138 2 13 13 13 13 13 13 13 13 48 48 2 13 29 24 24 29 29 21 21 90 250 249 250 209 209 209 209 209 209 209 250 242 242 250 250 249 209 213 171 171 209 209 209 209 209 209 171 250 2 2 29 16 21 21 14 14 2 8 8 8 48 48 48 90 90 76 2 2 6 2 16 29 27 29 2 13 29 29 29 29 2...
result:
ok 251 numbers
Test #10:
score: 0
Accepted
time: 13ms
memory: 18192kb
input:
2500 2500 xdwxxxdxwxdwdxdwwwwdwxdxdwwxdxwwdxxdwwwdxxxxxdddwwwxdwxdxxxdwdwxxwwwwxwxdxwxdxdxwdxdxxdwdwxwwxddwwxdddddxxxddxwwxwxwxddwwxdwdxwxdxwwxdddwxwxwwddxdxdddwxwdwwdwwdwwxwdw wwwwxwdxdxddwxwwdwwxxddxxw w wdxwxxxxwdwxdwdxxwwdxxxwwxwwxdwxwddddxwdwdxwdddxxdxddwwxddddxxxxdxwdwdwdxxxwdxwxxdwddwddwxwxww...
output:
1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1854 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1855 1254 1254 1254 1254 ...
result:
ok 2501 numbers
Test #11:
score: 0
Accepted
time: 224ms
memory: 18200kb
input:
10000 100000 bbbbjbbrrbjrjr brjbrbjbjbrbjrbrjrbjbjrrjjbrjrjbjjrjr bbjjbbjrbbbbrjjrbbbbrrbrjrbrbjjbbjbbbj jbbrjbjbjjbbbjbrbbrbjbbrrbrbrrbrrbjbjjjrbjrrrrrbjbbjrrjjbrjjrbrrrbb b jjbrjrjrrrjbbbrjjbjjjbrrbbjjjjjjbrrbjjbrrrjrrjrbjrrbbjbrjj jbjrbbrbjjjjbrbbrbjbrbjjbjrbbjjrjrbrjjbjjbbjjbbjjjbrbrrb jjrbbrbrr...
output:
91 91 90 90 90 90 91 91 91 91 99 99 5034 5034 5033 5045 5034 5034 5034 5034 5034 5034 59 59 59 59 59 59 59 59 60 10000 9999 8383 8383 8383 10000 5034 5034 3396 3511 4524 4524 4480 4482 4482 3396 3396 3413 3413 3413 3396 10000 9997 9997 9997 9997 9997 8889 8383 8383 8383 8390 7812 8383 8385 8383 8383...
result:
ok 100001 numbers
Test #12:
score: 0
Accepted
time: 38ms
memory: 17924kb
input:
31313 31313 plg pqgqgaglaal lag lagqgqppqap agpgglg aaaaglgpqg pap lggap llgaqqqpgpqg ppllllaqgaqg lalglplp al laglag aaq qalaapppg p lqg gpalqq apllpqqp qp qqgaqpgg lqqg qp qlgqagpgggg gagqaaqp lqllpplaalpl ag p qpppqaaal qppgqaalqaa lqql la qaglaa glpapqlagqlp aaaqagaqqql pggqgqlqlpq qlqggg g lagl...
output:
21893 8285 10985 10985 9667 23496 23496 20763 20975 31092 28391 31092 30496 30694 8285 10985 11197 16009 15597 16009 675 6114 5930 16009 31092 27062 26864 23496 23053 23496 24821 16009 18619 13220 31092 29755 16009 16009 16009 18619 18418 18005 16009 17331 18619 14635 14635 15280 8285 8716 8285 675 ...
result:
ok 31314 numbers
Test #13:
score: 0
Accepted
time: 198ms
memory: 45392kb
input:
313131 131313 ysy ys gsg i g s isi i i yia say g ig ig aai a ag aga ig ag ya gyi is agy g yi gg gga ia ag yy ggg sa y sgs aa gai i i s ia aii sag si a a i a i iig sss i ig gys gg i igy y gg gg ya yaa aii yi ss iag say siy iy gss gi y a sy g aa ai si a as ia iyg sy sga gs sis ys iyy iya s g y ii sis ...
output:
304123 312262 312262 275523 303314 303314 312262 275523 276335 293975 312262 303314 302486 25081 61655 58283 58283 43370 43370 61655 43370 52466 25081 25887 312262 284732 285545 284732 240474 240474 312262 96576 240474 239617 238827 237997 212894 240474 240474 240474 96576 124117 87296 240474 240474...
result:
ok 131314 numbers
Test #14:
score: 0
Accepted
time: 697ms
memory: 100932kb
input:
1000000 1000000 s v c g g w q p o x b r q t l w a e v a d i i u i b m x d x p f d d h j o p y x r h d w n w z i z r j w j j y s m j e z k m p j k o h d z i k h w k h v i r b z x a v b y r a h y z v l u z y e e s z z w p t d b h b h m q v b e f d r q t n i t u f q h p d m x b k k d z c l q w o x b j ...
output:
692520 807920 769459 538901 884904 500731 269604 538901 192697 38637 500731 154156 731303 423808 846522 231505 154156 654155 347013 846522 731303 76958 769459 115440 961752 884904 385537 884904 115440 154156 385537 731303 423808 615645 38637 231505 347013 192697 192697 231505 500731 385537 807920 19...
result:
ok 1000001 numbers
Test #15:
score: -100
Time Limit Exceeded
input:
149439 1000000 pkmzs zmzmzsp zspzzmz msszk szkmsk mpspkk kzzspzs kkspmss kkskmpp pkksk szsmkpk kpppspp kppzks skkmksp kzzspsm pkpkksp kkkpzzs kzssmkm psspsz smskk kzssmsp mszkppk kzpskzp kmmpsks pzkzzks pkppmkk mmszspz kmpkmpm mkkpsmp kmkzmpz kzzpskz zk kkppspk mmmkmzk zmssmp zzpkmk pkzzzpp ssskpmz ...
output:
69506 45944 60867 60650 9 18393 20218 19522 19575 45944 66612 66179 66101 85024 94244 90581 149402 137913 140215 139531 139642 9 27575 22131 23213 23090 18393 22063 21834 85024 88696 87275 87398 149402 126424 127297 127154 9 9187 14606 13568 13511 120639 114893 116035 116260 116187 45944 53708 51904...