QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#404198#6317. XOR Tree Pathcomeintocalm#WA 3ms3872kbC++202.5kb2024-05-03 15:49:592024-05-03 15:50:01

Judging History

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

  • [2024-05-03 15:50:01]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3872kb
  • [2024-05-03 15:49:59]
  • 提交

answer

#include <bits/stdc++.h>
#define LL long long
#define db double
using namespace std;

const int MAXN = 1e5 + 5, N = 9999;
int n, m, K, Q;
int c[MAXN];
int lka[MAXN], lkb[MAXN];

int Rt, tot, ID;
int son[MAXN * 30][2];
vector<int> vec[MAXN * 30];
void ins (int &x, int S, int dep = 29) {
	if (!x) x = ++tot;
	if (dep == 0) { vec[x].push_back (ID); return ; }
	ins (son[x][(S >> dep) & 1], S, dep - 1);
}

struct Qry {
	int d, id;
} q[MAXN];

const int cmp (const Qry t1, const Qry t2) {
	return t1.d < t2.d;
}

int top;
LL *stp[MAXN * 30], sta[MAXN * 30];
LL sz[MAXN], fa[MAXN];
LL Ans = 0;

LL calc (int x) {
	return (LL) sz[x] * (sz[x] - 1) / 2;	
}

int get (int x) { return x == fa[x] ? x : get (fa[x]); }

int chg (LL &u, LL val) {
	stp[++top] = &u, sta[top] = val;
	u = val;
}

void merge (int x, int y) {
	//cout << 'a';
	int X = x, Y = y;
	cout << x << " " << y << endl;
	x = get (x), y = get (y);
	if (x == y) return ;
	else {
		//cout << X << " " << Y << "  ";
		chg (Ans, Ans - calc (x) - calc (y));
		
		if (sz[x] < sz[y]) swap (x, y);
		chg (fa[y], x), chg (sz[x], sz[x] + sz[y]);
		chg (Ans, Ans + calc (x));
		//cout << Ans << endl;
	}
}

void bck() {
	*stp[top] = sta[top], --top;
}

void psh (int x) {
	//cout << 'a';
	if (!x) return ;
	for (int i = 0; i < vec[x].size(); ++i)
		merge (lka[vec[x][i]], lkb[vec[x][i]]);
	if (son[x][0]) psh (son[x][0]);
	if (son[x][1]) psh (son[x][1]);
}

int ans[MAXN];
void dfs (int x, int l, int r, int dep = 29) {
	//cout << 'a';
	if (l > r) return ;
	if (!x || dep == 0) {
		//cout << 'a';
		for (int i = l; i <= r; ++i)
			ans[q[i].id] = Ans;
		return ;
	}
	int pos = l - 1;
	if (pos < r && ((q[pos + 1].d >> dep) & 1) == 0)
		++pos;
	int rec = top;
	if ((K >> dep) & 1) {
		//cout << 'a';
		psh (son[x][1]), dfs (son[x][0], l, pos, dep - 1);
		while (top != rec) bck();
		psh (son[x][0]), dfs (son[x][1], pos + 1, r, dep - 1);
		while (top != rec) bck(); 
	}
	else {
		dfs (son[x][0], l, pos, dep - 1);
		dfs (son[x][1], pos + 1, r, dep - 1);
	}
}

int main() {
	int i,j,k;
	int x, y, z;
	scanf ("%d%d%d", &n, &m, &K);
	for (i = 1; i <= m; ++i) {
		scanf ("%d%d%d", &lka[i], &lkb[i], &c[i]), c[i] ^= K;
		ID = i, ins (Rt, c[i]);
	}
	scanf ("%d", &Q);
	for (i = 1; i <= Q; ++i)
		scanf ("%d", &q[i].d), q[i].id = i;
	sort (q + 1, q + Q + 1, cmp);
	for (i = 1; i <= n; ++i) fa[i] = i, sz[i] = 1;
	dfs (Rt, 1, Q);
	for (i = 1; i <= Q; ++i)
		printf ("%d\n", ans[i]);
  return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 3872kb

input:

5
1 0 0 1 0
1 2
1 3
3 4
3 5

output:

0

result:

wrong answer 1st numbers differ - expected: '5', found: '0'