이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 = 2;
const int base = 633;
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;
}
for (int i = n; i >= 0; --i) {
nxt[0][i] = min(nxt[0][i], nxt[0][i + 1]);
}
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;
}
}
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... |