# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
748430 | doowey | Sum Zero (RMI20_sumzero) | C++14 | 16 ms | 19540 KiB |
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;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = (int)4e5 + 10;
const int LOG = 19;
ll c[N];
vector<int> T[N];
vector<pii> Q[N];
int sol[N];
bool vis[N];
int path[N];
int iter = 0;
void dfs(int u){
path[iter] = u;
iter ++ ;
vis[u]=true;
for(auto xi : Q[u]){
int lf = 0, rf = iter;
int mid;
while(lf < rf){
mid = (lf + rf) / 2;
if(path[mid] > xi.fi){
lf = mid + 1;
}
else{
rf = mid;
}
}
sol[xi.se] = iter - 1 - lf;
}
for(auto x : T[u]){
dfs(x);
}
iter -- ;
}
int main(){
fastIO;
freopen("in.txt", "r", stdin);
int n;
cin >> n;
for(int i = 1; i <= n; i ++ ){
cin >> c[i];
}
ll suff = 0;
map<ll, int> en;
en[0ll] = n + 1;
int low = n + 2;
for(int i = n; i >= 1; i -- ){
suff += c[i];
if(en.count(suff)){
low = min(low, en[suff]);
}
if(low < n + 2)
T[low].push_back(i);
//cout << i << " -> " << low << "\n";
en[suff] = i;
}
int q;
cin >> q;
int li, ri;
int ans;
for(int iq = 1; iq <= q; iq ++ ){
cin >> li >> ri;
Q[li].push_back(mp(ri + 1, iq));
}
for(int i = n + 1; i >= 1; i -- ){
if(!vis[i]){
dfs(i);
}
}
for(int iq = 1; iq <= q; iq ++ ){
cout << sol[iq] << "\n";
}
return 0;
}
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... |