Submission #874829

#TimeUsernameProblemLanguageResultExecution timeMemory
874829TAhmed33Sum Zero (RMI20_sumzero)C++98
0 / 100
9 ms29528 KiB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
const int MAXN = 4e5 + 1;
const int inf2 = 17;
const int inf = 1e9;
typedef long long ll;
ll c[MAXN], n;
int lst[MAXN], pp[MAXN], dd[MAXN];
pair <int, int> ls[MAXN];
vector <int> adj[MAXN];
int mnn = inf;
int pos = inf;
vector <ll> comp = {0};
int ans[MAXN];
vector <pair <int, int>> qs[MAXN];
void dfs (int pos, int par) {
	if (par != -1) dd[pos] = 1 + dd[par];
	comp.push_back(pos);
	for (auto [x, y] : qs[pos]) {
		int l = 0, r = (int)comp.size() - 1, anss = -1;
		while (l <= r) {
			int mid = (l + r) >> 1;
			if (comp[mid] <= x) {
				r = mid - 1; anss = comp[mid];
			} else {
				l = mid + 1;
			}
		}
		int z = -dd[anss] + dd[pos];
		if (lst[anss] <= x) z++;
		ans[y] = z;
	}
	for (auto j : adj[pos]) {
		if (j != par) {
			dfs(j, pos);
		}
	}
	comp.pop_back();
}
signed main () {
	//ios::sync_with_stdio(0); cin.tie(0);
	cin >> n; memset(dd, -1, sizeof(dd));
	for (int i = 0; i < MAXN; i++) ls[i] = {inf, inf};
	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		c[i] += c[i - 1]; comp.push_back(c[i]);
	}
	sort(comp.begin(), comp.end()); comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
	for (int i = 0; i <= n; i++) c[i] = lower_bound(comp.begin(), comp.end(), c[i]) - comp.begin();
	comp.clear();
	for (int i = n; i >= 0; i--) {
		lst[i] = dd[c[i]] != -1 ? dd[c[i]] : inf;
		dd[c[i]] = i;
		swap(ls[c[i]].first, ls[c[i]].second);
		ls[c[i]].second = i;
		if (ls[c[i]].first < mnn) {
			mnn = ls[c[i]].first; pos = ls[c[i]].second;
		}
		pp[i] = pos;
	} 
	for (int i = n; i >= 0; i--) {
		int u;
		if (lst[i] == inf) u = inf;
		else u = pp[lst[i]];
		if (u != inf) adj[u].push_back(i);
		else dd[i] = 1;
	}
	int q;
	cin >> q;
	for (int i = 1; i <= q; i++) {
		int l, r;
		cin >> l >> r;
		int cur = pp[l - 1];
		if (cur == inf || lst[cur] > r) {
			ans[i] = 0;
			continue;
		}
		qs[cur].push_back({r, i});
		/*for (int i = inf2 - 1; i >= 0; i--) {
			if (nxt[i][cur] != inf && nxt[i][cur] <= r) {
				cur = nxt[i][cur];
			}
		}
		int z = -dep[cur] + dep[pp[l - 1]];
		if (lst[cur] <= r) cur = lst[cur], z++;
		cout << z << '\n';*/
	}
	for (int i = 0; i <= n; i++) {
		if (dd[i] == 1) dfs(i, -1);
	}
	for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
}
	

Compilation message (stderr)

sumzero.cpp: In function 'void dfs(int, int)':
sumzero.cpp:20:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   20 |  for (auto [x, y] : qs[pos]) {
      |            ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...