This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// PHK
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define bigint __int128
#define emb emplace_back
#define pb push_back
#define pii pair <int, int>
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define Task ""
#define MASK(k) (1ull << k)
#define bitcnt(k) __builtin_popcount(k)
#define testBit(n, k) ((n >> k) & 1)
#define flipBit(n, k) (n ^ (1ll << k))
#define offBit(n, k) (n & ~MASK(k))
#define onBit(n, k) (n | (1ll << k))
template <class T> bool minimize(T &a, T b) {if (a > b) {a = b; return true;} return false;}
template <class T> bool maximize(T &a, T b) {if (a < b) {a = b; return true;} return false;}
const int N = 4e5 + 5, LG = 19, mod = 1e9 + 7;
const ll INF = 1e18;
int n, a[N], up[N][LG];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
if (fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int j = 0; j < LG; j++)
for (int i = 1; i <= n + 1; i++)
up[i][j] = n + 1;
unordered_map <ll, int> pos;
pos[0] = 1; ll sum = 0;
for (int i = 1; i <= n; i++) {
sum += a[i];
int l = pos[sum]; up[l][0] = i;
pos[sum] = i + 1;
}
for (int i = n - 1; i > 0; i--) minimize(up[i][0], up[i + 1][0]);
for (int j = 1; j < LG; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++)
up[i][j] = max(up[i][j - 1], up[up[i][j - 1] + 1][j - 1]);
//for (int i = n - 1; i > 0; i--) minimize(up[i][j], up[i + 1][j]);
}
int q; cin >> q;
while (q--) {
int l, r; cin >> l >> r;
int ans = 0;
for (int bits = LG - 1, pos = l; bits > -1; bits--)
if (up[pos][bits] <= r) ans += 1 << bits, pos = up[pos][bits] + 1;
cout << ans << '\n';
}
}
Compilation message (stderr)
sumzero.cpp: In function 'int main()':
sumzero.cpp:36:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(Task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sumzero.cpp:37:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(Task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |