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>
#pragma GCC optimize("O3,no-stack-protector,conserve-stack")
using namespace std;
const int maxN = 4e5 + 5;
long long query[maxN];
long long get_l(const long long &num)
{
return num & ((1LL << 19) - 1LL);
}
long long get_r(const long long &num)
{
return (num >> 19) & ((1LL << 19) - 1LL);
}
long long get_idx(const long long &num)
{
return (num >> 38) & ((1LL << 19) - 1LL);
}
bool cmp_query(const long long &a, const long long &b)
{
return get_l(a) > get_l(b);
}
map<long long, int> helper;
map<long long, int>::iterator m_iter;
vector<int> S;
int lq[maxN], val[maxN];
int last_l;
int calc(const int &l, const int &r)
{
// //cout << "Start " << l << ' ' << r << '\n';
// if(r < l)return 0;
// if(val[r])last_l = lq[r];
// val[r] = val[r] + calc(l, lq[r] - 1);
// lq[r] = (val[r] ? last_l : l);
// //cout << " Calc " << l << ' ' << r << ' ' << val[r] << '\n';
int tr = r;
S.clear();
while(l <= tr)
{
if(val[tr])last_l = lq[tr];
S.push_back(tr);
tr = lq[tr] - 1;
}
int pr = l - 1;
while(!S.empty())
{
val[S.back()] += val[pr]; //cout << "Calc " << l << ' ' << S.back() << ' ' << val[S.back()] << '\n';
lq[S.back()] = (val[S.back()] ? last_l : l);
pr = S.back();
S.pop_back();
}
return val[r];
}
int re[maxN];
int res[maxN];
int n, q;
int main()
{
if(fopen("input.txt", "r"))
{
freopen("input.txt", "r", stdin);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
val[0] = 0;
for(int i = 1; i <= n; ++i)
{
cin >> re[i];
lq[i] = i;
val[i] = 0;
}
cin >> q;
long long l, r, idx;
for(int i = 1; i <= q; ++i)
{
cin >> l >> r;
idx = i;
query[i] = l | (r << 19) | (idx << 38);
}
sort(query + 1, query + q + 1, cmp_query);
helper.clear();
helper.insert(make_pair(0LL, n + 1));
long long sum = 0;
for(int i = n; i > 0; --i)
{
sum += re[i];
re[i] = -1;
m_iter = helper.find(sum);
if(m_iter == helper.end())
{
helper.insert(make_pair(sum, i));
}
else
{
re[i] = m_iter->second - 1;
m_iter->second = i;
}
}
int itl = n;
for(int i = 1; i <= q; ++i)
{
while(itl >= get_l(query[i]))
{
if(re[itl] != -1)
{
calc(itl, re[itl]);
if(!val[re[itl]])
{
val[re[itl]] = 1;
lq[re[itl]] = itl;
for(int j = re[itl] + 1; j <= n; ++j)
if(val[j])break;
else lq[j] = re[itl] + 1;
}
}
itl--;
}
res[get_idx(query[i])] = calc(get_l(query[i]), get_r(query[i]));
}
for(int i = 1; i <= q; ++i)
cout << res[i] << '\n';
return 0;
}
Compilation message (stderr)
sumzero.cpp: In function 'int main()':
sumzero.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |