QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#711992 | #7949. K-Lottery | kizen# | WA | 1507ms | 82500kb | C++20 | 4.2kb | 2024-11-05 14:13:41 | 2024-11-05 14:13:42 |
Judging History
answer
#include <bits/stdc++.h>
#ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#endif
#define sz(v) ((int)v.size())
#define all(v) v.begin(), v.end()
#define pb push_back
#define pi array<int, 2>
#define comp(v) (sort(all(v)), v.erase(unique(all(v)), v.end()))
#define lb(v, x) (lower_bound(all(v), x) - v.begin())
using namespace std;
#define int long long
const pi v1 = {3, 805306457};
// const pi v1 = {97, 805306457};
struct Seg{
int n;
vector<int> tr;
Seg(int m){
n = m + 4;
tr.resize(n * 4 + 4);
}
void push(int x, int s, int e, int pos, int val){
if(s == e){
tr[x] = val;
return;
}
int m = s + e >> 1;
if(pos <= m) push(x * 2, s, m, pos, val);
else push(x * 2 + 1, m + 1, e, pos, val);
tr[x] = (tr[x * 2] + tr[x * 2 + 1]) % v1[1];
}
int get(int x, int s, int e, int fs, int fe){
if(fe < s || fs > e || fs > fe) return 0;
if(fs <= s && fe >= e) return tr[x];
int m = s + e >> 1;
return (get(x * 2, s, m, fs, fe) + get(x * 2 + 1, m + 1, e, fs, fe)) % v1[1];
}
};
int pw(int x, int y, int md){
if(!y) return 1;
if(y == 1) return x % md;
int v = pw(x, y >> 1, md);
(v *= v) %= md;
if(y & 1) (v *= x) %= md;
return v;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int k, m, n; cin >> k >> m >> n;
vector<vector<int>> a(m, vector<int>(k));
for(int i = 0; i < m; ++i){
for(int j = 0; j < k; ++j){
cin >> a[i][j]; --a[i][j];
}
}
vector<int> hsh(m + 1);
for(int i = 0; i < m; ++i){
Seg tr(k + 4);
for(int j = 0; j < k; ++j){
int rv = tr.get(1, 0, k - 1, a[i][j], k - 1);
(rv *= pw(v1[0], j, v1[1])) %= v1[1];
(hsh[i] += rv) %= v1[1];
tr.push(1, 0, k - 1, a[i][j], pw(v1[0], j * k, v1[1]));
}
}
vector<int> p(n);
for(int i = 0; i < n; ++i){
cin >> p[i];
}
auto srt = p;
comp(srt);
for(int i = 0; i < n; ++i){
p[i] = lb(srt, p[i]);
}
Seg tr(n + 4), rt(n + 4);
for(int j = 0; j < k; ++j){
int rv = tr.get(1, 0, n - 1, p[j], n - 1);
(rv *= pw(v1[0], j, v1[1])) %= v1[1];
(hsh[m] += rv) %= v1[1];
tr.push(1, 0, n - 1, p[j], pw(v1[0], j * k, v1[1]));
rt.push(1, 0, n - 1, p[j], pw(v1[0], j, v1[1]));
}
vector<int> id(m);
iota(all(id), 0);
sort(all(id), [&](int x, int y){return hsh[x] < hsh[y];});
int inv1 = pw(v1[0], v1[1] - 2, v1[1]);
int inv1k1 = pw(inv1, k + 1, v1[1]);
int inv1k = pw(inv1, k, v1[1]);
int k1 = pw(v1[0], k - 1, v1[1]);
int inv1kinv = pw(inv1k, v1[1] - 2, v1[1]);
// for(int i = 0; i <= m; ++i){
// cout << hsh[i] << endl;
// }
int trgop = 1, rtgop = 1;
int trgopinv = 1, rtgopinv = 1;
for(int i = 0; i + k <= n; ++i){
int pos = lower_bound(id.begin(), id.end(), m, [&](int x, int y){return hsh[x] < hsh[y];}) - id.begin();
if(pos < sz(id) && hsh[id[pos]] == hsh[m]){
for(int j = 0; j < k; ++j){
cout << a[id[pos]][j] + 1 << ' ';
}
return 0;
}
if(i + k < n){
rt.push(1, 0, n - 1, p[i], 0);
tr.push(1, 0, n - 1, p[i], 0);
// cout << hsh[m] << ' ';
(hsh[m] += v1[1] - rt.get(1, 0, n - 1, 0, p[i] - 1) * rtgop % v1[1]) %= v1[1];
// cout << hsh[m] << ' ';
(hsh[m] *= inv1k1) %= v1[1];
(trgop *= inv1k) %= v1[1];
(rtgop *= inv1) %= v1[1];
(trgopinv *= inv1kinv) %= v1[1];
(rtgopinv *= v1[0]) %= v1[1];
// cout << hsh[m] << ' ';
int rv = tr.get(1, 0, n - 1, p[i + k], n - 1) * trgop % v1[1];
(rv *= k1) %= v1[1];
(hsh[m] += rv) %= v1[1];
tr.push(1, 0, n - 1, p[i + k], pw(k1, k, v1[1]) * trgopinv % v1[1]);
rt.push(1, 0, n - 1, p[i + k], k1 * rtgopinv % v1[1]);
// cout << endl;
}
}
cout << 0;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3544kb
input:
3 2 10 1 2 3 1 3 2 20 35 10 7 99 53 72 33 88 16
output:
1 3 2
result:
ok single line: '1 3 2 '
Test #2:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
4 5 10 1 2 3 4 1 2 4 3 3 4 1 2 4 1 2 3 4 2 3 1 19 31 9 1 89 48 63 30 78 12
output:
4 2 3 1
result:
ok single line: '4 2 3 1 '
Test #3:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
3 3 7 1 3 2 2 3 1 2 1 3 11 22 33 44 55 66 77
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 875ms
memory: 82368kb
input:
10000 10 1000000 1 5001 2 5002 3 5003 4 5004 5 5005 6 5006 7 5007 8 5008 9 5009 10 5010 11 5011 12 5012 13 5013 14 5014 15 5015 16 5016 17 5017 18 5018 19 5019 20 5020 21 5021 22 5022 23 5023 24 5024 25 5025 26 5026 27 5027 28 5028 29 5029 30 5030 31 5031 32 5032 33 5033 34 5034 35 5035 36 5036 37 5...
output:
1 5001 2 5002 3 5003 4 5004 5 5005 6 5006 7 5007 8 5008 9 5009 10 5010 11 5011 12 5012 13 5013 14 5014 15 5015 16 5016 17 5017 18 5018 19 5019 20 5020 21 5021 22 5022 23 5023 24 5024 25 5025 26 5026 27 5027 28 5028 29 5029 30 5030 31 5031 32 5032 33 5033 34 5034 35 5035 36 5036 37 5037 38 5038 39 50...
result:
ok single line: '1 5001 2 5002 3 5003 4 5004 5 ...4998 9998 4999 9999 5000 10000 '
Test #5:
score: 0
Accepted
time: 1507ms
memory: 82424kb
input:
10000 10 1000000 7171 4541 8189 3253 6694 2078 7384 1786 7847 5040 953 4126 4806 3532 7875 8531 3543 2706 8565 1509 2092 4125 6110 5251 6314 4574 6726 8900 9328 8639 3990 5234 9012 5023 3289 2825 8038 3593 2249 337 6252 3831 7967 3839 2815 540 3754 1009 8772 2939 2845 5067 6587 2615 375 7252 9940 18...
output:
0
result:
ok single line: '0'
Test #6:
score: 0
Accepted
time: 873ms
memory: 82500kb
input:
10000 10 1000000 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 9...
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 1483ms
memory: 82424kb
input:
10000 10 1000000 3 7 8 1 4 10 2 5 9 6 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 9...
output:
0
result:
ok single line: '0'
Test #8:
score: 0
Accepted
time: 1479ms
memory: 82372kb
input:
10000 10 1000000 1 3 7 10 6 4 9 8 2 5 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 9...
output:
0
result:
ok single line: '0'
Test #9:
score: -100
Wrong Answer
time: 654ms
memory: 82180kb
input:
100 1000 1000000 34 82 39 96 30 85 83 16 8 31 51 63 76 49 3 7 58 17 42 94 41 9 11 47 89 6 86 19 100 77 37 93 40 15 36 71 23 46 48 55 2 92 64 18 22 21 12 72 1 99 70 81 56 57 53 62 50 35 65 43 73 60 97 24 4 98 20 90 26 68 66 25 74 87 14 67 45 95 33 32 52 29 5 88 54 13 91 10 59 80 28 38 78 61 79 75 69 ...
output:
50 43 33 13 4 12 49 2 5 1 77 75 34 59 44 64 20 45 47 86 90 96 14 17 15 62 46 9 67 39 71 95 16 54 26 84 92 65 73 30 80 52 53 55 93 97 61 79 3 29 11 58 78 37 25 36 69 35 72 21 51 41 89 66 22 98 100 38 82 88 27 70 81 56 57 24 7 74 87 28 91 63 18 23 6 99 48 60 32 42 19 8 94 85 10 68 40 76 83 31
result:
wrong answer 1st lines differ - expected: '0', found: '50 43 33 13 4 12 49 2 5 1 77 7...2 19 8 94 85 10 68 40 76 83 31 '