# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
597230 | CaroLinda | Sum Zero (RMI20_sumzero) | C++14 | 211 ms | 15108 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>
#define ll long long
const int LOG = 20;
const int MAXN = 4e5+10;
using namespace std;
int N, Q, T;
ll C[MAXN];
int d[MAXN], l[MAXN], r[MAXN];
int dp[MAXN];
int ans[MAXN];
int id[MAXN];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N;
for(int i = 1; i <= N; i++)
cin >> C[i], C[i] += C[i-1];
iota(id, id+1+N, 0);
sort(id, id+1+N, [&](int a, int b){
if(C[a] == C[b]){
return a < b;
}
return C[a] < C[b];
});
for(int i= N; i > 0; i--){
if(C[id[i-1]] == C[id[i]]){
dp[id[i]] =id[i-1];
}
else dp[id[i]] = -1;
}
dp[0] = 0;
for(int i = 1; i <= N; i++){
if(dp[i] != -1){
d[i] = d[dp[i]]+1;
if( (d[i-1] == d[i] && dp[i-1] > dp[i]) || d[i-1] > d[i] ){
d[i] = d[i-1];
dp[i] = dp[i-1];
}
}
else{
d[i] = d[i-1];
dp[i] = dp[i-1];
}
}
cin >> Q;
for(int i = 1; i <= Q; i++){
cin >> l[i] >> r[i];
ans[i] = d[r[i]]-d[l[i]-1];
}
int toGet = 0, toFill = 1;
for(int i = 0; i < LOG; i++){
for(int j = 1; j <= Q; j++){
if((1<<i)&ans[j]){
r[j] = dp[r[j]];
}
}
for(int j= 1; j <= N; j++)
d[j] = dp[dp[j]];
for(int j = 1; j <= N; j++)
dp[j] = d[j];
}
for(int i = 1; i <= Q; i++)
cout << ans[i]-(r[i] < l[i]-1) << "\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... |