제출 #857111

#제출 시각아이디문제언어결과실행 시간메모리
857111busamateSum Zero (RMI20_sumzero)C++14
0 / 100
8 ms25436 KiB
#include <iostream> #include <set> #include <vector> using namespace std; long long N, Q, hol, o; int t[400001]; int ans[400001]; vector<int> kerd[400001]; int aa[400001]; vector<int> f[400001]; int tav[400001]; set<pair<long long, int>> szet; set<int> veg; void solve(int x, int ki) { int y = aa[ki]; if (t[x] < y) { ans[ki] = 0; return; } int ret = 0; for (int i = 18; i >= 0; i--) { if (tav[x] >= (1 << i) && y <= t[f[x][i]]) { ret += (1 << i); x = f[x][i]; } } ans[ki] = ret + 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N; for (int i = 1; i <= N; i++) cin >> t[i]; cin >> Q; for (int i = 0; i < Q; i++) { cin >> aa[i] >> hol; kerd[hol].push_back(i); } szet.insert({0, -0}); veg.insert(0); int last = 0; for (int i = 1; i <= N; i++) { o += (long long)t[i]; auto it = szet.lower_bound({o, -2 * N}); if (it != szet.end() && (*it).first == o && -(*it).second + 1 > last) { last = -(*it).second + 1; auto it2 = veg.lower_bound(last); it2--; int apa = (*it2); tav[i] = tav[apa] + 1; f[i].push_back(apa); for (int j = 1; (1 << j) <= tav[i]; j++) { f[i].push_back(f[f[i][j - 1]][j - 1]); } t[i] = last; veg.insert(i); } int utso = (*veg.rbegin()); for (int j : kerd[i]) { solve(utso, j); } szet.insert({o, -i}); } for (int i = 0; i < Q; i++) cout << ans[i] << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...