QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#117605 | #5441. Quartz Collection | UrgantTeam# | RE | 0ms | 0kb | C++23 | 4.4kb | 2023-07-01 20:06:19 | 2023-07-01 20:06:21 |
Judging History
answer
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second
using namespace std;
typedef long double ld;
typedef long long ll;
#define int long long
int const maxn = 1e5 + 5;
int a[maxn], b[maxn], c[maxn], w[2 * maxn], type[maxn], cnt[(1 << 19)], s1[2][(1 << 19)];
int s2[2][3][(1 << 19)];
map < int, int > add;
int my, alice, need;
void merge(int i, int l, int r) {
cnt[i] = cnt[2 * i + 1] + cnt[2 * i + 2];
s1[1][i] = s1[1][2 * i + 1];
s1[0][i] = s1[0][2 * i + 2];
if (cnt[2 * i + 1] % 2 == 0) {
s1[1][i] += s1[1][2 * i + 2];
s1[0][i] += s1[0][2 * i + 2];
} else {
s1[1][i] += s1[0][2 * i + 2];
s1[0][i] += s1[1][2 * i + 2];
}
for (int start = 0; start <= 1; start++) {
for (int need = 1; need <= 2; need++) {
s2[start][need][i] = s2[start][need][2 * i + 1];
int nS = start, nN = need;
nN -= (cnt[2 * i + 1] % 4);
while (nN <= 0) {
nN += 2;
nS ^= 1;
}
s2[start][need][i] += s2[nS][nN][2 * i + 2];
}
}
}
void update(int i, int l, int r, int lq, int value) {
if (r - l == 1) {
cnt[i] = value;
if (value) s1[1][i] = w[l];
else s1[1][i] = 0;
s1[0][i] = 0;
for (int j = 0; j <= 1; j++) {
for (int z = 0; z <= 2; z++) {
s2[j][z][i] = 0;
if (j == 1 && z && value) s2[j][z][i] = w[l];
}
}
} else {
int m = (r + l) / 2;
if (lq < m) update(2 * i + 1, l, m, lq, value);
else update(2 * i + 2, m, r, lq, value);
merge(i, l, r);
}
}
int get1(int i, int l, int r, int lq, int rq) {
if (lq >= r || l >= rq) return 0;
if (lq <= l && r <= rq) {
int ans = s1[my][i];
my ^= (cnt[i] % 2);
return ans;
}
int m = (r + l) / 2;
return get1(2 * i + 1, l, m, lq, rq) + get1(2 * i + 2, m, r, lq, rq);
}
int get2(int i, int l, int r, int lq, int rq) {
if (lq >= r || l >= rq) return 0;
if (lq <= l && r <= rq) {
int ans = s2[alice][need][i];
need -= (cnt[i] % 4);
while (need <= 0) {
need += 2;
alice ^= 1;
}
return ans;
}
int m = (r + l) / 2;
return get2(2 * i + 1, l, m, lq, rq) + get2(2 * i + 2, m, r, lq, rq);
}
void adds(int v, int t) {
update(0, 0, 2 * maxn, v, t);
}
/*void solve(int n) {
int ans = 0;
for (int i = 1; i <= n; i++) {
ans += b[i];
c[i] = a[i] - b[i];
}
sort(c + 1, c + n + 1);
int ptr = 1;
while (ptr <= n && c[ptr] <= 0) ptr++;
for (int i = 0; i < ptr; i += 4) {
for (int j = i; j <= min(ptr - 1, i + 1); j++) ans += c[j];
}
if ((ptr - 1) % 2 == 1) {
ptr++;
}
for (int i = ptr; i <= n; i += 2) ans += c[i];
cout << ans << '\n';
}*/
main()
{
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m, t, x, y, sum = 0, cntt = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
sum += b[i];
cntt += (a[i] < b[i]);
w[i] = a[i] - b[i];
}
vector < vector < int > > Q;
for (int i = 1; i <= m; i++) {
cin >> t >> x >> y;
w[i + n] = x - y;
Q.push_back({t, x, y});
}
sort(w + 1, w + n + m + 1);
for (int i = 1; i <= n; i++) {
type[i] = lower_bound(w + 1, w + n + m + 1, a[i] - b[i]) - w + add[a[i] - b[i]];
add[a[i] - b[i]]++;
adds(type[i], 1);
}
for (int i = 0; i <= m; i++) {
if (i) {
t = Q[i - 1][0], x = Q[i - 1][1], y = Q[i - 1][2];
sum -= b[t], cntt -= (a[t] < b[t]), adds(type[t], 0);
a[t] = x, b[t] = y;
type[t] = lower_bound(w + 1, w + n + m + 1, a[t] - b[t]) - w + add[a[t] - b[t]], add[a[t] - b[t]]++;
sum += b[t], cntt += (a[t] < b[t]), adds(type[t], 1);
}
int pos = lower_bound(w + 1, w + n + m + 1, 0) - w;
alice = 1, need = 1;
int ans = get2(0, 0, 2 * maxn, 0, pos) + sum;
if (cntt % 2 == 0) my = 1, ans += get1(0, 0, 2 * maxn, pos, 2 * maxn);
else my = 0, ans += get1(0, 0, 2 * maxn, pos, 2 * maxn);
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
4 5 2 4 5 7 1 7 2 1 4 5 2 1 6 2 4 4 3 2 1 3 3 6 6