제출 #541484

#제출 시각아이디문제언어결과실행 시간메모리
541484cegaxSum Zero (RMI20_sumzero)C++17
61 / 100
523 ms50604 KiB
#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rall(A) A.rbegin(),A.rend()
#define pb push_back 
#define dbg(x) do {cerr << #x <<" = " << (x) << endl; } while (false)
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
//cout << setprecision(12) << fixed;

const int maxn = 4e5+5;
const int LOGN = 20;

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n; cin >> n;
    int a[n+1], b[n+1];
    b[0] = 0;

    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        b[i] = b[i-1] + a[i];
    }

    map<int, int> last;
    int go[n+2][LOGN];

    go[n+1][0] = n+1;

    go[n][0] = (a[n] == 0 ? n : n+1);
    last[b[n]] = n;

    for(int i = n-1; i >= 1; i--) {
        last[b[i]] = i;
        go[i][0] = min((last[b[i-1]] == 0 ? n+1 : last[b[i-1]]), go[i+1][0]);
    }

    for(int i = 1; i <= n+1; i++)
        go[i][0]++;

    // for(int i = 1; i <= n; i++)
    //     cout << go[i][0] << " ";

    for(int j = 1; j < LOGN; j++) {
        for(int i = 1; i <= n+1; i++) {
            if(go[i][j-1] <= n+1) go[i][j] = go[go[i][j-1]][j-1];
            else go[i][j] = n+2;
        }
    }

    int q; cin >> q;
    while(q--) {
        int l, r; cin >> l >> r;

        int ans = 0;


        for(int i = LOGN-1; i >= 0; i--) {
            if(go[l][i] <= r+1) {
                ans |= (1 << i);
                l = go[l][i];
            }
        }

        cout << ans << "\n";
    }
	
	return 0;
}

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