QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#584008 | #1913. Non-Decreasing Subsequences | makrav | 8.333333 | 196ms | 14192kb | C++20 | 3.4kb | 2024-09-23 03:37:15 | 2024-09-23 03:37:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define sc second
const int mod = 1e9 + 7;
struct fenwick {
int n;
vector<int> t;
fenwick() = default;
fenwick(int n_) {
n = n_;
t.assign(n + 1, 0);
}
void clear() {
t.assign(n + 1, 0);
}
void upd(int p, int vl) {
for (; p <= n; p = (p | (p + 1))) {
t[p] += vl;
if (t[p] >= mod) t[p] -= mod;
}
}
int get(int p) {
int sm = 0;
for (; p > 0; p = (p & (p + 1)) - 1) {
sm += t[p];
if (sm >= mod) sm -= mod;
}
return sm;
}
};
void solve() {
int n, k; cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int q; cin >> q;
vector<vector<pair<int, int>>> qrs(n);
vector<int> answ(q);
for (int i = 0; i < q; i++) {
int l, r; cin >> l >> r;
l--; r--;
qrs[l].pb({r, i});
}
for (int i = 0; i < n; i++) sort(all(qrs[i]));
vector<vector<int>> dp(n, vector<int>(k + 1));
vector<fenwick> fw_right(k + 1), fw_left(k + 1);
for (int i = 1; i <= k; i++) {
fw_right[i] = fenwick(k + 2);
fw_left[i] = fenwick(k + 2);
}
auto solve = [&](int l, int r, auto&&self) -> void {
if (l + 1 == r) {
for (auto &u : qrs[l]) {
answ[u.second] = 2;
}
qrs[l].clear();
return;
}
int tm = (l + r) / 2;
for (int i = 1; i <= k; i++) {
fw_right[i].clear(); fw_left[i].clear();
}
for (int i = tm; i < r; i++) {
for (int j = 1; j <= a[i]; j++) {
fw_right[j].upd(a[i], fw_right[j].get(a[i]));
if (j == a[i]) fw_right[j].upd(a[i], 1);
}
for (int j = 1; j <= k; j++) dp[i][j] = fw_right[j].get(k);
}
for (int i = tm - 1; i >= l; i--) {
for (int j = a[i]; j <= k; j++) {
fw_left[j].upd(a[i], fw_left[j].get(k) - fw_left[j].get(a[i] - 1));
if (j == a[i]) fw_left[j].upd(a[i], 1);
}
for (int j = 1; j <= k; j++) dp[i][j] = fw_left[j].get(k);
}
for (int i = l; i < tm; i++) {
while (!qrs[i].empty() && qrs[i].back().first < r) {
int rg = qrs[i].back().first;
int sm = 0, ans = 1;
for (int j = 1; j <= k; j++) {
ans += dp[rg][j] + dp[i][j];
ans %= mod;
sm += dp[i][j];
if (sm >= mod) sm -= mod;
ans += dp[rg][j] * 1ll * sm;
ans %= mod;
}
answ[qrs[i].back().second] = ans;
qrs[i].pop_back();
}
}
self(l, tm, self);
self(tm, r, self);
};
solve(0, n, solve);
for (auto &u : answ) cout << u << '\n';
}
signed main() {
int tt = 1;
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
cin >> tt;
#else
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#endif
while (tt--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 8.33333
Accepted
time: 0ms
memory: 3628kb
input:
5 2 1 2 1 1 2 3 2 3 4 5 1 5
output:
3 4 20
result:
ok 3 lines
Test #2:
score: 0
Wrong Answer
time: 183ms
memory: 14136kb
input:
50000 20 1 5 20 6 15 5 4 17 6 16 20 13 2 18 17 2 16 12 12 3 1 10 13 1 14 12 14 2 8 2 3 20 7 2 17 13 18 20 9 15 16 20 7 9 18 3 3 5 6 14 7 18 15 19 18 9 2 12 10 10 5 12 1 3 5 18 8 2 9 16 8 16 8 15 17 5 9 19 9 15 12 7 4 7 17 2 15 19 5 16 20 9 19 20 4 3 9 11 16 10 18 3 5 5 17 1 1 18 11 9 4 3 7 7 1 4 20 ...
output:
115668749 -422079652 287296502 104810640 843262986 362706671 366696293 -32992347 -847103020 567730593 -975727729 -876290180 -515511350 597012516 125473427 -203519727 -960569718 563971421 -134866097 772424945 316718326 -657988651 543981923 87023067 -794940291 -142611749 -783261510 -462375447 -2462530...
result:
wrong answer 1st lines differ - expected: '648477307', found: '115668749'
Test #3:
score: 0
Wrong Answer
time: 196ms
memory: 14192kb
input:
50000 20 12 10 2 6 1 3 11 11 13 20 3 16 20 16 13 18 3 19 18 18 1 15 16 7 9 9 3 9 15 15 20 6 16 13 11 16 15 13 19 19 4 1 7 4 8 11 1 11 9 19 20 9 5 15 7 13 3 9 13 9 15 4 6 11 9 16 18 15 1 16 14 4 8 12 19 16 2 20 6 10 10 17 18 14 11 16 18 6 4 10 14 11 13 20 1 1 7 18 16 19 6 1 15 13 12 13 20 5 4 17 14 5...
output:
-157397286 -547078751 771009432 -176298388 -111854053 825616104 15518491 -221441937 -234851991 940550750 -709545345 555877451 -432111891 670838869 -117526983 -97454006 -743560033 394873584 436022323 -949183654 -78231040 778952162 984288908 571040945 33681806 192538987 347465131 650145218 -122163929 ...
result:
wrong answer 1st lines differ - expected: '510482945', found: '-157397286'
Test #4:
score: 0
Wrong Answer
time: 188ms
memory: 14120kb
input:
50000 20 16 12 3 14 15 20 3 16 13 10 12 10 20 14 17 2 12 8 20 6 7 4 12 15 14 13 5 7 11 3 3 6 6 17 12 20 16 14 16 8 4 19 17 15 4 6 16 15 13 8 12 12 3 4 18 16 8 2 3 18 16 17 4 1 14 15 12 9 20 7 9 3 17 17 10 1 2 17 7 7 4 19 18 6 14 7 14 1 20 8 19 15 4 14 7 9 20 18 10 19 17 10 14 13 6 3 5 20 19 4 6 15 1...
output:
-979962569 -122508081 805884438 637262055 298198926 412508248 -71724101 -275376927 -226379281 -615979386 803799421 846468628 -160481154 -60204555 98813266 240926127 270719450 -43939938 969562346 318421866 911644493 -888349699 187675830 178401662 827028680 -570386239 -912799543 -807341275 -194945212 ...
result:
wrong answer 1st lines differ - expected: '637280489', found: '-979962569'
Test #5:
score: 0
Wrong Answer
time: 57ms
memory: 6736kb
input:
1000 20 15 3 13 7 4 11 8 18 3 3 2 18 11 5 15 16 4 19 7 6 11 18 11 11 8 16 13 1 3 12 19 17 6 4 15 10 6 3 19 8 17 20 17 7 16 11 14 20 1 13 5 11 10 7 14 9 2 6 10 4 9 20 12 15 15 7 16 20 9 14 19 17 6 15 3 1 18 9 20 10 13 4 1 2 3 14 11 4 11 12 20 19 3 3 13 18 9 8 9 9 14 8 5 19 14 20 11 3 20 11 13 12 6 5 ...
output:
-818530669 648810113 28944914 309327725 -685753921 395324111 7052687 -815927267 -605010163 -184274576 -405312593 594695182 19667 -794790178 865697038 -811555289 724283422 -203894261 528218797 -65498831 298838703 275123195 -846814386 607509701 770633892 127699836 7827678 -466435322 862049565 57700511...
result:
wrong answer 1st lines differ - expected: '555039665', found: '-818530669'
Test #6:
score: 0
Wrong Answer
time: 54ms
memory: 6800kb
input:
1000 20 4 5 7 12 5 3 1 4 18 6 4 6 15 14 12 10 17 3 15 16 2 20 9 6 11 1 17 16 20 20 2 15 17 20 18 13 14 11 8 11 16 12 16 3 17 19 4 14 13 19 9 6 10 9 12 12 1 20 8 13 11 1 19 19 20 17 4 5 19 11 16 6 14 3 8 11 14 12 16 18 2 16 4 3 5 7 14 17 6 13 9 8 13 8 7 4 16 10 9 6 12 16 11 6 10 11 8 3 14 3 1 15 18 4...
output:
894800271 578713470 329544123 486181111 877745774 60579366 -733093375 -522056555 -900642077 35910130 -159305831 597196237 668841622 -67696458 -861817655 -127802844 -421213667 809007925 -31415838 -610173985 513047552 -85646278 49189297 -192005642 24210 60646 -960732275 386514906 -668326859 -464372923...
result:
wrong answer 1st lines differ - expected: '819635573', found: '894800271'
Test #7:
score: 0
Wrong Answer
time: 85ms
memory: 10704kb
input:
50000 5 5 5 1 4 4 4 2 3 3 1 1 1 4 2 2 2 5 5 5 4 4 4 4 1 4 3 2 1 5 1 1 4 2 1 4 1 1 5 5 3 2 1 5 2 4 3 4 5 3 3 3 1 3 3 3 1 5 5 4 4 2 1 4 4 1 4 1 3 1 5 1 2 2 2 4 5 5 2 2 2 1 1 4 3 4 2 1 3 3 1 4 1 1 4 1 3 3 1 5 3 3 2 1 4 4 4 1 5 2 2 3 4 2 3 2 2 1 4 5 3 4 5 1 1 3 1 3 2 4 4 1 3 3 2 1 1 2 3 2 1 1 4 4 5 1 2 ...
output:
-318019235 770511477 -794263669 -642527894 112222060 -643074443 676852648 396898125 -402199072 -120278220 11174674 -899983210 102863201 -577923198 346998945 202224287 -512040978 542130131 -466243027 334151529 -143575032 -132999127 552532136 385686078 919005744 -326050096 137316688 650392292 -7428481...
result:
wrong answer 1st lines differ - expected: '926368451', found: '-318019235'
Test #8:
score: 0
Wrong Answer
time: 85ms
memory: 10772kb
input:
50000 5 4 4 5 3 5 1 3 1 4 3 4 4 5 5 5 5 4 2 3 2 3 2 3 2 4 3 2 4 5 1 3 5 1 3 3 5 5 2 2 3 1 2 1 3 4 3 2 2 4 1 3 1 2 2 2 5 5 5 1 1 2 3 1 2 2 5 4 3 3 2 3 1 1 5 3 4 2 1 5 2 3 4 4 1 5 5 3 1 2 3 2 3 2 4 2 1 5 5 3 2 3 5 2 3 5 1 1 1 3 2 3 5 5 1 3 2 3 5 2 1 2 3 5 5 3 1 5 2 2 3 4 5 4 2 2 3 5 5 1 2 1 3 4 3 5 1 ...
output:
711045375 328941485 162479732 689503296 -593585370 -886047892 -701959536 515756320 438434285 -945448103 -724131504 432921673 -635261906 -740538181 47073214 -768731478 62019725 -486402782 355081077 657610053 -228496484 982790125 -381349538 269613307 -11529515 -976548725 456628227 775065102 -440513792...
result:
wrong answer 1st lines differ - expected: '125103175', found: '711045375'
Test #9:
score: 0
Wrong Answer
time: 89ms
memory: 10688kb
input:
50000 5 3 5 4 1 5 5 3 3 2 4 2 3 2 2 5 1 2 4 2 3 2 5 4 3 5 3 2 4 1 1 4 5 2 3 2 3 2 5 1 3 5 4 2 1 5 2 3 3 5 4 1 3 5 1 5 4 5 1 2 5 3 3 2 1 5 5 4 1 4 4 4 5 4 5 2 1 1 4 3 2 4 5 4 3 2 3 4 2 1 2 3 3 4 4 4 3 4 4 4 4 2 4 1 2 5 2 2 1 3 2 4 1 1 3 1 5 2 4 1 2 2 3 2 1 4 2 3 4 5 3 4 3 3 1 4 3 5 1 3 2 4 3 4 1 5 4 ...
output:
-726440809 969968437 711135873 -515105711 526232892 -854813488 140331524 -437030706 200160056 -805793153 -770059415 -305225602 128373121 630518065 275195271 750004801 -960340056 -690554964 889522319 -225607047 569879846 -347792610 159563862 700457799 -459915418 -381964693 177963731 856854725 7306615...
result:
wrong answer 1st lines differ - expected: '338257176', found: '-726440809'
Test #10:
score: 0
Wrong Answer
time: 157ms
memory: 12272kb
input:
50000 20 16 13 12 19 15 8 19 11 4 15 20 20 14 10 16 4 6 6 17 5 7 5 16 16 5 14 13 6 6 5 10 1 9 2 12 3 1 10 5 16 16 16 15 9 5 2 12 2 19 20 6 5 17 13 20 1 18 4 7 4 8 8 16 16 1 19 10 1 20 14 16 16 9 10 16 5 3 8 6 1 19 4 5 15 8 17 8 6 20 14 1 8 13 8 15 14 7 17 14 18 2 10 13 11 11 1 7 14 20 5 14 18 8 19 5...
output:
-80317348 306737033 -598746875 -867087642 758966797 255355383 -606956862 -875226682 922547695 895983280 -322757877 409174051 -526535892 829334268 -117764480 -321103571 -594340945 -142790005 -255408576 -286514913 -883909628 -675808505 -304356800 451460193 593927163 -943008373 -377638595 228555078 -37...
result:
wrong answer 1st lines differ - expected: '247419772', found: '-80317348'
Test #11:
score: 0
Wrong Answer
time: 156ms
memory: 12656kb
input:
50000 20 1 6 13 1 6 5 16 8 4 17 18 15 5 18 15 12 14 2 18 13 13 17 9 15 3 19 15 15 2 16 11 14 13 15 6 18 11 1 5 14 17 14 8 14 4 14 5 9 16 14 2 8 11 2 14 5 1 1 19 2 16 1 7 8 15 12 17 5 13 13 10 1 6 18 14 1 3 11 10 18 4 3 17 6 4 11 2 4 11 12 17 18 12 3 17 18 7 13 14 11 5 4 11 2 13 17 15 15 19 4 5 2 6 1...
output:
-71189841 438478347 -362409128 -355090299 -154125070 716237498 54954104 -829222530 -651767753 844083674 892249409 381030175 203074138 -325720832 71244302 596004087 -291001725 -873014923 93373324 81913177 91132354 423930546 -145892146 239714095 205531853 -628087005 -143140876 589532118 -812273194 929...
result:
wrong answer 1st lines differ - expected: '181383728', found: '-71189841'
Test #12:
score: 0
Wrong Answer
time: 150ms
memory: 12656kb
input:
50000 20 14 1 16 11 11 6 19 10 20 4 17 3 13 19 13 11 4 18 16 17 16 1 18 16 18 6 13 2 4 15 19 17 15 15 20 6 12 10 7 3 5 3 5 17 2 18 19 5 7 14 1 3 14 10 10 3 16 14 5 19 8 3 7 3 9 18 20 12 7 6 15 11 9 11 19 2 20 18 6 7 3 18 1 17 7 2 19 14 16 15 12 15 10 11 9 18 8 8 2 7 14 16 9 14 6 8 15 18 17 12 16 19 ...
output:
837090083 407993017 711449228 -356558659 -138725031 249600563 -295120311 834448168 478173318 -613370469 -12858312 155206424 354798656 -194712891 479808434 -176924824 -49142590 -217033659 -489546865 -312548534 -256290537 -956895623 -256215314 280480504 -187263754 -564349395 332459562 700533243 -68453...
result:
wrong answer 1st lines differ - expected: '921338517', found: '837090083'