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 <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <complex>
#include <list>
#include <chrono>
#include <random>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;
#define endl "\n"
// #define int long long
const int INF = 2 * 1e5 + 5;
const int MOD = 1e9 + 7;
void solve(){
int n;
cin >> n;
vector<int> a(n);
int k = 620; // aprox sqrt(n)
for(int i = 0; i < n; i++){
cin >> a[i];
}
vector<int> idx(n);
for(int i = 0; i < n+1; i++){
idx[i] = n+1;
}
unordered_map<int, int> mp;
mp[0] = n;
int s = 0;
for(int i = n-1; i >= 0; i--){
s += a[i];
if(mp.count(s)) idx[i] = mp[s];
mp[s] = i;
}
mp.clear();
int curr = n+1;
for(int i = n-1; i >= 0; i--){
if(idx[i] > -1){
curr = min(curr, idx[i]);
}
idx[i] = curr;
}
vector<int> valid(n);
for(int i = 0; i < n; i++){
curr = i;
for(int j = 0; j < k && curr <= n; j++){
curr = idx[curr];
}
valid[i] = curr;
}
int m;
cin >> m;
while(m--){
int l, r;
cin >> l >> r;
curr = l-1;
int sol = 0;
while(valid[curr] <= r){
sol += k;
curr = valid[curr];
}
while(idx[curr] <= r){
sol++;
curr = idx[curr];
}
cout << sol << endl;
}
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int t = 1;
// cin >> t;
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... |