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 <iostream>
#include <set>
using namespace std;
const int step = 400;
long long N, Q, hol, o, tart, a, b;
int t[400001];
int apa[400001];
int f[400001];
set<pair<long long, int>> szet;
set<int> veg;
int solve(int x)
{
int y = a;
if (t[x] < y)
{
return 0;
}
int ret = 0;
while (f[x] != -1 && y <= t[f[x]])
{
ret += step;
x = f[x];
}
while (x != 0 && y <= t[apa[x]])
{
ret++;
x = apa[x];
}
return ret + 1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i <= N; i++)
{
cin >> t[i];
f[i] = -1;
}
f[0] = -1;
szet.insert({0, -0});
veg.insert(0);
int last = 0;
for (int i = 1; i <= N; i++)
{
o += (long long)t[i];
auto it = szet.lower_bound({o, -2 * N});
if (it != szet.end() && (*it).first == o && -(*it).second + 1 > last)
{
last = -(*it).second + 1;
szet.erase(it);
auto it2 = veg.lower_bound(last);
it2--;
apa[i] = (*it2);
f[i] = i;
for (int cnt = 0; cnt < step; cnt++)
{
if (f[i] == 0)
{
f[i] = -1;
break;
}
f[i] = apa[f[i]];
}
t[i] = last;
veg.insert(i);
}
szet.insert({o, -i});
}
cin >> Q;
for (int i = 0; i < Q; i++)
{
cin >> a >> b;
auto it = veg.lower_bound(b + 1);
it--;
cout << solve((*it)) << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |