QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#688325 | #7949. K-Lottery | tamthegod | TL | 1899ms | 81880kb | C++23 | 5.1kb | 2024-10-30 03:16:45 | 2024-10-30 03:16:46 |
Judging History
answer
#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int k, m, n;
int a[maxN];
vector<vector<int>> vc;
int id[maxN];
const int base = 237;
int pw[maxN];
unordered_map<int, int> mp;
int inv[maxN];
int poww(int k, int n)
{
int res = 1;
while(n)
{
if(n & 1) res = (1ll * res * k) % mod;
k = (1ll * k * k) % mod;
n /= 2;
}
return res;
}
void ReadInput()
{
pw[0] = 1;
for(int i=1; i<maxN; i++)
pw[i] = (1ll * pw[i - 1] * base) % mod;
cin >> k >> m >> n;
vc.resize(m + 5, vector<int> (k + 5));
for(int i=1; i<=m; i++)
for(int j=1; j<=k; j++)
cin >> vc[i][j];
for(int i=1; i<=m; i++)
{
for(int j=1; j<=k; j++)
id[vc[i][j]] = j;
int val = 0;
for(int j=1; j<=k; j++)
val = (val + 1ll * id[j] * pw[j - 1] % mod) % mod;
mp[val] = i;
}
vector<pair<int, int>> val;
for(int i=1; i<=n; i++)
{
cin >> a[i];
val.pb({a[i], i});
}
sort(val.begin(), val.end());
for(int i=1; i<=n; i++)
a[val[i - 1].se] = i;
int tmp = poww(base, mod - 2);
inv[0] = 1;
for(int i=1; i<=n; i++)
inv[i] = (1ll * inv[i - 1] * tmp) % mod;
}
int st[4 * maxN], lazy[4 * maxN];
inline void refind(int id)
{
st[id] = st[id * 2] + st[id * 2 + 1];
if(st[id] >= mod) st[id] -= mod;
}
inline void down(int id)
{
int t = lazy[id];
if(!t) return;
if(t > 0)
{
st[id * 2] = (1ll * st[id * 2] * pw[t]) % mod;
st[id * 2 + 1] = (1ll * st[id * 2 + 1] * pw[t]) % mod;
}
else if(t < 0)
{
st[id * 2] = (1ll * st[id * 2] * inv[-t]) % mod;
st[id * 2 + 1] = (1ll * st[id * 2 + 1] * inv[-t]) % mod;
}
lazy[id * 2] += t;
lazy[id * 2 + 1] += t;
lazy[id] = 0;
}
void update(int id, int l, int r, int pos, int val)
{
if(l == r && l == pos)
{
st[id] = val;
return;
}
down(id);
int mid = (l + r) / 2;
if(pos <= mid) update(id * 2, l, mid, pos, val);
else update(id * 2 + 1, mid + 1, r, pos, val);
refind(id);
}
void range_upd(int id, int l, int r, int u, int v, int val)
{
if(l > v || r < u) return;
if(l >= u && r <= v)
{
if(val > 0) st[id] = (1ll * st[id] * pw[val]) % mod;
else if(val < 0) st[id] = (1ll * st[id] * inv[-val]) % mod;
lazy[id] += val;
return;
}
int mid = (l + r) / 2;
down(id);
range_upd(id * 2, l, mid, u, v, val);
range_upd(id * 2 + 1, mid + 1, r, u, v, val);
refind(id);
}
int bit[maxN];
inline void update(int pos, int val)
{
for(; pos <= n; pos += (pos & (-pos)))
bit[pos] += val;
}
inline int get(int pos)
{
int res = 0;
for(; pos; pos -= (pos & (-pos)))
res += bit[pos];
return res;
}
bool chk(int id, int t)
{
vector<int> tmp;
for(int i=t-k+1; i<=t; i++)
tmp.pb(a[i]);
sort(tmp.begin(), tmp.end());
vector<int> val;
for(int i=t-k+1; i<=t; i++)
val.pb(upper_bound(tmp.begin(), tmp.end(), a[i]) - tmp.begin());
for(int i=1; i<=k; i++)
if(val[i - 1] != vc[id][i]) return false;
return true;
}
void Solve()
{
int cur = 0;
int tmp = 0;
for(int i=0; i<k; i++)
{
tmp += pw[i];
if(tmp >= mod) tmp -= mod;
}
for(int i=1; i<=n; i++)
{
if(i > k)
{
update(1, 1, n, a[i - k], 0);
update(a[i - k], -1);
if(a[i - k] < a[i]) range_upd(1, 1, n, a[i - k] + 1, a[i], -1);
else if(a[i - k] > a[i]) range_upd(1, 1, n, a[i] + 1, a[i - k], 1);
}
update(a[i], 1);
int val = get(a[i] - 1);
if(i <= k)
range_upd(1, 1, n, a[i] + 1, n, 1);
update(1, 1, n, a[i], (1ll * i * pw[val]) % mod);
int res = st[1];
if(i >= k)
{
res -= cur;
if(res < 0) res += mod;
if(mp[res])
{
if(chk(mp[res], i))
{
int i = mp[res];
for(int j=1; j<=k; j++)
cout << vc[i][j] << " ";
return;
}
}
cur += tmp;
if(cur >= mod) cur -= mod;
}
}
cout << 0;
}
#define taskname "sol"
int32_t main()
{
if (fopen(taskname ".inp", "r"))
{
freopen(taskname ".inp", "r", stdin);
//freopen(taskname ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
//cin >> T;
for(int itest=1; itest<=T; itest++)
{
ReadInput();
Solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 6ms
memory: 11820kb
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: 6ms
memory: 11836kb
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: 6ms
memory: 11900kb
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: 953ms
memory: 78004kb
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: 1784ms
memory: 81880kb
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: 640ms
memory: 40680kb
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: 1899ms
memory: 81080kb
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: -100
Time Limit Exceeded
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