// 01001100 01001111 01010100 01000001 \\
// \\
// ╦ ╔═╗╔╦╗╔═╗ \\
// ║ ║ ║ ║ ╠═╣ \\
// ╩═╝╚═╝ ╩ ╩ ╩ \\
// \\
// 01001100 01001111 01010100 01000001 \\
#include <bits/stdc++.h>
using namespace std;
#define N 400001
#define K 13
#define nl '\n'
#define ff first
#define ss second
#define add insert
#define ll long long
#define ld long double
#define terminator main
#define pll pair<ll,ll>
#define append push_back
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define L0TA ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
ll a[N];
int x[N][K];
void solve(){
int n, q, l, r, ans;
cin >> n;
vector<pair<ll, int>> v {{0, 1}};
for(int i = 1; i <= n; i++){
cin >> a[i];
x[i][0] = n + 2;
a[i] += a[i - 1];
v.append({a[i], i + 1});
}
sort(all(v));
for(int i = 1; i < v.size(); i++)
if(v[i - 1].ff == v[i].ff)
x[v[i - 1].ss][0] = v[i].ss;
v.clear();
for(int i = n - 1; i > 0; i--)
x[i][0] = min(x[i][0], x[i + 1][0]);
for(int j = 0; j < K; j++)
x[n + 1][j] = x[n + 2][j] = n + 2;
for(int j = 0; j < K - 1; j++)
for(int i = 1; i <= n; i++)
x[i][j + 1] = x[x[i][j]][j];
cin >> q;
while(q--){
cin >> l >> r;
r++;
ans = 0;
for(int i = K - 1; i >= 0; i--){
while(x[l][i] <= r){
ans += (1 << i);
l = x[l][i];
}
}
cout << ans << nl;
}
}
int terminator(){
L0TA;
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... |