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;
long long N, Q, hol, o, tart, a, b;
int t[400001];
int f[400001][19];
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;
for (int i = 18; i >= 0; i--)
{
if (f[x][i] != -1 && y <= t[f[x][i]])
{
ret += (1 << i);
x = f[x][i];
}
}
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];
for (int j = 0; j < 19; j++)
f[i][j] = -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;
auto it2 = veg.lower_bound(last);
it2--;
int apa = (*it2);
f[i][0] = apa;
for (int j = 1; j < 19 && f[f[i][j - 1]][j - 1] != -1; j++)
{
f[i][j] = f[f[i][j - 1]][j - 1];
}
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... |