이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=4e5+7, K=500;
ll T[LIM];
map<ll,int>mp;
int nxt[LIM][2], n, q;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
rep(i, n) {
cin >> T[i+1];
T[i+1]+=T[i];
}
for(int i=n; i; --i) {
mp[T[i]]=i;
if(mp[T[i-1]]) nxt[i][0]=mp[T[i-1]];
else nxt[i][0]=n+1;
}
nxt[n+1][0]=nxt[n+1][1]=n+1;
for(int i=n; i; --i) {
nxt[i][0]=min(nxt[i][0], nxt[i+1][0]);
int akt=i;
rep(j, K) {
akt=nxt[akt][0]+1;
if(akt==n+1) {
++akt;
break;
}
}
nxt[i][1]=akt-1;
}
int q;
cin >> q;
while(q--) {
int l, r, ans=0;
cin >> l >> r;
while(nxt[l][1]<=r) {
ans+=K;
l=nxt[l][1]+1;
}
while(nxt[l][0]<=r) {
++ans;
l=nxt[l][0]+1;
}
cout << ans << '\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... |