Submission #636570

#TimeUsernameProblemLanguageResultExecution timeMemory
636570danikoynovSum Zero (RMI20_sumzero)C++14
61 / 100
511 ms20724 KiB
#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;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...