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 int long long
#define pii pair<int,int>
#define Pii pair<int, pair<int,int>>
using namespace std;
const int N = 4e5 + 5, mod = 1e9 + 7; // !
int t, ans[N], a[N];
set<pair<int, pair<int,int> >> v[N];
vector<int> V[N], p;
map<int,int> f;
void dfs(int u) {
p.push_back(u);
for(int i = 0; i < V[u].size(); i++) dfs(V[u][i]);
p.pop_back();
while(v[u].size()) {
Pii x = *--v[u].end();
v[u].erase(x); if(x.f < 0) continue;
if((int)p.size() >= (1 << x.f) && p[(int)p.size() - (1 << x.f)] <= x.s.s) ans[x.s.f] += 1 << x.f, v[p[(int)p.size() - (1 << x.f)]].insert({x.f - 1, x.s});
else if(x.f) v[u].insert({x.f - 1, x.s});
}
// p.push_back(u);
}
main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n, q;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += a[i - 1];
}
cin >> q;
int mn = n + 1;
for(int i = n; i >= 0; i--) {
if(f[a[i]]) mn = min(mn, f[a[i]]);
V[mn].push_back(i);
f[a[i]] = i;
}
for(int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r; --l;
v[l].insert({18, {i, r}});
}
dfs(n + 1);
for(int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
Compilation message (stderr)
sumzero.cpp: In function 'void dfs(long long int)':
sumzero.cpp:15:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(int i = 0; i < V[u].size(); i++) dfs(V[u][i]);
| ~~^~~~~~~~~~~~~
sumzero.cpp: At global scope:
sumzero.cpp:25:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
25 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |