# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1177135 | altern23 | Sum Zero (RMI20_sumzero) | C++17 | 1094 ms | 38912 KiB |
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pii pair<ll, ll>
#define fi first
#define sec second
#define ld long double
const int MAXN = 1e5;
const ll INF = 4e18;
const int MOD = 1e9 + 7;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for(;tc--;){
ll N; cin >> N;
vector<ll> ps(N + 1);
for(int i = 1; i <= N; i++){
ll x; cin >> x;
ps[i] = ps[i - 1] + x;
}
vector<vector<ll>> up(N + 2, vector<ll>(9));
map<ll, ll> mp;
ll MN = N + 1;
for(int i = N; i >= 0; --i){
if(mp.count(ps[i])){
MN = min(MN, mp[ps[i]]);
}
mp[ps[i]] = i;
up[i][0] = MN;
}
for(int LOG = 0; LOG < 9; LOG++){
up[N + 1][LOG] = N + 1;
}
for(int LOG = 1; LOG < 9; LOG++){
for(int i = 0; i <= N; i++){
up[i][LOG] = up[up[i][LOG - 1]][LOG - 1];
}
}
ll Q; cin >> Q;
for(int i = 1; i <= Q; i++){
ll l, r; cin >> l >> r;
--l;
ll ans = 0;
while(up[l][8] <= r){
ans += (1LL << 8);
l = up[l][8];
}
for(int LOG = 8; LOG >= 0; --LOG){
if(up[l][LOG] <= r){
ans += (1LL << LOG);
l = up[l][LOG];
}
}
cout << ans << "\n";
}
}
}
/*
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |