제출 #1136178

#제출 시각아이디문제언어결과실행 시간메모리
1136178BuiDucManh123Sum Zero (RMI20_sumzero)C++20
0 / 100
3 ms3648 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define taskname ""
using namespace std;

const int lg = 20;
int n, i, j, q, ans, l, r;
vector<int> f_prev(400009, -1), f_curr(400009, -1);
ll a[400009];
map<ll, int> compressed;

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;

    // Nén giá trị tổng tiền tố
    int compressed_idx = 0;

    for (i = 1; i <= n; i++) {
        cin >> a[i];
        a[i] += a[i - 1];
        if (compressed.find(a[i]) == compressed.end()) {
            compressed[a[i]] = ++compressed_idx;
        }
    }

    vector<int> last_seen(compressed_idx + 1, -1);

    for (i = 1; i <= n; i++) {
        int cmp_idx = compressed[a[i]];
        f_prev[i] = max(last_seen[cmp_idx], f_prev[i - 1]);
        last_seen[cmp_idx] = i;
    }

    for (int k = 1; k < lg; k++) {
        for (i = 1; i <= n; i++) {
            if (f_prev[i] != -1) {
                f_curr[i] = f_prev[f_prev[i]];
            } else {
                f_curr[i] = -1;
            }
        }
        swap(f_prev, f_curr); // Chuyển sang tầng tiếp theo
    }

    cin >> q;
    while (q--) {
        cin >> l >> r;
        ans = 0;
        for (i = lg - 1; i >= 0; i--) {
            if (f_prev[r] >= l - 1) {
                ans += (1 << i);
                r = f_prev[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...