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>
using namespace std;
template<typename T> bool umax(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool umin(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
const int sz = 1e6 + 5;
#define ll long long
struct DSU{
vector<int>e;
DSU(int n){
e.assign(n + 1, -1);
}
int _find(int v){
if(e[v] < 0){
return v;
}
return e[v] = _find(e[v]);
}
int _union(int u,int v){
u = _find(u);
v = _find(v);
if(u != v){
if(e[u] > e[v]){
swap(u, v);
}
e[u] += e[v];
e[v] = u;
return 1;
}
return 0;
}
};
const int L = 19;
int a[sz];
ll pref[sz];
int up[L][sz];
void test_case(int testcase){
int n;
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
map<ll,int> last;
set<int>st;
int mn = n + 1;
for(int i = n; i > 0; --i){
last[pref[i]] = i;
if(last.count(pref[i - 1])){
umin(mn, last[pref[i - 1]]);
}
up[0][i] = mn + 1;
}
for(int i = 0; i < L; ++i){
up[i][n + 2] = up[i][n + 1] = n + 2;
}
for(int i = 1; i < L; ++i){
for(int j = 1; j <= n; ++j){
up[i][j] = min((int)(n + 2), up[i - 1][up[i - 1][j]]);
}
}
int Q;
cin >> Q;
while(Q--){
int l, r;
cin >> l >> r;
int res = 0;
for(int i = 18; i >= 0; --i){
if(up[i][l] <= r + 1){
res += (1 << i);
l = up[i][l];
}
}
cout << res << endl;
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
for(int test = 1; test <= T; ++test){
test_case(test);
}
}
/*
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |