QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#94881#5669. Traveling in Jade CitySEM_PRESSAO_pedroteosousa#TL 2ms3348kbC++233.1kb2023-04-08 03:39:222023-04-08 03:39:24

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-08 03:39:24]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:3348kb
  • [2023-04-08 03:39:22]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define int ll
#define all(v) v.begin(),v.end()
#define pb push_back

// #define endl '\n'

void dbg_out() {cerr << endl; }
template< typename H, typename... T> 
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) //{ cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }

struct Bit {
	int total_sum;
	vector<int> bit;

	Bit(int n): total_sum(0), bit(n) {}

	int query(int id) {
		int res = 0;
		for(id++;id>0;id-=id&-id) res += bit[id-1];
		return res;
	}

	void update(int id, int val) {
		total_sum += val;
		for(id++;id<=int(bit.size());id+=id&-id) bit[id-1] += val;
	}

	int query_range(int l, int r) {
		return query(r) - ((l == 0) ? 0 : query(l-1));
	}
};

void solve() {
	int n, k, m, q; cin >> n >> k >> m >> q;
	--k;
	Bit cyc(n), path(m+1);
	vector<int> c(n), p(m+1);
	vector<bool> okc(n, true), okp(m+1, true);
	for(int i=0;i<n;i++) {
		cin >> c[i];
		cyc.update(i, c[i]);
	}
	for(int i=0;i<=m;i++) {
		cin >> p[i];
		path.update(i, p[i]);
	}

	dbg(cyc.total_sum, path.total_sum);
	dbg(cyc.query(0));
	dbg(cyc.query(1));
	dbg(cyc.query_range(0, 1));

	const int INF = 1e10;

	auto cyc_dist = [&](int u, int v)  -> int {
		assert(u < n && v < n);
		if(u > v) swap(u, v);
		if(u == v) return 0;
		int ans = cyc.query_range(u, v-1);
		return min<int>(ans, cyc.total_sum - ans);
	};

	// transform in path coordinates
	auto transform = [&](int x) -> int {
		if(x == 0) return 0;
		else if(x == k) return m + 1;
		else {
			assert(x >= n);
			return x - n + 1;
		}
	};

	auto path_dist = [&](int u, int v) -> int {
		u = transform(u);
		v = transform(v);
		if(u > v) swap(u, v);
		if(u == v) return 0;
		return path.query_range(u, v-1);
	};

	auto out_path_dist = [&](int u, int v) {
		return path.total_sum - path.query_range(u, v);
	};

	function<int(int,int)> query = [&](int u, int v) {
		assert(u <= v);
		int ans = INF;
		if(u < n && v < n) { 
			// both outside
			ans = min(ans, cyc_dist(u, v));
			ans = min(ans, cyc_dist(u, 0) + path.total_sum + cyc_dist(k, v));
			ans = min(ans, cyc_dist(u, k) + path.total_sum + cyc_dist(0, v));
		} else if(u < n && v >= n) {
			// v is inside
			ans = min(ans, cyc_dist(u, 0) + path_dist(v, 0));
			ans = min(ans, cyc_dist(u, k) + path_dist(v, k));
		} else {
			assert(u >= n && v >= n);
			ans = min(ans, path_dist(u, v));
			ans = min(ans, cyc_dist(0, k) + out_path_dist(u, v));
		}
		return ans;
	};

	while(q--) {
		char cc; cin >> cc;
		if(cc == 'q') {
			int u, v; cin >> u >> v;
			--u; --v;
			if(u > v) swap(u,v);
			int ans = query(u, v);
			if(ans >= INF) {
				cout << "impossible" << endl;
			} else {
				cout << ans << endl;
			}
		} else if(cc == 'c') { // cycle
			int i; cin >> i;
			--i;
			if(okc[i]) {
				cyc.update(i, INF - c[i]);
			} else {
				cyc.update(i, c[i] - INF);
			}
			okc[i] = !okc[i];
		} else if(cc == 'x') {
			int i; cin >> i;
			if(okp[i]) {
				path.update(i, INF - p[i]);
			} else {
				path.update(i, p[i] - INF);
			}
			okp[i] = !okp[i];
		} else {
			assert(false);
		}
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3348kb

input:

4 3 1 9
2 3 8 4
1 1
q 3 4
x 0
q 3 4
c 3
q 3 4
c 1
q 3 4
x 0
q 3 4

output:

6
8
9
impossible
6

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 3328kb

input:

4 3 1 9
2 3 8 4
1 1
q 3 4
x 0
q 3 4
c 3
q 3 4
c 1
q 3 4
x 0
q 3 4

output:

6
8
9
impossible
6

result:

ok 5 lines

Test #3:

score: -100
Time Limit Exceeded

input:

1000000 999999 1000000 1000000
200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 2...

output:

177406400
122264600
70328400
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
impossible
29295200
impossible
22163200
impossible
impossible
impossible
11422200
impossible
62579800
impossible
164661200
impossible
-9802339000
impossible
impossible
impossible
impo...

result: