# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1135966 | Thanhs | Sum Zero (RMI20_sumzero) | C++20 | 3 ms | 1600 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<pair<int, int>> v;
map<int, int> pr;
int n, a[NM][30];
void solve()
{
cin >> n;
pr[0] = 0;
int cr = 0;
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
cr += x;
if (pr.count(cr))
v.emplace_back(pr[cr] + 1, i);
pr[cr] = i;
}
sort(v.begin(), v.end(), [&](const auto& x, const auto& y) {return x.fi > y.fi;});
int pt = 0, mn = n + 1;
for (int i = n; i >= 1; i--)
{
while (pt < v.size() && v[pt].fi >= i)
setmin(mn, v[pt++].se);
a[i][0] = mn;
}
a[n + 1][0] = n + 1;
for (int i = 1; (1 << i) <= n; i++)
for (int j = 1; j <= n + 1; j++)
{
if (a[j][i - 1] == n + 1)
a[j][i] = n + 1;
else
a[j][i] = (a[a[j][i - 1] + 1][i - 1]);
}
cout << a[5][1] << endl;
int q;
cin >> q;
while (q--)
{
int l, r;
cin >> l >> r;
int ans = 0;
for (int i = __lg(n); i >= 0; i--)
if (a[l][i] <= r)
{
ans += 1 << i;
l = a[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();
}
컴파일 시 표준 에러 (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... |