Submission #972569

#TimeUsernameProblemLanguageResultExecution timeMemory
972569VMaksimoski008Sum Zero (RMI20_sumzero)C++17
0 / 100
3 ms2908 KiB
#include <bits/stdc++.h>

#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 4e5 + 5;
const double eps = 1e-9;

int up[maxn][LOG];

int32_t main() {
    ios_base::sync_with_stdio(false);
    cout.tie(0); cin.tie(0);

    int n;
    cin >> n;

    vector<int> v(n+1), last(n+1, 1e9);
    for(int i=1; i<=n; i++) cin >> v[i];

    map<ll, int> mp; mp[0] = 0;
    ll sum = 0;

    vector<pii> I; I.push_back({ 0, 0 });
    for(int i=1; i<=n; i++) {
        sum += v[i];
        if(mp.count(sum)) last[i] = mp[sum];
        mp[sum] = i;
        if(last[i] != 1e9) I.push_back({ last[i], i });
    }

    vector<int> pref; pref.push_back(0);
    for(int i=1; i<I.size(); i++) {
        if((int)pref.size() == 0) pref.push_back(I[i].first);
        else pref.push_back(max(pref[i-1], I[i].first));
    }

    for(int j=0; j<LOG; j++)
        for(int i=0; i<=n; i++) up[i][j] = -1;

    int p=0;
    for(int i=1; i<=n; i++) {
        while(p+1 < (int)I.size() && I[p+1].second <= i) p++;
        up[i][0] = pref[p];
    }

    for(int j=1; j<LOG; j++)
        for(int i=1; i<=n; i++)
            if(up[i][j-1] != -1) up[i][j] = up[up[i][j-1]][j-1];

    int q;
    cin >> q;

    while(q--) {
        int l, r, ans = 0;
        cin >> l >> r;
        l--;
        for(int j=LOG-1; j>=0; j--) {
            if(up[r][j] >= l && up[r][j] != -1) {
                ans += (1 << j);
                r = up[r][j];
            }
        }
        cout << ans << '\n';
    }

    return 0;
}

Compilation message (stderr)

sumzero.cpp: In function 'int32_t main()':
sumzero.cpp:45:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for(int i=1; i<I.size(); i++) {
      |                  ~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...