제출 #475957

#제출 시각아이디문제언어결과실행 시간메모리
475957AlexandruabcdeSum Zero (RMI20_sumzero)C++14
22 / 100
7 ms628 KiB
#include <bits/stdc++.h> using namespace std; //ifstream f ("sumzero.in"); //ofstream g ("sumzero.out"); constexpr int NMAX = 5003; typedef long long LL; int N; int Root; int L, R[NMAX]; int V[NMAX]; map <LL, int> mp; int First[NMAX]; int Last[NMAX]; int prev_Question[NMAX]; int Question[NMAX]; int D[NMAX]; int vf = 0; void Read () { cin >> N; for (int i = 1; i <= N; ++ i ) cin >> V[i]; } void Precalculare () { Root = N + 2; First[N+2] = N+1; Last[N+2] = N+1; LL suff = 0; mp[0] = N; int val = N+1; for (int i = N; i >= 1; -- i ) { suff += 1LL * V[i]; int ind = mp[suff]; if (ind != 0) if (ind < val) { val = ind; First[val+1] = i; } Last[val+1] = i; mp[suff] = i-1; } mp.clear(); } void Dfs (int Node) { if (Node == 0) return; D[++vf] = Node; int q = Question[Node]; while (q != 0) { int val = R[q]+1; int st = 1, dr = vf; int pos = 1; while (st <= dr) { int mij = (st + dr) / 2; if (D[mij] > val) { pos = mij; st = mij + 1; } else dr = mij - 1; } ++ pos; V[q] = vf - pos; q = prev_Question[q]; } for (int i = First[Node]; i >= Last[Node]; -- i ) Dfs(i); --vf; } int Q; void Solve () { cin >> Q; for (int i = 1; i <= Q; ++ i ) { cin >> L >> R[i]; prev_Question[i] = Question[L]; Question[L] = i; } Dfs(Root); for (int i = 1; i <= Q; ++ i ) cout << V[i] << '\n'; } int main () { Read(); Precalculare(); Solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...