This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// PhuThuyRuntime <3
// A secret makes a woman woman
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fo(i, l, r) for(int i = l; i <= r; i++)
#define foi(i, l, r) for(int i = l; i >= r; i--)
#define elif else if
#define el cout << "\n";
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define fi first
#define se second
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#define ll long long
#define ull unsigned long long
#define pob pop_back
#define bs binary_search
#define vi vector<int>
#define vii vector<pair<int, int>>
#define getbit(i, j) (i >> j) & 1
#define offbit(i, j) (1 << j) ^ i
#define onbit(i, j) (1 << j) | i
const int N = 1e5 + 2;
const ll mod = 1e9 + 7;
const int inf = INT_MAX;
const int base = 31;
const long double EPS = 1e-9;
const long double pi = acos(-1.0);
int n, q, a[4 * N];
ll f[4 * N];
void inp(){
cin >> n;
fo(i, 1, n){
cin >> a[i];
f[i] = f[i - 1] + a[i];
}
cin >> q;
}
map<ll, int> pos;
int up[4 * N][20];
void sol(){
int cur = 1e9;
foi(i, n, 1){
if(pos.count(f[i])){
pos[f[i]] = min(pos[f[i]], i);
}
else pos[f[i]] = i;
if(pos.count(f[i - 1])){
cur = min(cur, pos[f[i - 1]]);
}
up[i][0] = (cur == 1e9 ? 0 : cur);
// cout << i << ' ' << cur, el
}
fo(j, 1, __lg(n)){
fo(i, 1, n){
if(up[i][j - 1] > 0) up[i][j] = up[up[i][j - 1] + 1][j - 1];
// if(i == 4) cout << (1 << j) << ':' << up[i][j], el
}
}
while(q--){
int l, r;
cin >> l >> r;
int res = 0;
foi(j, __lg(n), 0){
if(up[l][j] != 0 && up[l][j] <= r){
res |= 1 << j;
l = up[l][j] + 1;
// cout << l << ' ';
}
}
cout << res, el
}
}
int main(){
// in("sum0.inp");
// out("sum0.out");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
inp();
sol();
return 0;
}
/*
10
1 2 -3 0 1 -4 3 2 -1 1
3
1 10
1 5
2 9
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |