Submission #1136187

#TimeUsernameProblemLanguageResultExecution timeMemory
1136187BuiDucManh123Sum Zero (RMI20_sumzero)C++20
0 / 100
22 ms52280 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, i, j, q, ans, l, r, f[lg + 9][MAXN];
ll a[MAXN];
int check[2 * MAXN]; // Dùng mảng thay cho map

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 min_val = 0, max_val = 0;
    
    // Tính tổng tiền tố và tìm giá trị min, max
    for (i = 1; i <= n; i++) {
        cin >> a[i];
        a[i] += a[i - 1];
        min_val = min(min_val, a[i]);
        max_val = max(max_val, a[i]);
    }

    // Ánh xạ giá trị sang chỉ số trong khoảng [0, 2n]
    int offset = -min_val; // Dịch các giá trị để min_val = 0
    int range = max_val + offset + 1;

    // Khởi tạo mảng check
    fill(check, check + range + 1, 0);

    // Khởi tạo mảng f
    for (i = 0; i < lg; i++)
        for (j = 0; j <= n; j++)
            f[i][j] = -1;

    // Duyệt tính giá trị f[0]
    check[offset] = -1; // Giá trị 0 được lưu tại vị trí offset
    for (i = 1; i <= n; i++) {
        int idx = a[i] + offset; // Chỉ số ánh xạ
        f[0][i] = max(check[idx], f[0][i - 1]);
        check[idx] = i; // Cập nhật vị trí xuất hiện cuối
    }

    // Tính toán f cho các giá trị mũ
    for (i = 1; i < lg; i++) {
        for (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;
            }
        }
    }

    // Trả lời các truy vấn
    cin >> q;
    while (q--) {
        cin >> l >> r;
        ans = 0;
        for (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:19:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |         freopen(taskname ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...