跳到正文

剪贴板

剪贴板 ob64dgxr

原文
剪贴板信息

剪贴板信息

收录
更新

一

C++
#include <stdio.h>
#include <string.h>

typedef long long LL;
constexpr int N = 3005, M = 1 << 20;

int a[N], n, c[M];
LL res;

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) scanf("%d", a + i);
	for (int i = 0; i < n; ++i) {
		int s = 0;
		for (int j = i; j < n; ++j) res += c[s ^= a[j]];
		s = 0;
		for (int j = i; ~j; --j) ++c[s ^= a[j]];
	}
	printf("%lld\n", res);
	return 0;
}

二

C++
#include <stdio.h>
#include <stdlib.h>

#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>

#define fi first
#define se second
#define pb emplace_back

using namespace std;

typedef pair<int, int> PII;

int main() {
	int n;
	scanf("%d", &n);
	vector<int> a(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
	}
	vector<PII> res;
	function<void(int, int)> Add = [&](int x, int y) {
		res.pb(x, y);
		a[x] += a[y];
	};
	int kcnt = 0;
	while (true) {
		int ocnt = 0;
		for (int i = 0; i < n; ++i) {
			ocnt += a[i] & 1;
		}

		bool equal = true;
		for (int i = 1; i < n; ++i) {
			if (a[i] != a[0]) {
				equal = false;
				break;
			}
		}
		if (equal) {
			break;
		}

		vector<PII> ars;
		for (int i = 0; i < n; ++i) {
			if (a[i] & 1) {
				ars.pb(a[i], i);
			}
		}
		sort(ars.begin(), ars.end());
		for (int i = ars.size() - 1; ~i; --i) {
			int x = ars[i].fi;
			int lim = 1;
			while (lim < x) {
				lim <<= 1;
			}
			int l = 0, r = i, mid;
			while (l < r) {
				mid = l + r >> 1;
				if (ars[mid].fi >= lim - x) r = mid;
				else l = mid + 1;
			}
			if (ars[r].fi == lim - x) {
				Add(ars[i].se, ars[r].se);
			}
		}

		int t = -1;
		for (int i = 0; i < n; ++i) {
			if ((a[i] & 1) && (t == -1 || a[i] < a[t])) {
				t = i;
			}
		}
		if (t != -1) {
			for (int i = 0; i < n; ++i) {
				if (i != t && (a[i] & 1)) {
					Add(i, t);
				}
			}
			Add(t, t);
		}
		for (int i = 0; i < n; ++i) {
			a[i] >>= 1;
		}
	}

	printf("%d\n", (int)res.size());
	for (int i = 0; i < res.size(); ++i) {
		printf("%d %d\n", res[i].fi + 1, res[i].se + 1);
	}

	return 0;
}

三

C++
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define LOG(FMT...) fprintf(stderr, FMT)

typedef long long LL;
const int N = 500005, M = 6000005;

LL min(LL x, LL y) {
	return x < y ? x : y;
}
LL max(LL x, LL y) {
	return x > y ? x : y;
}
bool cmin(LL &x, LL y) {
	return x > y ? x = y, true : false;
}
bool cmax(LL &x, LL y) {
	return x < y ? x = y, true : false;
}

int n, m, a[N];
LL L, R;
int k[N], sk;
LL c[N], sc;
int h[N], e[M], ne[M], w[M], idx;
bool NIE;

void add(int x, int y, int c) {
	e[++idx] = y, w[idx] = c, ne[idx] = h[x], h[x] = idx;
}

void dfs(int x) {
	// 0 <= k[x] t + c[x] <= a[x]
	// k[x] == 1: -c[x] <= t <= a[x] - c[x]
	// k[x] == -1: c[x] - a[x] <= t <= c[x]
	sk += k[x], sc += c[x];
	if (k[x] == 1) cmax(L, -c[x]), cmin(R, a[x] - c[x]);
	if (k[x] == -1) cmax(L, c[x] - a[x]), cmin(R, c[x]);
	if (L > R) puts("NIE"), exit(0);
	for (int i = h[x]; i; i = ne[i]) {
		int y = e[i];
		if (!k[y]) {
			k[y] = -k[x], c[y] = w[i] - c[x];
			// k[y] t + c[y] = w[i] - k[x] t - c[x]
			// k[y] = -k[x], c[y] = w[i] - c[x]
			dfs(y);
		} else if (k[x] != k[y]) {
			if (c[x] + c[y] != w[i]) puts("NIE"), exit(0);
		} else {
			// 2 k[x] t == w[i] - c[x] - c[y]
			if (w[i] - c[x] - c[y] & 1) puts("NIE"), exit(0);
			LL t = (w[i] - c[x] - c[y]) / 2 * k[x];
			if (t < L || t > R) puts("NIE"), exit(0);
			L = R = (w[i] - c[x] - c[y]) / 2 * k[x];
		}
	}
}

int main() {
	scanf("%d%d", &n, &m);
	LOG("%d %d\n", n, m);
	LL sumA = 0, sumL = 0, sumR = 0;
	for (int x = 1; x <= n; ++x) {
		scanf("%d", a + x);
		sumA += a[x];
	}
	for (int i = 0; i < m; ++i) {
		int x, y, c;
		scanf("%d%d%d", &x, &y, &c);
		add(x, y, c), add(y, x, c);
	}
	for (int x = 1; x <= n; ++x) {
		if (!k[x]) {
			L = 0, R = a[x];
			k[x] = 1, c[x] = 0;
			sk = 0, sc = 0;
			dfs(x);
			// sk * t + sc
			if (sk < 0) sumL += sk * R + sc, sumR += sk * L + sc;
			else if (sk == 0) sumL += sc, sumR += sc;
			else sumL += sk * L + sc, sumR += sk * R + sc;
		}
	}
	if (NIE) return puts("NIE"), 0;
	printf("%lld %lld\n", sumA - sumR, sumA - sumL);
	return 0;
}

四

C++
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <algorithm>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long LL;
constexpr int N = 100005, TC = N << 2;
constexpr LL INF = 1ll << 61, NINF = -INF;

int n;
LL a[N], b[N], res;

void cmax(LL &x, LL y) {
	if (x < y) {
		x = y;
	}
}

#define L (z << 1)
#define R (z << 1 | 1)

LL v[TC];
int w[TC], t[TC];

void upd(int z) {
	v[z] = max(v[L], v[R]);
}

void down(int z) {
	if (t[z]) {
		v[L] += t[z], v[R] += t[z], t[L] += t[z], t[R] += t[z], t[z] = 0;
	}
}

void build(int l = 1, int r = n, int z = 1) {
	v[z] = NINF, w[z] = t[z] = 0;
	if (l != r) {
		int mid = l + r >> 1;
		build(l, mid, L), build(mid + 1, r, R);
	}
}

void change1(int x, int l = 1, int r = n, int z = 1) {
	if (r <= x) {
		v[z] += 2, t[z] += 2;
	} else {
		int mid = l + r >> 1;
		down(z);
		change1(x, l, mid, L);
		if (mid < x) {
			change1(x, mid + 1, r, R);
		}
		upd(z);
	}
}

void change2(int x, LL c, int l = 1, int r = n, int z = 1) {
	if (l == r) {
		v[z] = c, w[z] = 1;
	} else {
		int mid = l + r >> 1;
		down(z);
		if (x <= mid) {
			change2(x, c, l, mid, L);
		} else {
			change2(x, c, mid + 1, r, R);
		}
		upd(z), w[z] = w[L] + w[R];
	}
}

LL query1(int x, int l = 1, int r = n, int z = 1) {
	if (r <= x) {
		return v[z];
	}
	int mid = l + r >> 1;
	down(z);
	LL ret = query1(x, l, mid, L);
	if (mid < x) {
		cmax(ret, query1(x, mid + 1, r, R));
	}
	return ret;
}

int query2(int x, int l = 1, int r = n, int z = 1) {
	if (l >= x) {
		return w[z];
	}
	int mid = l + r >> 1;
	int ret = query2(x, mid + 1, r, R);
	if (x <= mid) {
		ret += query2(x, l, mid, L);
	}
	return ret;
}

int main() {
	LL maxn = 0;
	int task;
	for (scanf("%d", &task); task--; ) {
		scanf("%d", &n);
		cmax(maxn, n);
		for (int i = 1; i <= n; ++i) {
			scanf("%lld", a + i);
			a[i] -= i;
		}
		memcpy(b, a + 1, n << 3);
		sort(b, b + n);
		bool flag = false;
		for (int i = 1; i < n; ++i) {
			if (b[i] == b[i - 1]) {
				flag = true;
				break;
			}
		}
		if (flag) {
			puts("-1");
			continue;
		}
		res = 0;
		build();
		for (int i = 1; i <= n; ++i) {
			int x = lower_bound(b, b + n, a[i]) - b + 1;
			if (x > 1) {
				cmax(res, query1(x - 1) + a[i]);
				change1(x - 1);
			}
			change2(x, a[i] + 2 * query2(x) + 3);
		}
		printf("%lld\n", res);
	}
	LOG("%lld\n", maxn);
	return 0;
}