제출 #857091

#제출 시각아이디문제언어결과실행 시간메모리
857091busamateSum Zero (RMI20_sumzero)C++14
22 / 100
80 ms37556 KiB
#include <iostream> #include <set> #include <vector> using namespace std; long long N, Q, hol, o; long long t[400001]; int ans[400000]; vector<int> kerd[400001]; int aa[400000]; int f[400001][20]; int tav[400001]; int elo[400001]; set<pair<long long, int>> szet; set<int> veg; void solve(int x, int ki) { int y = aa[ki]; if (elo[x] < y) { ans[ki] = 0; return; } int ret = 0; for (int i = 19; i >= 0; i--) { if (tav[x] >= (1 << i) && y <= elo[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 += 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][0] = apa; for (int j = 1; (1 << j) <= tav[i]; j++) { f[i][j] = f[f[i][j - 1]][j - 1]; } elo[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...