# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1136158 | BuiDucManh123 | Sum Zero (RMI20_sumzero) | C++20 | 636 ms | 78296 KiB |
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define taskname ""
using namespace std;
const int lg = 20; // Logarithm base 2 of maximum n
const int MAXN = 400009;
ll n, q, ans, l, r;
ll f[lg + 1][MAXN], a[MAXN];
map<ll, ll> check;
int main() {
if (fopen(taskname ".inp", "r")) {
freopen(taskname ".inp", "r", stdin);
freopen(taskname ".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
// Initialize f array
for (int i = 0; i < lg; i++) {
for (int j = 0; j <= n; j++) {
f[i][j] = -1;
}
}
// Input and preprocessing
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += a[i - 1]; // Prefix sum
// Update the f array and map for prefix sums
if (check.find(a[i]) == check.end() && a[i] != 0) {
check[a[i]] = -1;
}
f[0][i] = max(check[a[i]], f[0][i - 1]);
check[a[i]] = i;
}
// Build sparse table
for (int i = 1; i < lg; i++) {
for (int j = 1; j <= n; j++) {
if (f[i - 1][j] != -1) {
f[i][j] = f[i - 1][f[i - 1][j]];
} else {
f[i][j] = -1;
}
}
}
// Answer queries
cin >> q;
while (q--) {
cin >> l >> r;
ans = 0;
for (int i = lg - 1; i >= 0; i--) {
if (f[i][r] >= l - 1) {
ans += (1 << i);
r = f[i][r];
}
}
cout << ans << "\n";
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |