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>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
#pragma GCC target("avx2,popcnt")
#pragma GCC optimize("O3,unroll-loops")
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int B = 18;
const int mxn = 4e5+10;
int N,Q;
int dp[mxn];
int arr[mxn];
int ans[mxn];
pii req[mxn];
bool roll;
void calc(int lvl){
roll = 0;
for(int i = 1;i<=N+1;i++)dp[i] = arr[i];
dp[mxn-1] = mxn-1;
for(int i = 0;i<lvl;i++){
roll ^= 1;
for(int j = 1;j<=N+1;j++){
dp[j] = dp[dp[j]];
}
}
for(int i = 1;i<=Q;i++){
auto &[s,e] = req[i];
if(dp[s]<=e+1){
ans[i] += 1<<lvl;
s = dp[s];
}
}
return;
}
unordered_map<ll,int> mp;
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N;
for(int i = 1;i<=N;i++){
cin>>arr[i];
}
int mn = mxn-1;
ll sum = 0;
for(int i = N+1;i>=1;i--){
sum += arr[i];
if(mp.find(sum) != mp.end())mn = min(mn,mp[sum]);
mp[sum] = i;
arr[i] = mn;
}
cin>>Q;
for(int i = 1;i<=Q;i++){
cin>>req[i].fs>>req[i].sc;
}
for(int i = B-1;i>=0;i--)calc(i);
for(int i = 1;i<=Q;i++){
cout<<ans[i]<<'\n';
}
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... |