Submission #1324797

#TimeUsernameProblemLanguageResultExecution timeMemory
1324797WH8Sličnost (COI23_slicnost)C++17
100 / 100
579 ms38132 KiB
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define int long long
#define pb push_back
#define f first
#define s second
#define ld long double
#define sz(x) static_cast<int>((x).size())
#define i5 tuple<int,int,int,int,int>
#define all(x) x.begin(), x.end()
#define iii tuple<int,int,int>
#define eb emplace_back
#define mt make_tuple
#define pll pair<int,int>

struct node {
	int s, e, m, lz;
	pll v;
	node *l, *r;
	node (int S, int E){
		s=S, e=E, m=(s+e)/2, v={0, e-s+1}, lz=0;
		if(s!=e){
			l=new node(s, m);
			r=new node(m+1, e);
		}
	}
	void prop(){
		if(s==e or lz==0)return;
		l->v.f+=lz, r->v.f+=lz, l->lz+=lz, r->lz+=lz;
		lz=0;
	}
	pll combine(pll a, pll b){
		if(a.f==b.f)return mp(a.f, b.s + a.s);
		return max(a, b);
	}
	void upd(int S, int E, int V){ // range add. 
		if((S==s and E==e) or s==e){
			v.f+=V;
			lz+=V;
			return;
		}
		prop();
		if (E<=m) l->upd(S,E,V);
		else if (S>m) r->upd(S,E,V);
		else l->upd(S,m,V),r->upd(m+1,E,V);
		v=combine(l->v,r->v);
	}
	pll qry(int S,int E){
		if((S==s and E==e) or s==e){
			return v;
		}
		prop();
		if(E<=m)return l->qry(S,E);
		if(S>m)return r->qry(S,E);
		return combine(l->qry(S,m),r->qry(m+1,E));
	}
} *root;


signed main(){
	int n,k,q;cin>>n>>k>>q;
	vector<vector<pair<int,pll>>> time(q+1), ev(n+1);
	vector<int> conv(n+1, 0), a(n+1, 0);
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++){
		int c;cin>>c;
		conv[c]=i;
	}
	for(int i=1;i<=n;i++)a[i]=conv[a[i]];
	auto cpa = a;
	for(int qt=0;qt<q;qt++){
		int x;cin>>x;
		if(x >= k) ev[x].pb({qt, {cpa[x], cpa[x+1]}});
		if(x + k <= n) ev[x+k].pb({qt, {cpa[x+1], cpa[x]}});
		swap(cpa[x], cpa[x+1]);
	}
	/*for(int i=1;i<=n;i++){
		printf("window %lld, events are : ", i);
		for(auto [t, pp] : ev[i]){
			auto [rem, add] = pp;
			printf(" (t=%lld, rem %lld, add %lld) ", t, rem, add);
		}
		cout<<endl;
	}*/
	root = new node(1, n);
	for(int i=1;i<=k;i++){
		root->upd(a[i], min(a[i]+k-1, n), 1);
	}
	vector<pll> res(n+1, {-1, -1});
	for(int i=k;i<=n;i++){
		res[i]=root->qry(k, n);
		// calculate the new max pairs
		for (auto [t, pp] : ev[i]){
			auto [rem, add] = pp;
			root->upd(rem, min(rem+k-1, n), -1);
			root->upd(add, min(add+k-1, n), 1);
			time[t].pb({i, root->qry(k, n)});
		}
		// rollback
		for (auto [t, pp] : ev[i]){
			auto [rem, add] = pp;
			root->upd(rem, min(rem+k-1, n), 1);
			root->upd(add, min(add+k-1, n), -1);
		}
		// move to next window in original array
		root->upd(a[i-k+1], min(a[i-k+1]+k-1, n), -1);
		if(i+1 <= n)root->upd(a[i+1], min(a[i+1]+k-1, n), 1);
	}
	map<int,int> m;
	for(auto [mx, cnt] : res){
		m[mx]+=cnt;
	}
	while(!m.empty() and (*prev(m.end())).s == 0)m.erase(prev(m.end()));
	cout<<(*prev(m.end())).f<<" "<<(*prev(m.end())).s<<'\n';
	for(int qt=0;qt<q;qt++){
		//assert(sz(time[qt]) >= 1);
		for (auto [x, nw] : time[qt]){
			m[res[x].f] -= res[x].s;
			m[nw.f] += nw.s;
			res[x]=nw;
		}
		while(!m.empty() and (*prev(m.end())).s == 0)m.erase(prev(m.end()));
		cout<<(*prev(m.end())).f<<" "<<(*prev(m.end())).s<<'\n';
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...