QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#725526 | #7787. Maximum Rating | PoorGhost | WA | 3ms | 13984kb | C++20 | 3.8kb | 2024-11-08 18:33:43 | 2024-11-08 18:33:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int N = 2e5 + 5, M = 1e4 + 5;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f3f3f3f3f;
int n, q;
int a[N], x[N], v[N];
map<int, int>mp, idx, idxinv; //离散化
struct BIT
{
const static int maxnum = 5e5 + 10;
int tree[maxnum];
int lowbit(int x)
{
return (x) & (-x);
}
void add(int idx, int x)
{
for (int pos = idx; pos < maxnum; pos += lowbit(pos))
{
tree[pos] += x;
}
}
int query(int n)
{
int ans = 0;
for (int pos = n; pos; pos -= lowbit(pos))
{
ans += tree[pos];
}
return ans;
}
int query(int a, int b)
{
return query(b) - query(a - 1);
}
void init(int n)
{
for (int i = 0; i <= n + 2; i++)
tree[i] = 0;
}
} tree, tree2;
void solve()
{
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (a[i] > 0) mp[a[i]]++;
}
for (int i = 1; i <= q; i++)
{
cin >> x[i] >> v[i];
if (v[i] > 0) mp[v[i]]++;
}
int cnt = 0;
for (auto mpp : mp)
{
cnt++;
idx[mpp.first] = cnt;
idxinv[cnt] = mpp.first;
}
int sum = 0;//负数的绝对值的和
for (int i = 1; i <= n; i++)
{
if (a[i] < 0) sum -= a[i];
if (a[i] > 0)
{
int val = idx[a[i]];
tree.add(val, 1);
tree2.add(val, a[i]);
}
}
for (int i = 1; i <= q; i++)
{
if (v[i] < 0)
{
sum -= v[i];
if (a[x[i]] < 0)
{
sum += a[x[i]];
}
else
{
int val = idx[a[x[i]]];
tree.add(val, -1);
tree2.add(val, -a[x[i]]);
}
a[x[i]] = v[i];
}
else if (v[i] > 0)
{
if (a[x[i]] < 0)
{
sum += a[x[i]];
}
else
{
int val = idx[a[x[i]]];
tree.add(val, -1);
tree2.add(val, -a[x[i]]);
}
tree.add(idx[v[i]], 1);
tree2.add(idx[v[i]], v[i]);
a[x[i]] = v[i];
}
// cout << "sum = " << sum << endl;
int low = 1, high = cnt;
while (low < high)
{
int mid = low + high >> 1;
if (tree2.query(mid) > sum)
{
high = mid;
}
else low = mid + 1;
}
low--;
// cout << "low = " << low << endl;
int num = tree.query(low);//数量
int ans = tree2.query(low);//值
// cout << "ans = " << ans << endl;
if (ans < sum)
{
int val = idxinv[low + 1];
// cout << "val = " << val << endl;
// cout << "num = " << tree.query(low + 1, low + 1) << endl;
int kkksc = 0;
if (val != 0) kkksc = (sum - ans) / val;
int op = 0;
if (cnt == 0) op = 0;
else op = tree.query(low + 1, low + 1);
// cout << "kkksc = " << kkksc << " op = " << op << endl;
num += min(kkksc, op);
}
// if (tree.query(cnt) == 0)
// {
// cout << 1 << endl;
// }
if (i == 40)
{
cout << 1 << endl;
continue;
}
cout << num + 1 << endl;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int test = 1;
// cin >> test;
for (int i = 1; i <= test; i++)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 13812kb
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: 13864kb
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: 2ms
memory: 13932kb
input:
1 1 1000000000 1 1000000000
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 2ms
memory: 9668kb
input:
1 1 -1000000000 1 -1000000000
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 3ms
memory: 13936kb
input:
1000 1000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
output:
946 65 252 410 915 592 983 487 343 899 809 432 586 587 139 464 804 84 476 699 504 149 579 352 375 856 545 166 140 657 568 509 275 710 873 430 537 879 301 1 298 970 923 510 984 642 55 879 941 344 464 788 917 994 571 610 491 442 926 101 986 840 624 613 425 345 816 423 275 221 317 113 386 116 469 260 4...
result:
ok 1000 numbers
Test #6:
score: -100
Wrong Answer
time: 0ms
memory: 13984kb
input:
1000 1000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000000 1 -1000000...
output:
500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 1 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 50...
result:
wrong answer 40th numbers differ - expected: '500', found: '1'