Submission #899897

#TimeUsernameProblemLanguageResultExecution timeMemory
899897TAhmed33Fish 2 (JOI22_fish2)C++98
8 / 100
4097 ms872892 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int MAXN = 1e5 + 25; pair <int, int> sparse[17][MAXN]; int a[MAXN], n; int pref[MAXN]; pair <int, int> query (int l, int r) { int m = __lg(r - l + 1); auto x = max(sparse[m][l], sparse[m][r - (1 << m) + 1]); x.second *= -1; return x; } int get (int l, int r) { if (l > r) return 0; return pref[r] - pref[l - 1]; } map <pair <int, int>, int> memo; int ans (int l, int r) { if (l > r) return 0; if (l == r) return 1; if (memo.count({l, r})) return memo[{l, r}]; auto u = query(l, r); int ret = 1; if (get(l, u.second - 1) >= a[u.second]) ret += ans(l, u.second - 1); if (get(u.second + 1, r) >= a[u.second]) ret += ans(u.second + 1, r); return memo[{l, r}] = ret; } signed main () { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; sparse[0][i] = {a[i], -i}; pref[i] = a[i] + pref[i - 1]; } for (int i = 1; i < 17; i++) { for (int j = 1; j + (1 << i) - 1 <= n; j++) { sparse[i][j] = max(sparse[i - 1][j], sparse[i - 1][j + (1 << (i - 1))]); } } int q; cin >> q; while (q--) { int t, l, r; cin >> t >> l >> r; cout << ans(l, r) << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...