이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
typedef long long ll;
typedef pair<int, int> pii;
const int MAX = 100001;
const int LOG = 20;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
#define pb push_back
#define sz(x) (int) x.size()
#define fr first
#define sc second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define dbg(x) cout << #x << ": " << "[ " << x << " ]\n"
int n, jump[MAX][LOG], p[MAX], q;
int query(int l, int r){
int ans = 0;
for(int j = LOG - 1; j >= 0; j--){
if(jump[r][j] >= l - 1){
ans += (1 << j);
r = jump[r][j];
}
}
return ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
unordered_map<ll, int> sums;
sums[0] = 0;
ll s = 0;
for(int i = 1; i <= n; i++){
int x; cin >> x; s += x;
if(sums.find(s) != sums.end())
p[i] = sums[s];
else p[i] = -1;
sums[s] = i;
}
for(int i = 0; i < LOG; i++)
jump[0][i] = -1;
for(int i = 1; i <= n; i++){
jump[i][0] = max(p[i], jump[i - 1][0]);
}
for(int j = 1; j < LOG; j++){
for(int i = 1; i <= n; i++){
if(jump[i][j - 1] != -1) jump[i][j] = jump[jump[i][j - 1]][j - 1];
else jump[i][j] = -1;
}
}
cin >> q;
while(q--){
int l, r; cin >> l >> r;
cout << query(l, r) << '\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... |