Submission #857118

#TimeUsernameProblemLanguageResultExecution timeMemory
857118busamateSum Zero (RMI20_sumzero)C++14
61 / 100
100 ms18624 KiB
#include <iostream> #include <set> #include <algorithm> using namespace std; long long N, Q, hol, o, tart; int t[100001]; int ki[100000]; int aa[100000]; int bb[100000]; int f[100001][19]; int tav[100001]; set<pair<long long, int>> szet; set<int> veg; void solve(int x, int ki) { int y = aa[ki]; if (t[x] < y) { aa[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]; } } aa[ki] = ret + 1; } bool kis(int x, int y){ return bb[x] < bb[y]; } 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] >> bb[i]; ki[i] = i; } sort(ki, ki + Q, kis); 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][0] = apa; 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); } int utso = (*veg.rbegin()); while(tart < Q && bb[ki[tart]]==i) { solve(utso, ki[tart]); tart++; } szet.insert({o, -i}); } for (int i = 0; i < Q; i++) cout << aa[i] << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...