QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#709855#8142. ElevatorzhishengRE 0ms0kbC++202.1kb2024-11-04 17:09:472024-11-04 17:09:47

Judging History

你现在查看的是最新测评结果

  • [2024-11-04 17:09:47]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-04 17:09:47]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct BIT {
    std::vector<int> bit; // 树状数组本身
    int n;                // 数组长度
    // 构造函数,初始化树状数组
    BIT(int size) : n(size), bit(n + 10, 0) {}

    // 获取最低位1的位置
    int lowbit(int x) {
        return x & (-x);
    }

    // 更新数组中的值
    void add(int idx, int delta) {
        while (idx <= n) {
            bit[idx] += delta;
            idx += lowbit(idx);
        }
    }

    // 查询前缀和
    int query(int idx) {
        int sum = 0;
        while (idx > 0) {
            sum += bit[idx];
            idx -= lowbit(idx);
        }
        return sum;
    }

    // 获取区间和
    int rangeQuery(int left, int right) {
        return query(right) - query(left - 1);
    }
    void clear () {
        for(auto &x : bit) {
            x = 0;
        }
    }
};
signed main() {
    ios::sync_with_stdio(0),cin.tie(0);
    int n,m;
    cin >> n >> m;
    vector <int> a(n + 1);
    vector <int> v;
    v.push_back(0);
    vector <int> ans (n + 1);
    for(int i=1;i<=n;i++) {
        cin >> a[i];
        v.push_back(a[i]);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());

    BIT t1(n + 1);
    BIT t1sz(n + 1);
    auto get = [&](int x) {
        return lower_bound(v.begin(),v.end(),x) - v.begin() ;
    };

    for(int i=1;i<=n;i++) {
        int x = t1.query(get(a[i]));
        int t1szx = t1sz.query(get(a[i]));
        // cout << x << " ";
        ans[i] += t1szx*a[i] - x + t1szx;
        t1.add(get(a[i]),a[i]);
        t1sz.add(get(a[i]),1);
    }
    t1.clear();
    t1sz.clear();
    for(int i=n;i>=1;i--) {
        int x = t1.query(get(a[i]-1));
        int t1szx = t1sz.query(get(a[i]-1));
        ans[i] += t1szx*a[i] - x ;
        t1.add(get(a[i]),a[i]);
        t1sz.add(get(a[i]),1);
    }
    for(int i=1;i<=n;i++) {
        if(ans[i] > m - 2) {
            cout << -1 << "\n";
        }
        else cout << ans[i] << "\n";
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

6 20
3 8 12 6 9 9

output:


result: