This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int32_t)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
using namespace std;
const int N = 4e5 + 100;
int c[N], sp[N][10], p[10];
ll solve() {
p[0] = 1;
for (int i = 1; i < 10; i++)
p[i] = p[i - 1] * 4;
int n;
cin >> n;
for (int i = 1; i <= n + 2; i++) {
for (int j = 0; j < 10; j++)
sp[i][j] = n + 2;
}
map <ll, int> mp;
ll sum = 0;
mp[0] = 1;
for (int i = 1; i <= n; i++) {
cin >> c[i];
sum += c[i];
if (mp[sum]) {
sp[mp[sum]][0] = min(sp[mp[sum]][0], i + 1);
}
mp[sum] = i + 1;
}
for (int i = n + 1; i >= 1; i--) {
sp[i][0] = min(sp[i + 1][0], sp[i][0]);
for (int j = 1; j < 10; j++) {
int x = i;
for (int k = 0; k < 4; k++) {
x = sp[x][j - 1];
}
sp[i][j] = x;
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
int id = 9, cnt = 0;
while (id >= 0) {
if (sp[l][id] <= r + 1) {
l = sp[l][id];
cnt += p[id];
} else {
id--;
}
}
cout << cnt << '\n';
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |