제출 #636451

#제출 시각아이디문제언어결과실행 시간메모리
636451danikoynovSum Zero (RMI20_sumzero)C++14
61 / 100
147 ms42712 KiB
#include<bits/stdc++.h> #define endl '\n' using namespace std; typedef long long ll; const int maxn = 4e5 + 10; int n, q; ll pref[maxn]; int sub[maxn], lvl[maxn], heavy[maxn], ch_head[maxn], par[maxn], ch_idx[maxn]; vector < int > g[maxn], ch; void dfs(int v) { heavy[v] = -1; sub[v] = 1; for (int u : g[v]) { lvl[u] = lvl[v] + 1; par[u] = v; dfs(u); sub[v] += sub[u]; if (heavy[v] == -1 || sub[u] > sub[heavy[v]]) heavy[v] = u; } } void hld(int v, int tp) { ch_idx[v] = ch.size(); ch.push_back(v); ch_head[v] = tp; if (heavy[v] != -1) { hld(heavy[v], tp); } for (int u : g[v]) { if (u == heavy[v]) continue; hld(u, u); } } void solve() { cin >> n; ll x; for (int i = 1; i <= n; i ++) { cin >> x; pref[i] = pref[i - 1] + x; } map < int, int > last; int cl = n + 1; for (int i = n; i >= 0; i --) { if (last[pref[i]] != 0) cl = min(cl, last[pref[i]]); g[cl].push_back(i); last[pref[i]] = i; } dfs(n + 1); hld(n + 1, n + 1); if (n > 3e5) return; cin >> q; int l, r, v, lf, rf, mf; for (int i = 1; i <= q; i ++) { cin >> l >> r; v = l - 1; while(ch_head[v] <= r) v = par[ch_head[v]]; rf = ch_idx[v], lf = ch_idx[ch_head[v]]; while(lf <= rf) { mf = (lf + rf) / 2; if (ch[mf] > r) lf = mf + 1; else rf = mf - 1; } cout << lvl[l - 1] - lvl[ch[rf]] - 1 << endl; } } void speed() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { speed(); ///freopen("input.txt", "r", stdin); solve(); return 0; } /** */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...