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>
#define f first
#define s second
#define ll long long
#define pii pair<int,int>
using namespace std;
const int N = 4e5 + 1, mod = 1e9 + 7; // !
int t, R[N][3], f[N];
ll a[N];
vector<pair<ll, int>> x;
main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += a[i - 1];
x.push_back({a[i], i});
}
x.push_back({0, 0});
sort(x.begin(), x.end());
int cur = 0;
vector<int> id(n + 1);
for(int i = 0; i < x.size(); i++) {
if(!i || x[i].f != x[i - 1].f) ++cur;
id[x[i].s] = cur;
}
int mn = n + 1;
for(int j = 0; j <= 2; j++) R[n + 1][j] = n + 1;
vector<int> x(n + 5);
for(int i = n; i >= 0; i--) {
if(f[id[i]]) mn = min(mn, f[id[i]]);
x[i] = mn;
f[id[i]] = i;
}
id.clear(); id.shrink_to_fit();
int q;
cin >> q;
vector<int> l(q + 1), r(q + 1), ans(q + 1);
for(int i = 1; i <= q; i++) {
cin >> l[i] >> r[i];
--l[i];
}
for(int I = 6; I >= 0; I--) {
// i * 5 ... i * 5 + 4
for(int i = 0; i <= n; i++) R[i][0] = x[i];
for(int j = 1; j <= I * 3 + 2; j++) {
for(int i = 0; i <= n; i++) {
R[i][j % 3] = R[R[i][(j % 3 + 2) % 3]][(j % 3 + 2) % 3];
}
}
for(int i = 1; i <= q; i++) {
for(int j = 2; j >= 0; j--) if(R[l[i]][j] <= r[i]) ans[i] += 1 << (I * 3 + j), l[i] = R[l[i]][j];
}
}
for(int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
Compilation message (stderr)
sumzero.cpp:11:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
11 | main(){
| ^~~~
sumzero.cpp: In function 'int main()':
sumzero.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < x.size(); i++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |