Submission #372156
Source Code Expand
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define MOD @
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec;
struct segtree
{
static const int DEPTH = 18;
static const int SIZE = 1 << DEPTH;
i64 sum[2 * SIZE]; int cnt[2 * SIZE];
void init()
{
for (int i = 0; i < 2 * SIZE; ++i) {
sum[i] = 0; cnt[i] = 0;
}
}
void add(int p, int v)
{
sum[p + SIZE] += (i64)p * v;
p += SIZE;
cnt[p] += v;
p >>= 1;
while (p) {
sum[p] = sum[p * 2] + sum[p * 2 + 1];
cnt[p] = cnt[p * 2] + cnt[p * 2 + 1];
p >>= 1;
}
}
pair<i64, int> query(int L, int R)
{
i64 r1 = 0;
int r2 = 0;
L += SIZE; R += SIZE;
while (L < R) {
if (L & 1) {
r1 += sum[L]; r2 += cnt[L];
L++;
}
if (R & 1) {
--R;
r1 += sum[R]; r2 += cnt[R];
}
L >>= 1; R >>= 1;
}
return make_pair(r1, r2);
}
};
const int OFS = 1000000000;
int N, Q;
set<int> locs[200100];
set<pair<int, int> > colors_gtr, colors_les;
segtree seg;
void flush_color(int a)
{
if (locs[a].size() > 0) {
int lg = *(--locs[a].end());
int sm = *(locs[a].begin());
colors_gtr.erase(make_pair(lg, a));
colors_les.erase(make_pair(sm, a));
}
}
void adjust_color(int a)
{
if (locs[a].size() > 0) {
int lg = *(--locs[a].end());
int sm = *(locs[a].begin());
colors_gtr.insert(make_pair(lg, a));
colors_les.insert(make_pair(sm, a));
}
}
void add_board(int x, int a)
{
seg.add(a, 1);
flush_color(a);
locs[a].insert(x);
adjust_color(a);
}
void erase_board(int x, int a)
{
seg.add(a, -1);
flush_color(a);
locs[a].erase(x);
adjust_color(a);
}
i64 solve(int target)
{
if (target == -1) return 1LL << 62LL;
auto gt = --colors_gtr.end();
if (gt->second == target) --gt;
auto le = colors_les.begin();
if (le->second == target) ++le;
int gtl = gt->first, lel = -le->first;
i64 mv = (i64)min(gtl, lel) + gtl + lel;
// printf("%d %d %d\n", target, gtl, lel);
auto colorl = seg.query(0, target), colorg = seg.query(target, 200200);
i64 ret = mv;
ret += (i64)colorl.second * target - colorl.first;
ret += colorg.first - (i64)colorg.second * target;
return ret;
}
int med()
{
int tot = (seg.query(0, 200200).second + 1) / 2;
int left = 0, right = 200200;
while (left < right) {
int mid = (left + right) / 2;
if (seg.query(0, mid + 1).second < tot) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
int main()
{
seg.init();
colors_gtr.insert(make_pair(0, -1));
colors_les.insert(make_pair(0, -1));
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
int x, a;
scanf("%d%d", &x, &a);
add_board(x, a);
}
scanf("%d", &Q);
for (int i = 0; i < Q; ++i) {
int c, y, b;
scanf("%d%d%d", &c, &y, &b);
if (c == 1) {
add_board(y, b);
} else {
erase_board(y, b);
}
i64 ret = 1LL << 62LL;
ret = min(ret, solve((--colors_gtr.end())->second));
ret = min(ret, solve((colors_les.begin())->second));
ret = min(ret, solve(med()));
//printf("med: %d\n", med());
printf("%lld\n", ret);
}
return 0;
}
Submission Info
Submission Time |
|
Task |
J - 看板の塗り替え |
User |
semiexp |
Language |
C++11 (GCC 4.9.2) |
Score |
300 |
Code Size |
3414 Byte |
Status |
AC |
Exec Time |
807 ms |
Memory |
44452 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:155:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
^
./Main.cpp:158:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &a);
^
./Main.cpp:162:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Q);
^
./Main.cpp:166:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &c, &y, &b);
^
Judge Result
Set Name |
Subtask |
All |
Score / Max Score |
30 / 30 |
270 / 270 |
Status |
|
|
Set Name |
Test Cases |
Subtask |
subtask_00.txt, subtask_01.txt, subtask_02.txt, subtask_03.txt, subtask_04.txt, subtask_05.txt, subtask_06.txt, subtask_07.txt, subtask_08.txt, subtask_09.txt, subtask_10.txt, subtask_11.txt, subtask_12.txt, subtask_13.txt, subtask_14.txt, subtask_15.txt, subtask_16.txt, subtask_17.txt, subtask_18.txt, subtask_19.txt, subtask_20.txt, subtask_21.txt, subtask_22.txt, subtask_23.txt, subtask_24.txt, subtask_25.txt |
All |
scrambled_00.txt, scrambled_01.txt, scrambled_02.txt, scrambled_03.txt, scrambled_04.txt, scrambled_05.txt, scrambled_06.txt, scrambled_07.txt, scrambled_08.txt, scrambled_09.txt, scrambled_10.txt, scrambled_11.txt, scrambled_12.txt, scrambled_13.txt, scrambled_14.txt, scrambled_15.txt, scrambled_16.txt, scrambled_17.txt, scrambled_18.txt, scrambled_19.txt, scrambled_20.txt, scrambled_21.txt, scrambled_22.txt, scrambled_23.txt, scrambled_24.txt, scrambled_25.txt, scrambled_26.txt, subtask_00.txt, subtask_01.txt, subtask_02.txt, subtask_03.txt, subtask_04.txt, subtask_05.txt, subtask_06.txt, subtask_07.txt, subtask_08.txt, subtask_09.txt, subtask_10.txt, subtask_11.txt, subtask_12.txt, subtask_13.txt, subtask_14.txt, subtask_15.txt, subtask_16.txt, subtask_17.txt, subtask_18.txt, subtask_19.txt, subtask_20.txt, subtask_21.txt, subtask_22.txt, subtask_23.txt, subtask_24.txt, subtask_25.txt |
Case Name |
Status |
Exec Time |
Memory |
scrambled_00.txt |
AC |
52 ms |
16288 KB |
scrambled_01.txt |
AC |
807 ms |
37536 KB |
scrambled_02.txt |
AC |
482 ms |
30112 KB |
scrambled_03.txt |
AC |
367 ms |
28320 KB |
scrambled_04.txt |
AC |
242 ms |
24608 KB |
scrambled_05.txt |
AC |
325 ms |
28576 KB |
scrambled_06.txt |
AC |
640 ms |
28320 KB |
scrambled_07.txt |
AC |
104 ms |
18204 KB |
scrambled_08.txt |
AC |
564 ms |
27548 KB |
scrambled_09.txt |
AC |
308 ms |
23456 KB |
scrambled_10.txt |
AC |
152 ms |
21924 KB |
scrambled_11.txt |
AC |
439 ms |
21020 KB |
scrambled_12.txt |
AC |
289 ms |
20384 KB |
scrambled_13.txt |
AC |
141 ms |
17316 KB |
scrambled_14.txt |
AC |
202 ms |
20260 KB |
scrambled_15.txt |
AC |
181 ms |
17700 KB |
scrambled_16.txt |
AC |
471 ms |
20896 KB |
scrambled_17.txt |
AC |
276 ms |
18984 KB |
scrambled_18.txt |
AC |
143 ms |
17832 KB |
scrambled_19.txt |
AC |
277 ms |
17564 KB |
scrambled_20.txt |
AC |
162 ms |
17572 KB |
scrambled_21.txt |
AC |
755 ms |
44452 KB |
scrambled_22.txt |
AC |
711 ms |
28320 KB |
scrambled_23.txt |
AC |
172 ms |
19096 KB |
scrambled_24.txt |
AC |
535 ms |
26020 KB |
scrambled_25.txt |
AC |
385 ms |
23336 KB |
scrambled_26.txt |
AC |
345 ms |
17952 KB |
subtask_00.txt |
AC |
61 ms |
16800 KB |
subtask_01.txt |
AC |
59 ms |
16808 KB |
subtask_02.txt |
AC |
58 ms |
16604 KB |
subtask_03.txt |
AC |
53 ms |
16420 KB |
subtask_04.txt |
AC |
55 ms |
16544 KB |
subtask_05.txt |
AC |
59 ms |
16552 KB |
subtask_06.txt |
AC |
51 ms |
16424 KB |
subtask_07.txt |
AC |
58 ms |
16544 KB |
subtask_08.txt |
AC |
54 ms |
16292 KB |
subtask_09.txt |
AC |
51 ms |
16416 KB |
subtask_10.txt |
AC |
57 ms |
16416 KB |
subtask_11.txt |
AC |
52 ms |
16288 KB |
subtask_12.txt |
AC |
55 ms |
16292 KB |
subtask_13.txt |
AC |
49 ms |
16292 KB |
subtask_14.txt |
AC |
56 ms |
16292 KB |
subtask_15.txt |
AC |
57 ms |
16412 KB |
subtask_16.txt |
AC |
54 ms |
16408 KB |
subtask_17.txt |
AC |
55 ms |
16280 KB |
subtask_18.txt |
AC |
55 ms |
16292 KB |
subtask_19.txt |
AC |
57 ms |
16292 KB |
subtask_20.txt |
AC |
59 ms |
16808 KB |
subtask_21.txt |
AC |
59 ms |
16548 KB |
subtask_22.txt |
AC |
72 ms |
16416 KB |
subtask_23.txt |
AC |
69 ms |
16352 KB |
subtask_24.txt |
AC |
56 ms |
16420 KB |
subtask_25.txt |
AC |
59 ms |
16420 KB |