Submission #376369


Source Code Expand

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

template<typename It>
void make_fenwick(It beg, size_t n) {
	for(size_t i = 0; i < n; i ++) {
		size_t p = i | (i+1);
		if(p < n) beg[p] += beg[i];
	}
}
template<typename It, typename Val>
void add_fenwick(It beg, size_t n, size_t i, Val val) {
	for(; i < n; i |= i+1) beg[i] += val;
}
template<typename It, typename Val>
Val sum_fenwick(It beg, size_t i, Val sum) {
	for(; i > 0; i = i & (i-1)) sum += beg[i-1];
	return sum;
}
template<typename It>
typename std::iterator_traits<It>::value_type sum_fenwick(It beg, size_t i) {
	return sum_fenwick(beg, i, typename std::iterator_traits<It>::value_type());
}

size_t highestOneBit(size_t v) {
	v |= v >> 1;
	v |= v >> 2;
	v |= v >> 4;
	v |= v >> 8;
	v |= v >> 16;
#if SIZE_MAX > 0xffffffffULL
	v |= v >> 32;
#endif
	return (v >> 1) + 1;
}

template<typename It, typename Val, typename Cmp>
size_t search_fenwick(It beg, size_t n, Val val, Val left, Cmp cmp) {
	if(!cmp(left, val)) return 0;
	size_t i = 0;
	for(size_t w = highestOneBit(n); w > 0; w >>= 1) {
		if(i + w <= n) {
			Val mid = left;
			mid += beg[i + w - 1];
			if(cmp(mid, val)) {
				i += w;
				left = mid;
			}
		}
	}
	return i + 1;
}
template<typename It, typename Val>
size_t select_fenwick(It beg, size_t n, Val k) {
	return search_fenwick(beg, n, k + 1, Val(), std::less<Val>()) - 1;
}

struct DynamicRunLength {
	int n, numRuns;
	vector<int> ft;
	vector<int> colors;
	vector<bool> runs;	//runの左端を保持する
	vector<int> ftRuns;

	void init(int X) {
		n = 0, numRuns = 0;
		ft.assign(X, 0);
		colors.assign(X, -1);
		runs.assign(X, false);
		ftRuns.assign(X, 0);
	}

	//ft, colorsに事前にデータを格納しておいてからbuildを呼ぶことで構築することができる
	void build() {
		int X = ft.size();
		assert(colors.size() == X);
		runs.assign(X, false);
		ftRuns.assign(X, 0);
		n = 0, numRuns = 0;

		int prevColor = -1;
		for(int i = 0; i < X; ++ i) {
			assert(ft[i] == 0 || ft[i] == 1);
			assert((ft[i] == 1) == (colors[i] != -1));

			if(ft[i] == 1) {
				++ n;
				int a = colors[i];
				if(a != prevColor) {
					++ numRuns;
					runs[i] = true;
					ftRuns[i] = 1;
					prevColor = a;
				}
			}
		}
		make_fenwick(ft.begin(), ft.size());
		make_fenwick(ftRuns.begin(), ftRuns.size());
	}

	void insert(int x, int a) {
		assert(0 <= x && x < (int)ft.size());
		assert(colors[x] == -1);

		int rank = sum_fenwick(ft.begin(), x);
		if(rank != n) {
			int nextPos = select_fenwick(ft.begin(), ft.size(), rank);
			if(colors[nextPos] == a && runs[nextPos]) {
				-- numRuns;
				runs[nextPos] = false;
				add_fenwick(ftRuns.begin(), ftRuns.size(), nextPos, -1);
			}else if(colors[nextPos] != a && !runs[nextPos]) {
				++ numRuns;
				runs[nextPos] = true;
				add_fenwick(ftRuns.begin(), ftRuns.size(), nextPos, +1);
			}
		}
		bool newRun = true;
		if(rank != 0) {
			int prevPos = select_fenwick(ft.begin(), ft.size(), rank - 1);
			if(colors[prevPos] == a)
				newRun = false;
		}
		if(newRun) {
			++ numRuns;
			runs[x] = true;
			add_fenwick(ftRuns.begin(), ftRuns.size(), x, +1);
		}

		add_fenwick(ft.begin(), ft.size(), x, +1);
		colors[x] = a;
		++ n;
	}

	void erase(int x) {
		assert(0 <= x && x < (int)ft.size());
		assert(colors[x] != -1);
		int a = colors[x];

		if(runs[x]) {
			-- numRuns;
			runs[x] = false;
			add_fenwick(ftRuns.begin(), ftRuns.size(), x, -1);

			int rank = sum_fenwick(ft.begin(), x);
			if(rank != n - 1) {
				int nextPos = select_fenwick(ft.begin(), ft.size(), rank + 1);
				if(colors[nextPos] == a) {
					assert(!runs[nextPos]);
					++ numRuns;
					runs[nextPos] = true;
					add_fenwick(ftRuns.begin(), ftRuns.size(), nextPos, +1);
				}else {
					assert(runs[nextPos]);
					if(rank != 0) {
						int prevPos = select_fenwick(ft.begin(), ft.size(), rank - 1);
						if(colors[prevPos] == colors[nextPos]) {
							-- numRuns;
							runs[nextPos] = false;
							add_fenwick(ftRuns.begin(), ftRuns.size(), nextPos, -1);
						}
					}
				}
			}
		}

		add_fenwick(ft.begin(), ft.size(), x, -1);
		colors[x] = -1;
		-- n;
	}

	//左端から(k+1)番目のrunの左端を返す
	int selectRunLeft(int k) const {
		if(k >= numRuns) return INF;
		return select_fenwick(ftRuns.begin(), ftRuns.size(), k);
	}

	//右端から(k+1)番目のrunの右端を返す
	int selectRunRight(int k) const {
		if(k >= numRuns) return INF;
		if(k == 0)
			return select_fenwick(ft.begin(), ft.size(), n - 1);
		int i = select_fenwick(ftRuns.begin(), ftRuns.size(), numRuns - k);
		int rank = sum_fenwick(ft.begin(), i);
		return select_fenwick(ft.begin(), ft.size(), rank - 1);
	}
};

struct Query {
	int ty, x, a;
};

int main() {
	int N;
	scanf("%d", &N);
	vector<Query> queries;
	rep(i, N) {
		int x, a;
		scanf("%d%d", &x, &a);
		queries.push_back({ 0, x, a });
	}
	int Q;
	scanf("%d", &Q);
	set<pii> signs;
	rep(i, Q) {
		int c, y, b;
		scanf("%d%d%d", &c, &y, &b);
		queries.push_back({ c, y, b });
	}
	vector<int> xs(N + Q);
	rep(i, N + Q)
		xs[i] = queries[i].x;
	sort(all(xs));
	xs.erase(unique(all(xs)), xs.end());
	int X = xs.size();
	const int C = 200001;
	int totalCnt = 0;
	long long totalSum = 0;
	vector<int> cnt(C, 0);
	vector<int> ftCnt(C, 0);
	vector<long long> ftSum(C, 0);
	DynamicRunLength runs;
	runs.init(X);
	signs.clear();
	rep(i, N) {
		const Query &q = queries[i];
		++ totalCnt;
		totalSum += q.a;
		++ cnt[q.a];
		++ ftCnt[q.a];
		ftSum[q.a] += q.a;
		int xi = lower_bound(all(xs), q.x) - xs.begin();
		++ runs.ft[xi];
		runs.colors[xi] = q.a;
	}
	make_fenwick(ftCnt.begin(), ftCnt.size());
	make_fenwick(ftSum.begin(), ftSum.size());
	runs.build();
	rep(i, Q) {
		const Query &q = queries[N + i];
		if(q.ty == 1) {
			signs.insert(mp(q.x, q.a));
			++ totalCnt;
			totalSum += q.a;
			++ cnt[q.a];
			add_fenwick(ftCnt.begin(), ftCnt.size(), q.a, +1);
			add_fenwick(ftSum.begin(), ftSum.size(), q.a, +q.a);
			int xi = lower_bound(all(xs), q.x) - xs.begin();
			runs.insert(xi, q.a);
		}else if(q.ty == 2) {
			signs.erase(mp(q.x, q.a));
			-- totalCnt;
			totalSum -= q.a;
			-- cnt[q.a];
			add_fenwick(ftCnt.begin(), ftCnt.size(), q.a, -1);
			add_fenwick(ftSum.begin(), ftSum.size(), q.a, -q.a);
			int xi = lower_bound(all(xs), q.x) - xs.begin();
			runs.erase(xi);
		}

		int leftPos = runs.selectRunLeft(0);
		int leftColor = runs.colors[leftPos];

		int rightPos = runs.selectRunRight(0);
		int rightColor = runs.colors[rightPos];

		int medianColor = select_fenwick(ftCnt.begin(), ftCnt.size(), (totalCnt - 1) / 2);

		int candidateColors[3] = { leftColor, rightColor, medianColor };
		sort(candidateColors, candidateColors + 3);
		int K = unique(candidateColors, candidateColors + 3) - candidateColors;

		long long ans = INFL;
		rep(k, K) {
			int color = candidateColors[k];

			if(cnt[color] == totalCnt) {
				ans = 0;
				break;
			}

			int left, right;

			if(color != leftColor)
				left = xs[leftPos];
			else
				left = xs[runs.selectRunLeft(1)];

			if(color != rightColor)
				right = xs[rightPos];
			else
				right = xs[runs.selectRunRight(1)];

			if(0 < left) left = 0;
			if(right < 0) right = 0;

			int cntL = sum_fenwick(ftCnt.begin(), color);
			long long sumL = sum_fenwick(ftSum.begin(), color);
			int cntR = totalCnt - cntL;
			long long sumR = totalSum - sumL;

			long long totalTime = 0;

			totalTime += (long long)min(-left, right) + (right - left);

			totalTime += (long long)color * cntL - sumL;
			totalTime += sumR - (long long)color * cntR;

			amin(ans, totalTime);
		}

		printf("%lld\n", ans);
	}
	return 0;
}

Submission Info

Submission Time
Task J - 看板の塗り替え
User anta
Language C++11 (GCC 4.9.2)
Score 300
Code Size 9004 Byte
Status AC
Exec Time 493 ms
Memory 14100 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:229:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
                 ^
./Main.cpp:233: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:237:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
                 ^
./Main.cpp:241: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
AC × 26
AC × 53
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 33 ms 3872 KB
scrambled_01.txt AC 470 ms 14100 KB
scrambled_02.txt AC 373 ms 11284 KB
scrambled_03.txt AC 215 ms 8600 KB
scrambled_04.txt AC 157 ms 7056 KB
scrambled_05.txt AC 141 ms 7196 KB
scrambled_06.txt AC 387 ms 8212 KB
scrambled_07.txt AC 62 ms 4372 KB
scrambled_08.txt AC 313 ms 7696 KB
scrambled_09.txt AC 183 ms 6036 KB
scrambled_10.txt AC 71 ms 5144 KB
scrambled_11.txt AC 345 ms 10644 KB
scrambled_12.txt AC 205 ms 8340 KB
scrambled_13.txt AC 96 ms 5540 KB
scrambled_14.txt AC 135 ms 6928 KB
scrambled_15.txt AC 133 ms 6040 KB
scrambled_16.txt AC 422 ms 10640 KB
scrambled_17.txt AC 233 ms 7564 KB
scrambled_18.txt AC 100 ms 5536 KB
scrambled_19.txt AC 257 ms 7188 KB
scrambled_20.txt AC 135 ms 5788 KB
scrambled_21.txt AC 493 ms 14100 KB
scrambled_22.txt AC 443 ms 10640 KB
scrambled_23.txt AC 120 ms 5532 KB
scrambled_24.txt AC 347 ms 9104 KB
scrambled_25.txt AC 260 ms 7828 KB
scrambled_26.txt AC 317 ms 6688 KB
subtask_00.txt AC 38 ms 4128 KB
subtask_01.txt AC 37 ms 4060 KB
subtask_02.txt AC 36 ms 4004 KB
subtask_03.txt AC 33 ms 3876 KB
subtask_04.txt AC 34 ms 3932 KB
subtask_05.txt AC 37 ms 3996 KB
subtask_06.txt AC 33 ms 3872 KB
subtask_07.txt AC 36 ms 4004 KB
subtask_08.txt AC 32 ms 3876 KB
subtask_09.txt AC 32 ms 3868 KB
subtask_10.txt AC 35 ms 4004 KB
subtask_11.txt AC 31 ms 3996 KB
subtask_12.txt AC 34 ms 4008 KB
subtask_13.txt AC 31 ms 3996 KB
subtask_14.txt AC 34 ms 3876 KB
subtask_15.txt AC 37 ms 4000 KB
subtask_16.txt AC 34 ms 4008 KB
subtask_17.txt AC 34 ms 4004 KB
subtask_18.txt AC 32 ms 3872 KB
subtask_19.txt AC 36 ms 4004 KB
subtask_20.txt AC 38 ms 4124 KB
subtask_21.txt AC 36 ms 4064 KB
subtask_22.txt AC 32 ms 4004 KB
subtask_23.txt AC 37 ms 4128 KB
subtask_24.txt AC 31 ms 3872 KB
subtask_25.txt AC 35 ms 4004 KB