# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1136153 | Thanhs | Sum Zero (RMI20_sumzero) | C++20 | 362 ms | 18244 KiB |
#include <bits/stdc++.h>
using namespace std;
#define name "TENBAI"
#define fi first
#define se second
// #define int long long
#define endl '\n'
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}
const int NM = 4e5 + 5;
vector<int> nen;
int n, a[NM][7], pr[NM], A[NM];
int F(int i, int j)
{
if (j % 3)
{
if (F(i, j - 1) == n + 1)
return n + 1;
return F(F(i, j - 1) + 1, j - 1);
}
return a[i][j / 3];
}
void solve()
{
memset(pr, -1, sizeof pr);
cin >> n;
nen.resize(n + 1);
nen[0] = 0;
int cr = 0;
for (int i = 1; i <= n; i++)
{
cin >> A[i];
cr += A[i];
nen[i] = cr;
}
sort(nen.begin(), nen.end());
nen.erase(unique(nen.begin(), nen.end()));
for (int i = 1; i <= n + 1; i++)
a[i][0] = n + 1;
cr = 0;
for (int i = 0; i <= n; i++)
{
cr += A[i];
int t = lower_bound(nen.begin(), nen.end(), cr) - nen.begin();
if (~pr[t])
setmin(a[pr[t] + 1][0], i);
pr[t] = i;
}
for (int i = n - 1; i >= 1; i--)
setmin(a[i][0], a[i + 1][0]);
for (int i = 3; (1 << i) <= n; i += 3)
for (int j = 1; j <= n + 1; j++)
{
if (F(j, i - 1) == n + 1)
a[j][i / 3] = n + 1;
else
a[j][i / 3] = F(F(j, i - 1) + 1, i - 1);
}
int q;
cin >> q;
while (q--)
{
int l, r;
cin >> l >> r;
int ans = 0;
for (int i = __lg(n); i >= 0; i--)
if (F(l, i) <= r)
{
ans += 1 << i;
l = F(l, i) + 1;
}
cout << ans << endl;
}
}
signed main()
{
if (fopen("in.txt", "r"))
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
else if (fopen(name".inp", "r"))
{
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
// cin >> tc;
while (tc--)
solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |