답안 #1027040

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1027040 2024-07-18T19:14:10 Z vjudge1 Sličnost (COI23_slicnost) C++17
50 / 100
147 ms 8436 KB
#include <bits/stdc++.h>
using namespace std;

void f(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif
}

struct segtree{
	int n, tl, tr, val, pos, check; 
	vector<int> roots, left, right;

	vector<array<int , 3>> st;


	array<int, 3> TxT(array<int, 3> l, array<int, 3> r){
		
		array<int, 3> res={0, 0, 0};
	
		res[0]=l[0]+r[0];
		res[1]=max(l[1], l[0]+r[1]);

		if(l[1]==res[1]) res[2]+=l[2];
		if(r[1]+l[0]==res[1]) res[2]+=r[2];

		return res;
	}

	int add(){
		int id=st.size();
		st.push_back({0, 0, 0});
		left.push_back(-1);
		right.push_back(-1);
		return id;
	}

	segtree(int _n){
		n=_n;

		int v=add();

		roots.push_back(v);
		build(v, 0, n-1);
	}

	void build(int v, int l, int r){
		if(l==r) st[v]={0, 0, 1};
		else{
			int mid=(l+r)/2;
			if(left[v]==-1) left[v]=add();
			if(right[v]==-1) right[v]=add();

			build(left[v], l, mid);
			build(right[v], mid+1, r);

			st[v]=TxT(st[left[v]], st[right[v]]);
		}
	}


	void update(int v, int l, int r, int nv){
		if(l==r){
			if(v!=nv){
				st[nv]=st[v];
			}
			st[nv][0]+=val; st[nv][1]+=val;
		}
		else{
			int mid=(l+r) / 2;

			if(check){
				if(pos<=mid){
					update(left[v], l, mid, left[nv]);
				}
				else{
					update(right[v], mid+1, r, right[nv]);
				}
				st[nv]=TxT(st[left[nv]], st[right[nv]]);

			}
			else{	
				if(pos<=mid){
					left[nv]=add();
					right[nv]=right[v];
					update(left[v], l, mid, left[nv]);
				}
				else{
					left[nv]=left[v];
					right[nv]=add();
					update(right[v], mid+1, r, right[nv]);
				}

				st[nv]=TxT(st[left[nv]], st[right[nv]]);
			}
		}


	}
	int upd(int p, int l, int v){

		if(check==0){
			pos=l, val=v;
			int id=add();
			roots.push_back(id);
			update(p, 0, n-1, id);
			return id;
		}
		else{
			pos=l, val=v;
			update(p, 0, n-1, p);
			return p;
		}
	}

	array<int, 3> g(int v, int l, int r){
		if(l>tr||r<tl) return {0, 0, 0};
		else if(l>=tl&&r<=tr) return st[v];
		else{
			int mid=(l+r) /2;
			return TxT(g(left[v], l, mid), g(right[v], mid+1, r));

		}
	}
	array<int, 2> get(int pos, int l, int r){
		tl = l; tr = r;
		auto x= g(pos, 0, n-1);

		return {x[1], x[2]};
	}
};

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	//f();

	int n, k, q; cin >> n >> k >> q;

	segtree st(n+1);

	vector<int> a(n), b(n), pos(n), segt;
	int c=0;
	for(auto &i: a) cin >> i, i--;
	for(auto &i: b) cin >> i, i--, pos[i]=c++;

	auto ek=[&](int y, int i){
		int r=pos[i];

		int x=st.upd(y, max(0, r-k+1), 1);
		st.upd(x, r+1, -1);
		return (int) st.roots.back();
	
	};
	auto cik=[&](int y, int i){
		int r=pos[i];
		int x= st.upd(y, max(0, r-k+1), -1);
		st.upd(x, r+1, 1);
		return (int)st.roots.back();
	};

	for(int i=0; i<k; i++){
		ek(st.roots.back(), a[i]);
	}
	segt.push_back(st.roots.back());
	
	vector<array<int,2>> all(n-k+1);
	vector<long long> pf(n+3);

	auto x=st.get(st.roots.back(), 0, n-k);
	all[0]=x;
	pf[x[0]]+=x[1];
	int ma=x[0];

	for(int i=k; i<n; i++){
		cik(st.roots.back(), a[i-k]); 
		segt.push_back(ek(st.roots.back(), a[i]));
		auto y=st.get(segt.back(), 0, n-k);
		all[i-k+1]=y;
		pf[y[0]]+=y[1];

		ma=max(ma, y[0]);		
	}
		
	cout<<ma<<" "<<pf[ma]<<"\n";

	auto cikar=[&](int v, int value){
		if(v<0) return;
		
		pf[all[v][0]] -= all[v][1];
		auto x=st.get(segt[v], 0, n-k);
		all[v] = x;
		pf[all[v][0]] += all[v][1];
	};

	auto cikar2=[&](int v, int value){
		if(v>n-k) return;
		
		pf[all[v][0]] -= all[v][1];
		auto x=st.get(segt[v], 0, n-k);
		all[v] = x;
		pf[all[v][0]] += all[v][1];
	};
	auto ekle=[&](int v, int value){
		if(v<0) return;
		
		pf[all[v][0]] -= all[v][1];
		auto x=st.get(segt[v], 0, n-k);

		all[v] = x;
		pf[all[v][0]] += all[v][1];
	};

	auto ekle2=[&](int v, int value){
		if(v>n-k) return;
		
		pf[all[v][0]] -= all[v][1];

		auto x=st.get(segt[v], 0, n-k);

		all[v] = x;
		pf[all[v][0]] += all[v][1];
	};

	st.check=1;
	while(q--){	
		int p; cin >> p; p--;
		
		cikar(p-k+1, a[p]);
		cikar2(p+1, a[p+1]);

		swap(a[p], a[p+1]);

		ekle(p-k+1, a[p]);
		ekle2(p+1, a[p+1]);

		if(pf[ma+1]>0) ma++;
		else if(pf[ma]==0) ma--;

		cout<<ma<<" "<<pf[ma]<<"\n";
	}
}

Compilation message

Main.cpp: In function 'void f()':
Main.cpp:6:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 |  freopen("in.txt", "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:7:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 |  freopen("out.txt", "w", stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 848 KB Output is correct
17 Correct 4 ms 876 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 3 ms 848 KB Output is correct
20 Correct 6 ms 848 KB Output is correct
21 Correct 3 ms 848 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 5 ms 848 KB Output is correct
24 Correct 5 ms 848 KB Output is correct
25 Correct 4 ms 848 KB Output is correct
26 Correct 4 ms 848 KB Output is correct
27 Correct 5 ms 848 KB Output is correct
28 Correct 5 ms 848 KB Output is correct
29 Correct 5 ms 788 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 848 KB Output is correct
17 Correct 4 ms 876 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 3 ms 848 KB Output is correct
20 Correct 6 ms 848 KB Output is correct
21 Correct 3 ms 848 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 5 ms 848 KB Output is correct
24 Correct 5 ms 848 KB Output is correct
25 Correct 4 ms 848 KB Output is correct
26 Correct 4 ms 848 KB Output is correct
27 Correct 5 ms 848 KB Output is correct
28 Correct 5 ms 848 KB Output is correct
29 Correct 5 ms 788 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 110 ms 7920 KB Output is correct
32 Correct 96 ms 7664 KB Output is correct
33 Correct 49 ms 6384 KB Output is correct
34 Correct 82 ms 6900 KB Output is correct
35 Correct 144 ms 7868 KB Output is correct
36 Correct 70 ms 8436 KB Output is correct
37 Correct 37 ms 6236 KB Output is correct
38 Correct 147 ms 7660 KB Output is correct
39 Correct 89 ms 7924 KB Output is correct
40 Correct 98 ms 7304 KB Output is correct
41 Correct 109 ms 7488 KB Output is correct
42 Correct 132 ms 8228 KB Output is correct
43 Correct 90 ms 7668 KB Output is correct
44 Correct 79 ms 7156 KB Output is correct
45 Correct 53 ms 6160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Incorrect 0 ms 348 KB Output isn't correct
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 848 KB Output is correct
17 Correct 4 ms 876 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 3 ms 848 KB Output is correct
20 Correct 6 ms 848 KB Output is correct
21 Correct 3 ms 848 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 5 ms 848 KB Output is correct
24 Correct 5 ms 848 KB Output is correct
25 Correct 4 ms 848 KB Output is correct
26 Correct 4 ms 848 KB Output is correct
27 Correct 5 ms 848 KB Output is correct
28 Correct 5 ms 848 KB Output is correct
29 Correct 5 ms 788 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Incorrect 0 ms 348 KB Output isn't correct
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 848 KB Output is correct
17 Correct 4 ms 876 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 3 ms 848 KB Output is correct
20 Correct 6 ms 848 KB Output is correct
21 Correct 3 ms 848 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 5 ms 848 KB Output is correct
24 Correct 5 ms 848 KB Output is correct
25 Correct 4 ms 848 KB Output is correct
26 Correct 4 ms 848 KB Output is correct
27 Correct 5 ms 848 KB Output is correct
28 Correct 5 ms 848 KB Output is correct
29 Correct 5 ms 788 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 110 ms 7920 KB Output is correct
32 Correct 96 ms 7664 KB Output is correct
33 Correct 49 ms 6384 KB Output is correct
34 Correct 82 ms 6900 KB Output is correct
35 Correct 144 ms 7868 KB Output is correct
36 Correct 70 ms 8436 KB Output is correct
37 Correct 37 ms 6236 KB Output is correct
38 Correct 147 ms 7660 KB Output is correct
39 Correct 89 ms 7924 KB Output is correct
40 Correct 98 ms 7304 KB Output is correct
41 Correct 109 ms 7488 KB Output is correct
42 Correct 132 ms 8228 KB Output is correct
43 Correct 90 ms 7668 KB Output is correct
44 Correct 79 ms 7156 KB Output is correct
45 Correct 53 ms 6160 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Incorrect 0 ms 348 KB Output isn't correct
48 Halted 0 ms 0 KB -