QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#389996 | #1823. Permutation CFG | black_moonrise# | WA | 436ms | 101864kb | C++14 | 3.3kb | 2024-04-14 23:27:42 | 2024-04-14 23:27:42 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int MAXP = 4e6 + 5;
const int P = 1e9 + 7;
typedef long long ll;
int inv[7];
int power(int x, int y) {
if (y == 0) return 1;
int tmp = power(x, y / 2);
if (y % 2 == 0) return 1ll * tmp * tmp % P;
else return 1ll * tmp * tmp % P * x % P;
}
struct SegmentTree {
struct Node {
int lc, rc;
int ret[6], cnt[6];
} a[MAXP];
int size, n;
void init(int x) {
n = x;
}
void update(int root) {
for (int i = 0; i < 6; i++) {
a[root].cnt[i] = min(a[a[root].lc].cnt[i] + a[a[root].rc].cnt[i], P);
a[root].ret[i] = (a[a[root].lc].ret[i] + a[a[root].rc].ret[i]) % P;
}
}
int modify(int root, int l, int r, int pos, int v) {
int ans = ++size;
a[ans] = a[root];
int tret = 1;
int tcnt = 1;
for (int i = 0; i < 6; i++) {
a[ans].cnt[i] = min(a[ans].cnt[i] + tcnt, P);
a[ans].ret[i] = (a[ans].ret[i] + tret) % P;
tcnt = min(1ll * tcnt * (v + i) / (i + 1), (ll) P);
tret = 1ll * tret * (v + i) % P * inv[i + 1] % P;
}
if (l == r) return ans;
int mid = (l + r) / 2;
if (mid >= pos) a[ans].lc = modify(a[ans].lc, l, mid, pos, v);
else a[ans].rc = modify(a[ans].rc, mid + 1, r, pos, v);
update(ans);
return ans;
}
int modify(int root, int pos, int v) {
return modify(root, 1, n, pos, v);
}
pair <int, int> query(int root, int l, int r, int k, int s) {
if (l == r) return make_pair(l, 0);
int mid = (l + r) / 2;
if (a[a[root].lc].cnt[s] > k) return query(a[root].lc, l, mid, k, s);
else {
pair <int, int> tmp = query(a[root].rc, mid + 1, r, k - a[a[root].lc].cnt[s], s);
tmp.second += a[a[root].lc].cnt[s];
return tmp;
}
}
pair <int, int> query(int root, int k, int s) {
if (k >= a[root].cnt[s]) return make_pair(n + 1, a[root].cnt[s]);
else return query(root, 1, n, k, s);
}
int conv[6];
int sum(int root, int l, int r, int qr, int s) {
if (qr >= r) {
int ans = 0;
for (int i = 0; i <= s; i++)
ans = (ans + 1ll * conv[i] * a[root].ret[s - i]) % P;
return ans;
}
if (l > qr) return 0;
int mid = (l + r) / 2;
return (sum(a[root].lc, l, mid, qr, s) + sum(a[root].rc, mid + 1, r, qr, s)) % P;
}
int sum(int root, int r, int x, int s) {
if (r <= 0) return 0;
int tmp = 1;
for (int i = 0; i <= s; i++) {
conv[i] = tmp;
tmp = 1ll * tmp * (P - x - i) % P * inv[i + 1] % P;
}
return sum(root, 1, n, r, s);
}
} ST;
int n, s, q, a[MAXN], p[MAXN], root[MAXN];
int query(int rt, int qr, int x, int s) {
if (s == 0) return rt >= x && p[x] <= qr;
else return ST.sum(root[rt], qr, x - 1, s - 1);
}
int main() {
scanf("%d%d%d", &n, &s, &q);
for (int i = 1; i <= s; i++)
inv[i] = power(i, P - 2);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
p[a[i]] = i;
}
ST.init(n);
for (int i = 1; i <= n; i++)
root[i] = ST.modify(root[i - 1], p[i], i);
for (int i = 1; i <= q; i++) {
int k, x; scanf("%d%d", &x, &k);
int rt = n, ans = 0;
for (int j = s - 1; j >= 0; j--) {
pair <int, int> tmp;
tmp = ST.query(root[rt], k, j);
ans = (ans + query(rt, tmp.first - 1, x, j)) % P;
ans = (ans - query(x - 1, tmp.first - 1, x, j) + P) % P;
if (tmp.first > n) break;
else {
rt = a[tmp.first];
k -= tmp.second;
}
}
printf("%d\n", ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 6012kb
input:
4 3 6 1 4 3 2 1 6 2 20 4 1 3 5 2 9 1 16
output:
3 6 0 1 2 8
result:
ok 6 numbers
Test #2:
score: 0
Accepted
time: 416ms
memory: 101864kb
input:
100000 5 200000 98129 52962 60307 83804 75634 55811 85666 40965 78529 40850 54600 83058 37001 92436 30323 54632 24238 77437 87162 75991 39863 74990 32168 99841 85430 66056 69893 7561 60704 40795 81535 89902 44331 20995 99461 93620 91817 97370 84025 98836 2455 77447 37078 90192 26249 84337 70311 4006...
output:
20217 9885 20204 20217 20218 20217 20217 727 20202 20218 20218 20214 20217 7790 20217 20217 20200 20217 20218 20217 20218 20218 20218 20217 20202 20217 20218 20218 5609 8419 20218 20200 20218 20216 11843 20218 20217 20207 20217 935 20203 5909 20218 20217 20218 20200 20217 20213 20203 20207 5654 4087...
result:
ok 200000 numbers
Test #3:
score: 0
Accepted
time: 422ms
memory: 101684kb
input:
100000 5 200000 59767 75967 97372 50276 20532 77164 30969 40418 84175 87555 79967 28404 26422 22441 4543 72719 9318 93986 12744 88814 25854 93658 9042 41389 24133 60155 80340 44920 58271 50431 92622 28573 30633 318 43850 78809 69750 67699 17767 8454 2543 26572 52552 77502 24901 94119 87156 19368 394...
output:
33305 33330 19455 33332 25500 1777 33332 33310 33337 33333 33297 33331 33337 33330 33317 33304 33332 33336 33331 33337 33335 5611 33334 33337 33333 33331 33337 33307 33334 22446 31995 27049 33337 5351 32269 33332 33334 33331 33299 33331 33334 24072 21451 33331 33338 33332 33331 33331 33319 23356 333...
result:
ok 200000 numbers
Test #4:
score: 0
Accepted
time: 393ms
memory: 99736kb
input:
97997 5 200000 92883 53079 12146 7142 47500 47118 60176 54625 8908 93576 33824 61522 79201 68956 25790 76970 63243 10575 33345 20943 77076 40068 30980 20739 90036 57454 38446 49088 90613 27293 39453 52577 94545 7934 73793 6201 33713 91255 45678 63783 65019 35224 65407 39863 38120 79943 69106 76540 6...
output:
21513 21504 21512 21513 21512 19613 21418 21511 21512 6191 21512 18462 4097 21511 21511 21510 21499 21502 21513 21512 21499 21512 21512 21513 21512 21512 21501 21512 10071 21513 21510 21511 21513 21512 21511 21498 21512 21511 21497 21513 21512 21512 21513 21512 21491 21513 16589 21508 21511 21500 20...
result:
ok 200000 numbers
Test #5:
score: -100
Wrong Answer
time: 436ms
memory: 99684kb
input:
97997 5 200000 1 2 3 4 5 6 7 8 9 10 97997 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 ...
output:
36269 44641 44708 44806 44700 45477 39594 44806 44405 45026 43915 44803 44706 28640 44655 44062 45477 22483 18051 44678 44841 44686 44695 44645 44930 44763 44700 35460 37180 43835 18503 40508 33475 45143 44763 44665 41482 44862 44633 44754 41636 45291 43119 42976 40377 41790 45709 43725 44184 38340 ...
result:
wrong answer 4th numbers differ - expected: '44743', found: '44806'