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];
int dp[2][MAXN];
int ans[MAXN];
map<ll,int> mp;
vector<pair<int,int> > q[2][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];
dp[0][0] = 0;
mp.insert(make_pair(0,0));
for(int i = 1; i <= N; i++){
d[i] = d[i-1];
dp[0][i] = dp[0][i-1];
if(mp.find(C[i]) != mp.end()){
int aux = mp[C[i]];
if( (d[aux]+1 == d[i-1] && aux >= dp[0][i] ) || d[aux]+1 > d[i-1] ){
d[i] = d[aux]+1;
dp[0][i] = aux;
}
}
mp[C[i]] = i;
}
cin >> Q;
for(int i = 1 , r; i <= Q; i++){
cin >> l[i] >> r;
int k = d[r]-d[l[i]-1];
ans[i] = k;
if(k != 0){
q[0][r].push_back(make_pair(k,i));
ans[i] = k-1;
}
}
int toGet = 0, toFill = 1;
for(int i = 0; i < LOG; i++, swap(toGet, toFill)){
for(int j = 0; j <= N; j++)
q[toFill][j].clear();
for(int j= 0; j <= N; j++){
for(auto &e: q[toGet][j]){
int k = e.first;
int l = e.second;
if((1<<i)&k){
//cout << "passei query " << l << " para " << dp[toGet][j] << " from " << j << " com " << (1<<i) << " pulos\n";
q[toFill][dp[toGet][j]].push_back(make_pair(k,l));
}
else q[toFill][j].push_back(e);
}
}
for(int j = 1; j <= N; j++){
dp[toFill][j] = dp[toGet][dp[toGet][j]];
}
}
for(int i = 0; i <= N; i++){
for(auto &e: q[toGet][i])
if(i >= l[e.second]-1)
ans[e.second]++;
}
for(int i = 1; i <= Q; i++)
cout << ans[i] <<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |