QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#768849#7787. Maximum RatingmobbbML 1ms3792kbC++203.7kb2024-11-21 14:51:152024-11-21 14:51:22

Judging History

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

  • [2024-11-21 14:51:22]
  • 评测
  • 测评结果:ML
  • 用时:1ms
  • 内存:3792kb
  • [2024-11-21 14:51:15]
  • 提交

answer

#include <bits/stdc++.h>

#define ll long long

const int N = 4e5 + 7;

int val[N], m;

struct info{
    ll val, t;
};

info operator + (const info &l,const info &r){
    info b = {0, 0};
	if (l.t && r.t){
		b.val = l.val + r.val;
		b.t = l.t + r.t;
	}else if (l.t){
		b = l;
	}else if (r.t){
		b = r;
	}
    return b;
}

struct node{
    info s;
}seg[N * 4];

void update(int id){
    seg[id].s = seg[2 * id].s + seg[2 * id + 1].s;
}

void build(int id,int l,int r){
    if (l == r){
        seg[id].s = {val[l], 0};
    }else{
        int mid = (l + r) / 2;
        build(id * 2,l,mid);
        build(id * 2 + 1,mid + 1,r);
        update(id);
    }
}

void change(int id,int l,int r,int pos,int t){
    if (l == r){
        seg[id].s.t = t;
    }else{
        int mid = (l + r) / 2;
        if (pos <= mid) {
            change(2 * id,l,mid,pos,t);
        }
        else{
            change(2 * id + 1,mid + 1,r,pos,t);
        }
        update(id);
    }
}

ll query(int id,int l,int r,ll sum){
    if (l == r){
    	if (sum >= seg[id].s.val){
        	return seg[id].s.t;
    	}else{
    		return 0;
    	}
    }
    int mid = (l + r) / 2;
    ll cur = 0;
    if (seg[id * 2].s.val >= sum){
    	cur += query(id * 2, l, mid, sum);
    }else{
    	cur += seg[id * 2].s.t;
    	sum -= cur;
    	cur += query(id * 2 + 1, mid + 1, r, sum);
    }
    return cur;
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int n, q;
	std::cin >> n >> q;

	std::vector<int> a(n + 1);
	ll sum = 0;
	std::map<int, std::pair<std::vector<int>, int>> pos;
	for (int i = 1; i <= n; i++){
		std::cin >> a[i];
		if (a[i] > 0){
			val[++m] = a[i];
		}else{
			sum += (-a[i]);
		}
	}
	std::vector<std::pair<int, int>> evt(q); 
	for (int i = 0; i < q; i++){
		int x, v;
		std::cin >> x >> v;
		evt[i] = {x, v};
		if (v > 0){
			val[++m] = v;
		}
	}	
	std::sort(val + 1, val + m + 1);
	for (int i = 1; i <= m; i++){
		pos[val[i]].first.push_back(i);
		pos[val[i]].second = -1;
	}
	// for (auto [v , vec] : pos){
	// 	std::cout << v << " : ";
	// 	for (auto x : vec.first){
	// 		std::cout << x << ' ';
	// 	}
	// 	std::cout << '\n';
	// }
	build(1, 1, m);
	for (int i = 1; i <= n; i++){
		if (a[i] <= 0) continue;
		int idx = pos[a[i]].first[++pos[a[i]].second];
		change(1, 1, m, idx, 1);
	}
	// std::cerr << "SSS";
	// std::cout << seg[1].s.val << ' ';
	// std::cerr << query(1, 1, m, sum) << '\n';
	for (auto [x, v] : evt){
		if (a[x] > 0 && v > 0){
			int curpos = pos[a[x]].first[pos[a[x]].second--];
			change(1, 1, m, curpos, 0);
			// std::cout << seg[1].s.val << ' ';
			int nextpos = pos[v].first[++pos[v].second];
			change(1, 1, m, nextpos, 1);
			a[x] = v;
			// std::cout << seg[1].s.val << '\n';
			// std::cout << curpos << ' ' << nextpos << '\n';
		}else if (a[x] > 0 && v <= 0){
			// std::cout << pos[a[x]].second << ' ';
			// std::cout << pos[a[x]].first[0] << ' ';
			// std::cout << a[x] << " ";
			int curpos = pos[a[x]].first[pos[a[x]].second--];
			// std::cout << curpos << ' ';  
			// std::cout << seg[1].s.val << ' ';
			change(1, 1, m, curpos, 0);
			// std::cout << seg[1].s.val << " ";
			sum += -v;
			a[x] = v;
		}else if (a[x] <= 0 && v > 0){
			sum -= (-a[x]);
			int nextpos = pos[v].first[++pos[v].second];
			change(1, 1, m, nextpos, 1);
			a[x] = v;
		}else if (a[x] <= 0 && v <= 0){
			sum -= (-a[x]);
			sum += (v);
		}
		// for (int i = 1; i <= n; i++){
		// 	std::cout << a[i] << " \n"[i == n];
		// }
		// std::cout << "[ ";
		// std::cout << sum << " , " << seg[1].s.val << " ] ";
		std::cout << query(1, 1, m, sum) + 1 << '\n';
		// std::cout << "-----------\n";
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3564kb

input:

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

output:

1
2
2
2
3

result:

ok 5 number(s): "1 2 2 2 3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

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

output:

1
2
1
2
1

result:

ok 5 number(s): "1 2 1 2 1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3792kb

input:

1 1
1000000000
1 1000000000

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Memory Limit Exceeded

input:

1 1
-1000000000
1 -1000000000

output:


result: