답안 #313500

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
313500 2020-10-16T06:20:14 Z shivensinha4 Nekameleoni (COCI15_nekameleoni) C++17
42 / 140
3000 ms 105080 KB
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#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 22 ms 2176 KB Output is correct
2 Correct 26 ms 2176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 4096 KB Output is correct
2 Correct 90 ms 4096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 139 ms 5664 KB Output is correct
2 Correct 184 ms 5624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1348 ms 21880 KB Output is correct
2 Execution timed out 3030 ms 73976 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 1988 ms 57336 KB Output is correct
2 Execution timed out 3090 ms 99064 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 2730 ms 43640 KB Output is correct
2 Execution timed out 3089 ms 85496 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3085 ms 68904 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3067 ms 63772 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3093 ms 104824 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3084 ms 105080 KB Time limit exceeded
2 Halted 0 ms 0 KB -