QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#626064#7787. Maximum RatingflsWA 1ms5704kbC++204.1kb2024-10-09 22:54:382024-10-09 22:54:38

Judging History

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

  • [2024-10-09 22:54:38]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5704kb
  • [2024-10-09 22:54:38]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define qmx(a,b) a<b?b:a
#define qmn(a,b) a>b?b:a
#define ll long long
#define fr first
#define se second

const int mxn = 2e5+5;

int n, q, ans;

ll a[mxn], q_val[mxn], sum_neg, pre_sum;

set <ll> input_pos_nums;

vector <ll> pos_nums;

int q_pos[mxn];

inline int query_num(ll num){
    int p = lower_bound(pos_nums.begin(), pos_nums.end(), num) - pos_nums.begin();
    return p + 1;
}

struct Segement_tree{
    int l;
    int r;
    ll sum;
    int cnt;
}tr[mxn << 3];

void build(int pl, int pr, int p){
    tr[p].l = pl;
    tr[p].r = pr;
    tr[p].sum = 0;
    tr[p].cnt = 0;
    if(pl == pr){
        return;
    }
    int mid = (pl + pr) >> 1;
    build(pl, mid, p << 1);
    build(mid + 1, pr, p << 1 | 1);
}

void modify(int d, int p, ll dt){
    if(tr[p].l == d && tr[p].r == d){
        tr[p].sum += dt;
        if(dt > 0)
            tr[p].cnt++;
        else
            tr[p].cnt--;
        return;
    }
    int mid = (tr[p].l + tr[p].r) >> 1;
    if(d <= mid)    modify(d, p << 1, dt);
    if(mid < d)     modify(d, p << 1 | 1, dt);
    tr[p].sum = tr[p << 1].sum + tr[p << 1 | 1].sum;
    tr[p].cnt = tr[p << 1].cnt + tr[p << 1 | 1].cnt;
}

ll getsum(int pl, int pr, int p){
    if(pr < pl)
        return 0;
    if(tr[p].l >= pl && tr[p].r <= pr){
        return tr[p].sum;
    }
    ll res = 0;
    int mid = (tr[p].l + tr[p].r) >> 1;
    if(pl <= mid)   res += getsum(pl, pr, p << 1);
    if(mid < pr)    res += getsum(pl, pr, p << 1 | 1);
    return res;
}

int getcnt(int pl, int pr, int p){
    if(pr < pl)
        return 0;
    if(tr[p].l >= pl && tr[p].r <= pr){
        return tr[p].cnt;
    }
    int res = 0;
    int mid = (tr[p].l + tr[p].r) >> 1;
    if(pl <= mid)   res += getcnt(pl, pr, p << 1);
    if(mid < pr)    res += getcnt(pl, pr, p << 1 | 1);
    return res;
}

int getpos(ll num, int p, ll lst){
    if(tr[p].l == tr[p].r){
        return tr[p].l;
    }
    ll dts = tr[p << 1].sum;
    if(dts + lst > num)
        return getpos(num, p << 1, lst);
    else
        return getpos(num, p << 1 | 1, lst + dts);
}

int main(){
    //input data
    ios::sync_with_stdio(false);
    cin >> n >> q;
    for(int i = 1 ; i <= n ; i++){
        cin >> a[i];
        if(a[i] > 0){
            input_pos_nums.insert(a[i]);
        }else{
            sum_neg -= a[i];
        }
    }
    for(int i = 1 ; i <= q ; i++){
        cin >> q_pos[i] >> q_val[i];
        input_pos_nums.insert(q_val[i]);
    }

    //离散化预处理
    for(auto num : input_pos_nums){
        pos_nums.push_back(num);
    }

    //权值线段树建树
    build(1, pos_nums.size(), 1);
    for(int p, i = 1 ; i <= n ; i++){
        if(a[i] > 0){
            p = query_num(a[i]);
            modify(p, 1, a[i]);
        }
    }

    //逐个处理询问
    ll bound_num;
    int lim_cnt, ql, qr, qmid;
    for(int p, i = 1 ; i <= q ; i++){
        //修改
        if(a[q_pos[i]] > 0){
            p = query_num(a[q_pos[i]]);
            modify(p, 1, -a[q_pos[i]]);
        }else{
            sum_neg += a[q_pos[i]];
        }
        a[q_pos[i]] = q_val[i];
        if(q_val[i] > 0){
            p = query_num(q_val[i]);
            modify(p, 1, q_val[i]);
        }else{
            sum_neg -= q_val[i];
        }

        //查询
        //cerr << sum_neg << " " << tr[1].sum << endl;
        if(sum_neg > tr[1].sum){
            ans = tr[1].cnt + 1;
        }else{
            p = getpos(sum_neg, 1, 0);
            pre_sum = getsum(1, p - 1, 1);
            ans = getcnt(1, p - 1, 1);
            lim_cnt = getcnt(p, p, 1);
            bound_num = pos_nums[p - 1];
            ql = 0;
            qr = lim_cnt;
            while(ql < qr){
                qmid = (ql + qr + 1) >> 1;
                if(qmid * bound_num + pre_sum > sum_neg)
                    qr = qmid - 1;
                else
                    ql = qmid;
            }
            //while(qr * bound_num + pre_sum <= sum_neg)
                //qr++;
            ans += ql;
        }
        cout << ans << "\n";
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 5704kb

input:

3 5
1 2 3
3 4
2 -2
1 -3
3 1
2 1

output:

0
1
2
2
3

result:

wrong answer 1st numbers differ - expected: '1', found: '0'