Submission #1136163

#TimeUsernameProblemLanguageResultExecution timeMemory
1136163BuiDucManh123Sum Zero (RMI20_sumzero)C++20
61 / 100
268 ms43232 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define taskname ""
using namespace std;

const int lg = 20;  
const int MAXN = 400009;

ll n, q, ans, l, r;
int f[lg + 1][MAXN]; // Giảm kiểu dữ liệu từ ll sang int
ll a[MAXN];
unordered_map<ll, ll> check; // Đổi map sang unordered_map để giảm bộ nhớ

int main() {
    if (fopen(taskname ".inp", "r")) {
        freopen(taskname ".inp", "r", stdin);
        freopen(taskname ".out", "w", stdout);
    }

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n;

    for (int i = 0; i < lg; i++) {
        fill(f[i], f[i] + n + 1, -1); // Sử dụng fill thay vì vòng lặp
    }

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

        if (check.find(a[i]) == check.end() && a[i] != 0) {
            check[a[i]] = -1;
        }
        f[0][i] = max(check[a[i]], (ll)f[0][i - 1]); // Ép kiểu về int
        check[a[i]] = i;
    }

    for (int i = 1; i < lg; i++) {
        for (int j = 1; j <= n; j++) {
            if (f[i - 1][j] != -1) {
                f[i][j] = f[i - 1][f[i - 1][j]];
            } else {
                f[i][j] = -1;
            }
        }
    }

    cin >> q;
    while (q--) {
        cin >> l >> r;
        ans = 0;

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

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

    return 0;
}

Compilation message (stderr)

sumzero.cpp: In function 'int main()':
sumzero.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         freopen(taskname ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sumzero.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         freopen(taskname ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...