제출 #857132

#제출 시각아이디문제언어결과실행 시간메모리
857132busamateSum Zero (RMI20_sumzero)C++14
22 / 100
100 ms22044 KiB
#include <iostream> #include <set> #include <vector> using namespace std; long long N, Q, hol, o, tart, a, b; int t[400001]; vector<int> f[400001]; int tav[400001]; set<pair<long long, int>> szet; set<int> veg; int solve(int x) { int y = a; if (t[x] < y) { return 0; } int ret = 0; for (int i = 18; i >= 0; i--) { if ((1 << i) <= tav[x] && y <= t[f[x][i]]) { ret += (1 << i); x = f[x][i]; } } return 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]; } 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; int lv = 0; while ((1 << lv) <= tav[i]) lv++; f[i].reserve(lv); f[i].resize(lv); f[i][0] = apa; //cout << i << ' ' << lv << endl; for (int j = 1; (1 << j) <= tav[i]; j++) { f[i][j] = f[f[i][j - 1]][j - 1]; } t[i] = last; veg.insert(i); } szet.insert({o, -i}); } cin >> Q; for (int i = 0; i < Q; i++) { cin >> a >> b; auto it = veg.lower_bound(b + 1); it--; cout << solve((*it)) << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...