제출 #1136186

#제출 시각아이디문제언어결과실행 시간메모리
1136186BuiDucManh123Sum Zero (RMI20_sumzero)C++20
61 / 100
491 ms52772 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;
int n, i, j, q, ans, l, r, f[lg + 9][400009];
ll a[400009];
unordered_map<ll, int> check, compress;

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);

    // Đọc số lượng phần tử
    cin >> n;
    vector<ll> values;
    
    // Lưu giá trị tổng prefix
    for (i = 1; i <= n; i++) {
        cin >> a[i];
        a[i] += a[i - 1];
        values.pb(a[i]);
    }

    // Thêm giá trị 0 vào nén
    values.pb(0);
    sort(values.begin(), values.end());
    values.erase(unique(values.begin(), values.end()), values.end());

    // Ánh xạ giá trị thực tế thành chỉ số
    for (i = 0; i < values.size(); i++) {
        compress[values[i]] = i + 1;
    }

    // Khởi tạo f và check
    for (i = 0; i < lg; i++)
        for (j = 0; j <= n; j++)
            f[i][j] = -1;

    // Duyệt để cập nhật bảng f[0]
    for (i = 1; i <= n; i++) {
        int compressed_value = compress[a[i]];
        if (check[compressed_value] == 0 && compressed_value != compress[0]) 
            check[compressed_value] = -1;
        f[0][i] = max(check[compressed_value], f[0][i - 1]);
        check[compressed_value] = i;
    }

    // Tính toán bảng 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;
}

컴파일 시 표준 에러 (stderr) 메시지

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