Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 = 6;
- const int BASE = 10;
- 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 lifting(int from, int to)
- {
- int ans = 0;
- int pw = 100000;
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement