QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#778559 | #7787. Maximum Rating | 4eyebird | TL | 0ms | 0kb | C++17 | 4.1kb | 2024-11-24 15:10:50 | 2024-11-24 15:10:52 |
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Buff ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
//#define int long long
constexpr int MX = 1000000010;
class DST
{
public:
DST()
{
Mxn = MX;
lc.assign(7e6, 0);
rc.assign(7e6, 0);
sg.assign(7e6, 0);
stm.assign(7e6, 0);
}
void update(int p, ll x)
{
int rt = 1;
_update(p, p, x, 1, Mxn, rt);
}
void update(int l, int r, ll x)
{
int rt = 1;
_update(l, r, x, 1, Mxn, rt);
}
ll query(int p)
{
return _query(p, p, 1, Mxn, 1);
}
ll query(int l, int r)
{
return _query(l, r, 1, Mxn, 1);
}
private:
int idx, Mxn;
vector<ll> stm, sg;
vector<int> lc, rc;
void _update(ll L, ll R, ll x, ll l, ll r, int &pt)
{
int p = pt;
if (!pt)
{
pt = ++idx;
p = pt;
}
stm[p] += x * (R - L + 1);
if (L <= l && r <= R)
{
sg[p] += x;
return;
}
ll mid = (l + r) >> 1;
if (R <= mid)
_update(L, R, x, l, mid, lc[p]);
else if (L > mid)
_update(L, R, x, mid + 1, r, rc[p]);
else
{
_update(L, mid, x, l, mid, lc[p]);
_update(mid + 1, R, x, mid + 1, r, rc[p]);
}
}
ll _query(ll L, ll R, ll l, ll r, int p)
{
if (!p) return 0;
if (L <= l && r <= R)
return stm[p];
ll mid = (l + r) >> 1;
ll ret = sg[p] * (R - L + 1);
if (R <= mid)
ret += _query(L, R, l, mid, lc[p]);
else if (L > mid)
ret += _query(L, R, mid + 1, r, rc[p]);
else
{
ret += _query(L, mid, l, mid, lc[p]);
ret += _query(mid + 1, R, mid + 1, r, rc[p]);
}
return ret;
}
};
map<int, int> Ct;
ll a[200010];
void solve()
{
int n, q;
cin >> n >> q;
DST T1, T2;
ll Sf = 0, Sz = 0, Cz = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (a[i] < 0)
Sf += a[i];
else if (a[i] > 0)
{
Cz++;
Sz += a[i];
Ct[a[i]]++;
T1.update(a[i], MX, a[i]);
T2.update(a[i], MX, 1);
}
}
while (q--)
{
int p;
ll x;
cin >> p >> x;
if (a[p] < 0)
{
Sf -= a[p];
a[p] = x;
}
else
{
Cz--;
Sz -= a[p];
Ct[a[p]]--;
T1.update(a[p], MX, -a[p]);
T2.update(a[p], MX, -1);
a[p] = x;
}
if (x <= 0)
Sf += x;
else
{
Cz++;
Sz += x;
Ct[x]++;
T1.update(x, MX, x);
T2.update(x, MX, 1);
}
if (Sf == 0)
{
cout << "1\n";
continue;
}
if (Sz + Sf <= 0)
{
cout << Cz + 1 << '\n';
continue;
}
auto check = [&](int px) -> bool
{
ll qy = T1.query(px);
if (Sf + qy >= 0)
return true;
else
return false;
};
int l = 0, r = MX;
while (l < r)
{
int mid = (l + r) >> 1;
if (check(mid))
r = mid;
else
l = mid + 1;
}
ll qy = T1.query(l);
int mxc = T2.query(l);
ll ca = qy + Sf;
int cty = Ct[l];
while (cty-- && ca > 0)
{
ca -= l;
mxc--;
}
int ans = mxc + 1;
cout << ans << '\n';
}
}
signed main()
{
#ifdef YGYYYGJ
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
Buff;
int _T_ = 1;
while (_T_--)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 5 1 2 3 3 4 2 -2 1 -3 3 1 2 1