Submission #1029267

# Submission time Handle Problem Language Result Execution time Memory
1029267 2024-07-20T14:35:02 Z berr Sličnost (COI23_slicnost) C++17
100 / 100
1358 ms 504060 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, id; 
	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(){
		return id++;
	}

	segtree(int _n){
		n=_n;
		st.resize(25*(int)(1e6), {0, 0, 0});
		left.resize(25*(int)(1e6), -1);
		right.resize(25*(int)(1e6), -1);
		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){
			st[nv]=st[v];
			st[nv][0]+=val; st[nv][1]+=val;
		}
		else{
			int mid=(l+r) / 2;
			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){
		pos=l, val=v;
		int id=add();
		roots.push_back(id);
		update(p, 0, n-1, id);
		return id;
	}

	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];
		segt[v]=cik(segt[v], value);
		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];
		segt[v]=cik(segt[v], value);
		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];
		segt[v]=ek(segt[v], value);
		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];
		segt[v]=ek(segt[v], value);
		auto x=st.get(segt[v], 0, n-k);

		all[v] = x;
		pf[all[v][0]] += all[v][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);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 182 ms 489552 KB Output is correct
2 Correct 170 ms 489552 KB Output is correct
3 Correct 176 ms 489492 KB Output is correct
4 Correct 185 ms 489556 KB Output is correct
5 Correct 176 ms 489484 KB Output is correct
6 Correct 167 ms 489556 KB Output is correct
7 Correct 175 ms 489560 KB Output is correct
8 Correct 176 ms 489632 KB Output is correct
9 Correct 186 ms 489556 KB Output is correct
10 Correct 173 ms 489668 KB Output is correct
11 Correct 178 ms 489572 KB Output is correct
12 Correct 188 ms 489560 KB Output is correct
13 Correct 185 ms 489452 KB Output is correct
14 Correct 183 ms 489456 KB Output is correct
15 Correct 179 ms 489676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 489552 KB Output is correct
2 Correct 170 ms 489552 KB Output is correct
3 Correct 176 ms 489492 KB Output is correct
4 Correct 185 ms 489556 KB Output is correct
5 Correct 176 ms 489484 KB Output is correct
6 Correct 167 ms 489556 KB Output is correct
7 Correct 175 ms 489560 KB Output is correct
8 Correct 176 ms 489632 KB Output is correct
9 Correct 186 ms 489556 KB Output is correct
10 Correct 173 ms 489668 KB Output is correct
11 Correct 178 ms 489572 KB Output is correct
12 Correct 188 ms 489560 KB Output is correct
13 Correct 185 ms 489452 KB Output is correct
14 Correct 183 ms 489456 KB Output is correct
15 Correct 179 ms 489676 KB Output is correct
16 Correct 193 ms 489872 KB Output is correct
17 Correct 182 ms 490068 KB Output is correct
18 Correct 183 ms 489808 KB Output is correct
19 Correct 187 ms 489948 KB Output is correct
20 Correct 180 ms 489908 KB Output is correct
21 Correct 178 ms 490068 KB Output is correct
22 Correct 169 ms 489864 KB Output is correct
23 Correct 199 ms 489956 KB Output is correct
24 Correct 202 ms 489860 KB Output is correct
25 Correct 160 ms 489788 KB Output is correct
26 Correct 163 ms 489808 KB Output is correct
27 Correct 190 ms 489880 KB Output is correct
28 Correct 175 ms 489892 KB Output is correct
29 Correct 171 ms 489812 KB Output is correct
30 Correct 176 ms 489772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 489552 KB Output is correct
2 Correct 170 ms 489552 KB Output is correct
3 Correct 176 ms 489492 KB Output is correct
4 Correct 185 ms 489556 KB Output is correct
5 Correct 176 ms 489484 KB Output is correct
6 Correct 167 ms 489556 KB Output is correct
7 Correct 175 ms 489560 KB Output is correct
8 Correct 176 ms 489632 KB Output is correct
9 Correct 186 ms 489556 KB Output is correct
10 Correct 173 ms 489668 KB Output is correct
11 Correct 178 ms 489572 KB Output is correct
12 Correct 188 ms 489560 KB Output is correct
13 Correct 185 ms 489452 KB Output is correct
14 Correct 183 ms 489456 KB Output is correct
15 Correct 179 ms 489676 KB Output is correct
16 Correct 193 ms 489872 KB Output is correct
17 Correct 182 ms 490068 KB Output is correct
18 Correct 183 ms 489808 KB Output is correct
19 Correct 187 ms 489948 KB Output is correct
20 Correct 180 ms 489908 KB Output is correct
21 Correct 178 ms 490068 KB Output is correct
22 Correct 169 ms 489864 KB Output is correct
23 Correct 199 ms 489956 KB Output is correct
24 Correct 202 ms 489860 KB Output is correct
25 Correct 160 ms 489788 KB Output is correct
26 Correct 163 ms 489808 KB Output is correct
27 Correct 190 ms 489880 KB Output is correct
28 Correct 175 ms 489892 KB Output is correct
29 Correct 171 ms 489812 KB Output is correct
30 Correct 176 ms 489772 KB Output is correct
31 Correct 291 ms 495992 KB Output is correct
32 Correct 294 ms 495876 KB Output is correct
33 Correct 221 ms 493500 KB Output is correct
34 Correct 280 ms 495568 KB Output is correct
35 Correct 335 ms 496136 KB Output is correct
36 Correct 229 ms 495892 KB Output is correct
37 Correct 199 ms 493520 KB Output is correct
38 Correct 328 ms 495884 KB Output is correct
39 Correct 246 ms 496284 KB Output is correct
40 Correct 346 ms 495560 KB Output is correct
41 Correct 304 ms 495708 KB Output is correct
42 Correct 331 ms 495884 KB Output is correct
43 Correct 283 ms 495500 KB Output is correct
44 Correct 291 ms 495520 KB Output is correct
45 Correct 228 ms 493516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 489552 KB Output is correct
2 Correct 170 ms 489552 KB Output is correct
3 Correct 176 ms 489492 KB Output is correct
4 Correct 185 ms 489556 KB Output is correct
5 Correct 176 ms 489484 KB Output is correct
6 Correct 167 ms 489556 KB Output is correct
7 Correct 175 ms 489560 KB Output is correct
8 Correct 176 ms 489632 KB Output is correct
9 Correct 186 ms 489556 KB Output is correct
10 Correct 173 ms 489668 KB Output is correct
11 Correct 178 ms 489572 KB Output is correct
12 Correct 188 ms 489560 KB Output is correct
13 Correct 185 ms 489452 KB Output is correct
14 Correct 183 ms 489456 KB Output is correct
15 Correct 179 ms 489676 KB Output is correct
16 Correct 161 ms 489552 KB Output is correct
17 Correct 160 ms 489496 KB Output is correct
18 Correct 156 ms 489552 KB Output is correct
19 Correct 163 ms 489628 KB Output is correct
20 Correct 162 ms 489804 KB Output is correct
21 Correct 154 ms 489556 KB Output is correct
22 Correct 154 ms 489656 KB Output is correct
23 Correct 152 ms 489552 KB Output is correct
24 Correct 161 ms 489560 KB Output is correct
25 Correct 179 ms 489556 KB Output is correct
26 Correct 155 ms 489648 KB Output is correct
27 Correct 159 ms 489592 KB Output is correct
28 Correct 168 ms 489556 KB Output is correct
29 Correct 158 ms 489512 KB Output is correct
30 Correct 160 ms 489832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 489552 KB Output is correct
2 Correct 170 ms 489552 KB Output is correct
3 Correct 176 ms 489492 KB Output is correct
4 Correct 185 ms 489556 KB Output is correct
5 Correct 176 ms 489484 KB Output is correct
6 Correct 167 ms 489556 KB Output is correct
7 Correct 175 ms 489560 KB Output is correct
8 Correct 176 ms 489632 KB Output is correct
9 Correct 186 ms 489556 KB Output is correct
10 Correct 173 ms 489668 KB Output is correct
11 Correct 178 ms 489572 KB Output is correct
12 Correct 188 ms 489560 KB Output is correct
13 Correct 185 ms 489452 KB Output is correct
14 Correct 183 ms 489456 KB Output is correct
15 Correct 179 ms 489676 KB Output is correct
16 Correct 193 ms 489872 KB Output is correct
17 Correct 182 ms 490068 KB Output is correct
18 Correct 183 ms 489808 KB Output is correct
19 Correct 187 ms 489948 KB Output is correct
20 Correct 180 ms 489908 KB Output is correct
21 Correct 178 ms 490068 KB Output is correct
22 Correct 169 ms 489864 KB Output is correct
23 Correct 199 ms 489956 KB Output is correct
24 Correct 202 ms 489860 KB Output is correct
25 Correct 160 ms 489788 KB Output is correct
26 Correct 163 ms 489808 KB Output is correct
27 Correct 190 ms 489880 KB Output is correct
28 Correct 175 ms 489892 KB Output is correct
29 Correct 171 ms 489812 KB Output is correct
30 Correct 176 ms 489772 KB Output is correct
31 Correct 161 ms 489552 KB Output is correct
32 Correct 160 ms 489496 KB Output is correct
33 Correct 156 ms 489552 KB Output is correct
34 Correct 163 ms 489628 KB Output is correct
35 Correct 162 ms 489804 KB Output is correct
36 Correct 154 ms 489556 KB Output is correct
37 Correct 154 ms 489656 KB Output is correct
38 Correct 152 ms 489552 KB Output is correct
39 Correct 161 ms 489560 KB Output is correct
40 Correct 179 ms 489556 KB Output is correct
41 Correct 155 ms 489648 KB Output is correct
42 Correct 159 ms 489592 KB Output is correct
43 Correct 168 ms 489556 KB Output is correct
44 Correct 158 ms 489512 KB Output is correct
45 Correct 160 ms 489832 KB Output is correct
46 Correct 175 ms 490192 KB Output is correct
47 Correct 186 ms 490192 KB Output is correct
48 Correct 176 ms 489928 KB Output is correct
49 Correct 174 ms 490064 KB Output is correct
50 Correct 175 ms 490256 KB Output is correct
51 Correct 170 ms 490064 KB Output is correct
52 Correct 171 ms 490224 KB Output is correct
53 Correct 193 ms 490272 KB Output is correct
54 Correct 169 ms 490188 KB Output is correct
55 Correct 174 ms 490064 KB Output is correct
56 Correct 201 ms 490188 KB Output is correct
57 Correct 206 ms 490192 KB Output is correct
58 Correct 184 ms 490024 KB Output is correct
59 Correct 180 ms 490228 KB Output is correct
60 Correct 173 ms 489964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 489552 KB Output is correct
2 Correct 170 ms 489552 KB Output is correct
3 Correct 176 ms 489492 KB Output is correct
4 Correct 185 ms 489556 KB Output is correct
5 Correct 176 ms 489484 KB Output is correct
6 Correct 167 ms 489556 KB Output is correct
7 Correct 175 ms 489560 KB Output is correct
8 Correct 176 ms 489632 KB Output is correct
9 Correct 186 ms 489556 KB Output is correct
10 Correct 173 ms 489668 KB Output is correct
11 Correct 178 ms 489572 KB Output is correct
12 Correct 188 ms 489560 KB Output is correct
13 Correct 185 ms 489452 KB Output is correct
14 Correct 183 ms 489456 KB Output is correct
15 Correct 179 ms 489676 KB Output is correct
16 Correct 193 ms 489872 KB Output is correct
17 Correct 182 ms 490068 KB Output is correct
18 Correct 183 ms 489808 KB Output is correct
19 Correct 187 ms 489948 KB Output is correct
20 Correct 180 ms 489908 KB Output is correct
21 Correct 178 ms 490068 KB Output is correct
22 Correct 169 ms 489864 KB Output is correct
23 Correct 199 ms 489956 KB Output is correct
24 Correct 202 ms 489860 KB Output is correct
25 Correct 160 ms 489788 KB Output is correct
26 Correct 163 ms 489808 KB Output is correct
27 Correct 190 ms 489880 KB Output is correct
28 Correct 175 ms 489892 KB Output is correct
29 Correct 171 ms 489812 KB Output is correct
30 Correct 176 ms 489772 KB Output is correct
31 Correct 291 ms 495992 KB Output is correct
32 Correct 294 ms 495876 KB Output is correct
33 Correct 221 ms 493500 KB Output is correct
34 Correct 280 ms 495568 KB Output is correct
35 Correct 335 ms 496136 KB Output is correct
36 Correct 229 ms 495892 KB Output is correct
37 Correct 199 ms 493520 KB Output is correct
38 Correct 328 ms 495884 KB Output is correct
39 Correct 246 ms 496284 KB Output is correct
40 Correct 346 ms 495560 KB Output is correct
41 Correct 304 ms 495708 KB Output is correct
42 Correct 331 ms 495884 KB Output is correct
43 Correct 283 ms 495500 KB Output is correct
44 Correct 291 ms 495520 KB Output is correct
45 Correct 228 ms 493516 KB Output is correct
46 Correct 161 ms 489552 KB Output is correct
47 Correct 160 ms 489496 KB Output is correct
48 Correct 156 ms 489552 KB Output is correct
49 Correct 163 ms 489628 KB Output is correct
50 Correct 162 ms 489804 KB Output is correct
51 Correct 154 ms 489556 KB Output is correct
52 Correct 154 ms 489656 KB Output is correct
53 Correct 152 ms 489552 KB Output is correct
54 Correct 161 ms 489560 KB Output is correct
55 Correct 179 ms 489556 KB Output is correct
56 Correct 155 ms 489648 KB Output is correct
57 Correct 159 ms 489592 KB Output is correct
58 Correct 168 ms 489556 KB Output is correct
59 Correct 158 ms 489512 KB Output is correct
60 Correct 160 ms 489832 KB Output is correct
61 Correct 175 ms 490192 KB Output is correct
62 Correct 186 ms 490192 KB Output is correct
63 Correct 176 ms 489928 KB Output is correct
64 Correct 174 ms 490064 KB Output is correct
65 Correct 175 ms 490256 KB Output is correct
66 Correct 170 ms 490064 KB Output is correct
67 Correct 171 ms 490224 KB Output is correct
68 Correct 193 ms 490272 KB Output is correct
69 Correct 169 ms 490188 KB Output is correct
70 Correct 174 ms 490064 KB Output is correct
71 Correct 201 ms 490188 KB Output is correct
72 Correct 206 ms 490192 KB Output is correct
73 Correct 184 ms 490024 KB Output is correct
74 Correct 180 ms 490228 KB Output is correct
75 Correct 173 ms 489964 KB Output is correct
76 Correct 833 ms 503760 KB Output is correct
77 Correct 857 ms 503296 KB Output is correct
78 Correct 272 ms 494964 KB Output is correct
79 Correct 628 ms 498648 KB Output is correct
80 Correct 1121 ms 503808 KB Output is correct
81 Correct 257 ms 495828 KB Output is correct
82 Correct 552 ms 499032 KB Output is correct
83 Correct 1172 ms 503548 KB Output is correct
84 Correct 777 ms 504060 KB Output is correct
85 Correct 397 ms 498636 KB Output is correct
86 Correct 1358 ms 503708 KB Output is correct
87 Correct 1281 ms 503580 KB Output is correct
88 Correct 492 ms 496256 KB Output is correct
89 Correct 1078 ms 499140 KB Output is correct
90 Correct 408 ms 496072 KB Output is correct