답안 #313497

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
313497 2020-10-16T06:18:30 Z shivensinha4 Nekameleoni (COCI15_nekameleoni) C++17
42 / 140
3000 ms 105788 KB
#include <bits/stdc++.h> 
using namespace std; 
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
#define endl '\n'

int n, k, m;
const int INF = 1e9;

struct Node {
	vi pref, suf;
	int ans = INF, len;
};

class SegmentTree {
	private:
		vector<Node> tree; vi raw; int n;
		Node bound;
		
		Node merge(Node a, Node b) {
			vector<ii> seq(k);
			for_(i, 0, k) seq[i] = {a.suf[i], i};
			sort(seq.begin(), seq.end());
			vi sufMax(k);
			for (int i = k-1; i >= 0; i--) {
				sufMax[i] = b.pref[seq[i].second];
				if (i < k-1) sufMax[i] = max(sufMax[i], sufMax[i+1]);
			}
			
			Node curr = bound;
			curr.ans = min(a.ans, b.ans); 
			curr.len = a.len+b.len;
			curr.pref = a.pref; curr.suf = b.suf;
			
			for_(i, 0, k-1) curr.ans = min(curr.ans, seq[i].first + sufMax[i+1]);
			
			for_(i, 0, k) {
				curr.pref[i] = min(curr.pref[i], b.pref[i]+a.len);
				curr.suf[i] = min(curr.suf[i], a.suf[i]+b.len);
			}
			
			return curr;
		}
		
		void buildTree(int l, int r, int p) {
			if (l == r) {
				tree[p] = bound;
				if (raw[l] < k) {
					tree[p].pref[raw[l]] = 1;
					tree[p].suf[raw[l]] = 1;
				}
				if (k == 1 and raw[l] == 1) tree[p].ans = 1;
				return;
			}
			
			int mid = (l + r) / 2;
			int c1 = 2*p+1, c2 = 2*p+2;
			buildTree(l, mid, c1); buildTree(mid+1, r, c2);
			tree[p] = merge(tree[c1], tree[c2]);
		}
		
		void update(int i, int val, int l, int r, int p) {
			if (l > i or r < i) return;
			if (l == r) {
				tree[p] = bound;
				if (val < k) {
					tree[p].pref[val] = 1;
					tree[p].suf[val] = 1;
				}
				if (k == 1 and val == 1) tree[p].ans = 1;
				return;
			}
			
			int mid = (l + r) / 2;
			int c1 = 2*p+1, c2 = 2*p+2;
			update(i, val, l, mid, c1); update(i, val, mid+1, r, c2);
			tree[p] = merge(tree[c1], tree[c2]);
		}
		
		//Node ans(int i, int j, int l, int r, int p) {
			//if (l > j or r < i) return bound;
			//if (l >= i and r <= j) return tree[p];
			
			//int mid = (l + r) / 2;
			//int c1 = 2*p+1, c2 = 2*p+2;
			//return merge(ans(i, j, l, mid, c1), ans(i, j, mid+1, r, c2));
		//}
	
	public: 
		SegmentTree(vi input) {
			raw = input;
			n = raw.size();
			tree.resize(4*n);
			bound.pref.resize(k, INF); bound.suf.resize(k, INF); bound.len = 1;
			buildTree(0, n-1, 0);
		}
		
		int ans(int i, int j) {
			return tree[0].ans;
		}
		
		void update(int i, int val) {
			update(i, val, 0, n-1, 0);
		}
};




int main() {
	#ifdef shiven
	freopen("test.in", "r", stdin);
	#endif
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n >> k >> m;
	vi nums(n);
	for_(i, 0, n) {
		cin >> nums[i];
		nums[i] -= 1;
	}
	
	SegmentTree tree(nums);
	for_(q, 0, m) {
		int t; cin >> t;
		if (t == 2) {
			int ans = tree.ans(0, n-1);
			if (ans == INF) cout << -1;
			else cout << ans;
			cout << endl;
		} else {
			int p, v; cin >> p >> v;
			tree.update(p-1, v-1);
		}
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 2176 KB Output is correct
2 Correct 25 ms 2176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 4096 KB Output is correct
2 Correct 82 ms 4096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 5632 KB Output is correct
2 Correct 162 ms 5632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1190 ms 22264 KB Output is correct
2 Execution timed out 3081 ms 75124 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 1770 ms 57908 KB Output is correct
2 Execution timed out 3090 ms 100192 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 2411 ms 44756 KB Output is correct
2 Execution timed out 3078 ms 86760 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 2892 ms 69764 KB Output is correct
2 Execution timed out 3074 ms 90936 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 2872 ms 64776 KB Output is correct
2 Execution timed out 3083 ms 96020 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3085 ms 105788 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3099 ms 105788 KB Time limit exceeded
2 Halted 0 ms 0 KB -