Editorial for Số điểm lớn hơn


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: bamboo881

Lời giải

Trước tiên để trả lời các query mà đề bài yêu cầu thì ta cần xây dựng được các câu trả lời: tại ví trí ~i~ thì có bao nhiêu số lớn hơn ~a_i~ mà đứng trước ~i~. Để giải quyết điều này, bạn cần cập nhật vị trí các số lớn hơn ~a_i~ mà đứng trước ~i~ vào segment tree lên ~1~ (ban đầu là ~0~) và khi trả lời câu hỏi tại ví trí ~i~ thì ta có thể lấy tổng từ ~1~ đến ~i-1~ của segment tree.

Và cuối cùng để trả lời các query thì ta có thể xây dựng 1 mảng cộng dồn các câu trả lời ở trên.

Code tham khảo

#include<bits/stdc++.h>

using namespace std;
using LL = long long;
#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(a) int(a.size())
void debug_out() {cout << '\n';}
template <typename Head, typename ...Tail>
void debug_out(Head H, Tail ...T){
    cout << H << ' ';
    debug_out(T...);
}
#define debug(...) cout << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)


const int N = 1e5 + 11;
const int NN = 1e6 + 11;
LL fen[4 * N], n, m, a[N], f[N], c[N], arr[N];
pair<int, int> que[N];
vector<int> b[NN];

void upd(int id, int l, int r, int u) {
    if (l > u || r < u) return;
    if (l == r) {
        fen[id] = 1;
        return;
    }
    int m = (l + r) / 2;
    upd(id * 2, l, m, u);
    upd(id * 2 + 1, m + 1, r, u);
    fen[id] = fen[id * 2] + fen[id * 2 + 1];
}

LL get(int id, int l, int r, int u) {
    if (l > u) return 0;
    if (r <= u) return fen[id];
    int m = (l + r) / 2;
    return get(id * 2, l, m, u) + get(id * 2 + 1, m + 1, r, u);
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    clock_t start = clock();

    cin >> n >> m;

    for (int i = 0; i < n; ++i) cin >> arr[i];
    for (int i = 0; i < m; ++i) cin >> que[i].first >> que[i].second;

    for (int i = 0; i < n; ++i) b[arr[i]].push_back(i + 1);
    for (int i = NN - 1; i >= 1; --i) {
        for (int j = int(b[i].size()) - 1; j >= 0; --j) {
            c[b[i][j]] = get(1, 1, n, b[i][j]);
            upd(1, 1, n, b[i][j]);
        }   
    }   
    f[0] = 0;
    for (int i = 1; i <= n; ++i) f[i] = c[i] + f[i - 1];

    for (int i = 0; i < m; ++i)
        cout << f[que[i].second] - f[que[i].first - 1] << '\n';



    clock_t end = clock();
    cerr << "Time: " << fixed << setprecision(10) << double (end - start) / double (CLOCKS_PER_SEC) << '\n';
    return 0;
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.