Submission #1351278

#TimeUsernameProblemLanguageResultExecution timeMemory
1351278hminhatCollecting Stamps 4 (JOI25_stamps4)C++20
100 / 100
975 ms125768 KiB
/*	
	ROAD TO TST 
*/
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define fi first
#define se second
#define pii pair<int, int>
#define el "\n"
#define pll pair<ll, ll>
#define pb push_back
#define all(v) v.begin(), v.end()

#define rep(i, x, y) for(int i = x, _y = y; i <= _y; i++)
#define rev(i, x, y) for(int i = x, _y = y; i >= _y; i--)

void file() {
	#define name "test"
	if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
	else {
		#undef name
		#define name "C:\\Users\\hminh\\Desktop\\2026\\AIO\\test"
		if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
	}
}

const int nmax = 1e6 + 7;

int n, x, q, a[nmax], last[nmax >> 1];
ll C[nmax];
map<ll, ll> mp;

struct fenwick {
	int fw[nmax << 1];

	void update(int u, int val) {
		for(;u <= (n << 1); u += u & (-u)) fw[u] += val;
	}

	int get(int u) {
		int res = 0;
		for(;u >= 1; u -= u & (-u)) res += fw[u];
		return res;
	}

	int get(int l, int r) {
		return get(r) - get(l - 1);
	}
} fw;

int main()
{
	file();
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin >> n >> x;
	n <<= 1;
	rep(i, 1, n) cin >> a[i];
	rep(i, 1, n) cin >> C[i];
	ll cnt = 0;
	rep(i, 1, n) {
		if(last[a[i]]) fw.update(i, 1);
		else cnt += fw.get(1, i);
		last[a[i]] = i;
	}
	mp[cnt] = C[1];
	rep(i, 2, n) {
		int p = last[a[i - 1]];
		cnt -= (n + i - p - 1) - fw.get(p, n + i - 2);
		fw.update(p, -1);
		cnt += fw.get(1, p);
		last[a[i - 1]] = n + i - 1;
		fw.update(n + i - 1, 1);
		if(mp.count(cnt) == 0) mp[cnt] = C[i];
		else mp[cnt] = min(mp[cnt], C[i]);
	}
	vector<pll> V1, V2;
	V1.pb({-1, 2e18});
	V2.pb({-1, 2e18});
	for(pll p : mp) V1.pb(p), V2.pb({p.fi, p.se + p.fi * x});
	V2.pb({2e18, 2e18});
	V1.pb({2e18, 2e18});
	rep(i, 1, (int) V1.size() - 1) V1[i].se = min(V1[i].se, V1[i - 1].se);
	rev(i, (int) V2.size() - 2, 0) V2[i].se = min(V2[i].se, V2[i + 1].se);
	cin >> q;
	while(q -- ) {
		ll k;
		cin >> k;
		k = 1LL * n * n / 4 - k;
		int pos = upper_bound(all(V1), make_pair(k + 1, 0LL)) - V1.begin();
		cout << min(V1[pos - 1].se, V2[pos].se - k * x) << el;
	}
	return 0;
}

Compilation message (stderr)

Main.cpp: In function 'void file()':
Main.cpp:22:44: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
      |                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:22:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
      |                                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:26:52: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |                 if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
      |                                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:26:84: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |                 if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin);freopen(name".out", "w", stdout);}
      |                                                                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...