Submission #999994

#TimeUsernameProblemLanguageResultExecution timeMemory
999994WhisperSum Zero (RMI20_sumzero)C++17
100 / 100
872 ms19780 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; //#define int long long #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (b); i >= (a); i --) #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPD(i, n) for (int i = (n) - 1; i >= 0; --i) #define MASK(i) (1LL << (i)) #define BIT(x, i) (((x) >> (i)) & 1) /* Phu Trong from Nguyen Tat Thanh High School for gifted student */ template <class X, class Y> bool minimize(X &x, const Y &y){ X eps = 1e-9; if (x > y + eps) {x = y; return 1;} return 0; } template <class X, class Y> bool maximize(X &x, const Y &y){ X eps = 1e-9; if (x + eps < y) {x = y; return 1;} return 0; } #define MAX 400001 #define LOG 2 #define Base 200 int nArr, numQuery; #define All(x...) (x).begin(), (x).end() int up[MAX][LOG], pw[LOG]; int i; void process(void){ cin >> nArr; vector<pair<int, int>> segment; int sum = 0; map<int, int> last; last[0] = 0; for (i = 1; i <= nArr; ++i){ int x; cin >> x; sum += x; if (last.count(sum)){ segment.emplace_back(last[sum] + 1, i); } last[sum] = i; } sort(All(segment)); vector<pair<int, int>> candidate; for (pair<int, int>& x : segment){ while ((int)candidate.size() && candidate.back().second >= x.second) candidate.pop_back(); candidate.push_back(x); } segment = move(candidate); int j = 1, N = (int)segment.size(); for (i = 1; i <= N; ++i){ while (j <= N && segment[i - 1].second >= segment[j - 1].first) ++j; up[i][0] = (j > N ? 0 : j); } pw[0] = 1; for (int i = 1; i < LOG; ++i){ pw[i] = pw[i - 1] * Base; for (int u = 1; u <= N; ++u){ int v = u; REP(j, Base) v = up[v][i - 1]; up[u][i] = v; } } cin >> numQuery; int l, r, u; for (i = 1; i <= numQuery; ++i){ cin >> l >> r; u = lower_bound(All(segment), make_pair(l, l)) - segment.begin(); if (u >= N || segment[u].second > r) { cout << "0\n"; continue; } ++u; int ans = 0; int16_t k = LOG - 1; while (k >= 0){ if (up[u][k] > 0 && segment[up[u][k] - 1].second <= r){ u = up[u][k]; ans += pw[k]; } else{ k--; } } cout << ans + 1 << '\n'; } } signed main(){ #define name "Whisper" cin.tie(nullptr) -> sync_with_stdio(false); //freopen(name".inp", "r", stdin); //freopen(name".out", "w", stdout); process(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...