This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 2e5 + 5;
const int mod = 1e9 + 7;
const int LOG = 3;
const int base = 74;
const i64 oo = 1e18;
void solve() {
int n;
cin >> n;
vector<int> a(n), pw(LOG, 1);
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 1; i < LOG; ++i)
pw[i] = pw[i - 1] * base;
vector<vector<int>> nxt;
nxt.emplace_back(n + 2, n + 1);
map<i64, int> mp; mp[0] = 0;
i64 cur_sum = 0;
for (int i = 0; i < n; ++i) {
cur_sum += a[i];
auto it = mp.find(cur_sum);
if (it != mp.end()) nxt[0][it->ss] = i + 1;
mp[cur_sum] = i + 1;
}
a.clear();
a.shrink_to_fit();
for (int i = n; i >= 0; --i) {
nxt[0][i] = min(nxt[0][i], nxt[0][i + 1]);
}
nxt[0].shrink_to_fit();
for (int i = 1; i < LOG; ++i) {
nxt.emplace_back(n + 2, n + 1);
for (int j = 0; j < isz(nxt[i]); ++j) {
int cur = j;
for (int _ = 0; _ < base; ++_)
cur = nxt[i - 1][cur];
nxt[i][j] = cur;
}
nxt[i].shrink_to_fit();
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l;
int res = 0;
for (int i = LOG - 1; i >= 0; --i) {
while (nxt[i][l] <= r) {
res += pw[i];
l = nxt[i][l];
}
}
cout << res << '\n';
}
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |