Submission #1136188

#TimeUsernameProblemLanguageResultExecution timeMemory
1136188BuiDucManh123Sum Zero (RMI20_sumzero)C++20
0 / 100
2 ms3648 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;

int n, q, ans, l, r, f[lg + 9][MAXN];
ll a[MAXN];
int last_seen[2 * MAXN]; // Mảng lưu chỉ số cuối cùng

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;
    ll offset = MAXN; // Đảm bảo chỉ số không âm (dịch toàn bộ giá trị lên)
    
    // Khởi tạo mảng tổng tiền tố và last_seen
    fill(last_seen, last_seen + 2 * MAXN, -1);
    last_seen[offset] = 0; // Tổng tiền tố 0 xuất hiện ở vị trí 0

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        a[i] += a[i - 1]; // Tính tổng tiền tố
    }

    // Khởi tạo f[0]
    for (int i = 1; i <= n; i++) {
        int idx = a[i] + offset; // Dịch giá trị để chỉ số >= 0
        f[0][i] = last_seen[idx];
        last_seen[idx] = i; // Cập nhật chỉ số xuất hiện cuối cùng
    }

    // Tính f cho các mũ của 2
    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;
            }
        }
    }

    // Xử lý truy vấn
    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:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen(taskname ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sumzero.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen(taskname ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...