제출 #1136184

#제출 시각아이디문제언어결과실행 시간메모리
1136184BuiDucManh123Sum Zero (RMI20_sumzero)C++20
0 / 100
2 ms1092 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, q, ans, l, r, f[lg + 9][400009];
ll a[400009];
vector<ll> all_values;
int last_pos[400009]; // Thay cho unordered_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);

    // Input số phần tử
    cin >> n;

    // Bước 1: Nhập và tạo mảng tổng tiền tố
    all_values.pb(0); // Giá trị 0 cần được ánh xạ
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        a[i] += a[i - 1]; // Tạo mảng tổng tiền tố
        all_values.pb(a[i]);
    }

    // Bước 2: Ánh xạ tọa độ
    sort(all_values.begin(), all_values.end());
    all_values.erase(unique(all_values.begin(), all_values.end()), all_values.end());
    int m = all_values.size();

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

    // Bước 3: Xây dựng bảng f[0][j]
    for (int i = 1; i <= n; i++) {
        int pos = lower_bound(all_values.begin(), all_values.end(), a[i]) - all_values.begin();
        f[0][i] = max(last_pos[pos], f[0][i - 1]);
        last_pos[pos] = i;
    }

    // Bước 4: Tính f[i][j] (với i > 0)
    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;
            }
        }
    }

    // Bước 5: 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;
}

컴파일 시 표준 에러 (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...