Submission #636478

#TimeUsernameProblemLanguageResultExecution timeMemory
636478danikoynovSum Zero (RMI20_sumzero)C++14
61 / 100
434 ms26580 KiB
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const int maxn = 4e5 + 10, maxlog = 7, base = 10;

int n, dp[maxn][maxlog], q;
ll a[maxn], pref[maxn];
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        pref[i] = pref[i - 1] + a[i];
    }

    unordered_map < ll, int > last;
    int cl = n + 1;
    dp[n + 1][0] = n + 1;
    for (int i = n; i >= 0; i --)
    {
        if (last[pref[i]] != 0)
        cl = min(cl, last[pref[i]]);
        dp[i][0] = cl;
        //cout << i << " : " << cl << endl;
        last[pref[i]] = i;
    }

    for (int j = 1; j < maxlog; j ++)
        for (int i = 0; i <= n + 1; i ++)
        {
            dp[i][j] = dp[i][j - 1];
            for (int f = 1; f < base; f ++)
            dp[i][j] = dp[dp[i][j]][j - 1];
        }
    /**for (int j = 0; j < 4; j ++, cout << endl)
        for (int i = 0; i <= n; i ++)
        cout << dp[i][j] << " ";*/

    cin >> q;
    for (int i = 1; i <= q; i ++)
    {
        int l, r;
        cin >> l >> r;
        l = l - 1;
        int jump = 0, st = 1;
        for (int bit = 1; bit < maxlog; bit ++)
            st = st * base;
        for (int bit = maxlog - 1; bit >= 0; bit --)
        {
            //cout << l << " " << jump << endl;
            for (int f = 0; f < base; f ++)
            {
            if (dp[l][bit] <= r)
            {
                //cout << dp[l][bit] << endl;
                jump += st;
                l = dp[l][bit];
            }
            }
        st /= base;
        }
        cout << jump << endl;
    }
}
void speed()
{
    ios_base::sync_with_stdio((false));
    cin.tie(NULL);
    cout.tie(NULL);
}
int main()
{
    speed();
    //freopen("input.txt", "r", stdin);
    solve();
   return 0;
}
/**

*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...