제출 #624402

#제출 시각아이디문제언어결과실행 시간메모리
624402boris_mihovSum Zero (RMI20_sumzero)C++14
100 / 100
766 ms20136 KiB
#include <unordered_map> #include <algorithm> #include <iostream> #include <cassert> #include <numeric> #include <vector> typedef long long llong; const int MAXN = 400000 + 10; const int MAXLOG = 3; const int BASE = 100; std::unordered_map < llong,int > cnt; int sparse[MAXLOG][MAXN]; int n, q; void buildSparse() { for (int log = 1 ; log <= MAXLOG-1 ; ++log) { sparse[log][0] = -1; for (int i = 1 ; i <= n ; ++i) { sparse[log][i] = i; for (int j = 1 ; j <= BASE ; ++j) { sparse[log][i] = sparse[log-1][sparse[log][i]]; if (sparse[log][i] == -1) break; } } } } int ans, pw; int lifting(int from, const int &to) { ans = 0; pw = 10000; for (int log = MAXLOG-1 ; log >= 0 ; --log) { while (sparse[log][from] >= to-1) { ans += pw; from = sparse[log][from]; } pw /= BASE; } return ans; } void solve() { int l, r; buildSparse(); std::cin >> q; for (int i = 1 ; i <= q ; ++i) { std::cin >> l >> r; std::cout << lifting(r, l) << '\n'; } } void read() { cnt[0] = 1; sparse[0][0] = -1; int curr; llong sum = 0; std::cin >> n; for (int i = 1 ; i <= n ; ++i) { std::cin >> curr; sum += curr; sparse[0][i] = std::max(sparse[0][i-1], cnt[sum] - 1); cnt[sum] = i + 1; } } void fastIO() { std::ios_base :: sync_with_stdio(0); std::cout.tie(nullptr); std::cin.tie(nullptr); } int main() { fastIO(); read(); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...