QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#643049#8237. Sugar Sweet IIwsxcb#WA 189ms30656kbC++172.6kb2024-10-15 18:18:192024-10-15 18:18:20

Judging History

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

  • [2024-11-04 16:59:03]
  • hack成功,自动添加数据
  • (/hack/1109)
  • [2024-10-15 18:18:20]
  • 评测
  • 测评结果:WA
  • 用时:189ms
  • 内存:30656kb
  • [2024-10-15 18:18:19]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define int long long
const int N = 5e5 + 5, mod = 1e9 + 7;
int a[N], b[N], w[N], vis[N], d[N], inv2;
ll dp[N][2], A[N];
vector<int>e[N];

ll ksm(ll a, ll b) {
	ll ans = 1;
	while (b) {
		if (b & 1)
			ans = ans * a % mod;
		a = a * a % mod;
		b /= 2;
	}
	return ans % mod;
}

ll inv(ll a) {
	return ksm(a, mod - 2);
}

void add(int u, int v) {
	e[v].pb(u);
	d[u]++;
}

ll get(ll n, ll m) {
	return A[n] * inv(A[m]) % mod;
}

void solve() {
	int n;
	cin >> n;
	vector<ll>res(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		vector<int>().swap(e[i]);
		d[i] = 0;
		vis[i] = 0;
	}
	for (int i = 1; i <= n; i++)
		cin >> b[i];
	for (int i = 1; i <= n; i++)
		cin >> w[i];
	stack<int>st;
	for (int i = 1; i <= n; i++) {
		if (vis[i])
			continue;
		int x = i;
		set<int>stk;
		while (!vis[x]) {
			st.push(x);
			stk.insert(x);
			vis[x] = 1;
			x = b[x];
		}
		if (stk.find(x) != stk.end()) {
			vector<int>t;
			while (st.top() != x) {
				t.pb(st.top());
				st.pop();
			}
			t.pb(st.top());
			st.pop();
			set<int>s;
			int ok = 0;
			for (auto x : t) {
				s.insert(a[x]);
				if (s.size() > 1) {
					ok = 1;
					break;
				}
			}
			if (ok) {
				for (auto x : t) {
					int z = b[x];
					if (a[x] >= a[z])
						add(x, z);
				}
			} else {
				for (auto x : t)
					res[x] = a[x];
			}
		}
		while (st.size()) {
			int x = st.top();
			st.pop();
			add(x, b[x]);
		}
	}
//	for (int i = 1; i <= n; i++) {
//		cout << i << ' ';
//		for (auto x : e[i])
//			cout << x << ' ';
//		cout << '\n';
//	}
	//cout << res[2] << "  44444\n";
	queue<pair<int, int>>q;
	for (int i = 1; i <= n; i++) {
		if (d[i] == 0) {
			q.push({i, 1});
			if (!res[i]) {
				dp[i][0] = 0;
				dp[i][1] = 1;
			} else {
				dp[i][1] = 0;
				dp[i][0] = 1;
			}
		}
	}
	//return ;
	while (q.size()) {
		int u = q.front().first, c = q.front().second;
		q.pop();
		for (auto v : e[u]) {
			d[v]--;
			if (a[v] < a[u]) {
				dp[v][1] = 1;
				dp[v][0] = 0;
				q.push({v, 1});
			} else if (a[v] < a[u] + w[u]) {
				c++;
				dp[v][1] = get(n, c) * inv(A[n]) % mod;
				q.push({v, c});
			} else {
				dp[v][1] = 0;
				dp[v][0] = 1;
				q.push({v, 1});
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << (a[i] + dp[i][1]*w[i]) % mod << ' ';
	}
	cout << '\n';
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	inv2 = ksm(2, mod - 2);
	A[0] = 1;
	for (int i = 1; i < N; i++) {
		A[i] = A[i - 1] * i % mod;
	}
	int t = 1;
	cin >> t;
	while (t--)
		solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 30632kb

input:

4
4
2 5 5 2
4 2 1 3
3 2 1 4
3
5 4 3
1 1 1
6 6 6
3
5 4 3
2 3 1
1 2 3
5
2 1 3 2 1
5 1 1 3 4
1 3 4 2 4

output:

500000007 5 5 6 
5 10 9 
166666673 5 6 
500000006 4 3 4 5 

result:

ok 15 numbers

Test #2:

score: -100
Wrong Answer
time: 189ms
memory: 30656kb

input:

50000
5
508432375 168140163 892620793 578579275 251380640
3 4 4 1 3
346232959 736203130 186940774 655629320 607743104
1
863886789
1
364158084
18
864679185 463975750 558804051 604216585 694033700 499417132 375390750 337590759 467353355 111206671 983760005 984444619 322277587 138763925 205122047 97736...

output:

854665334 904343293 590444253 906393935 859123744 
863886789 
871186919 814243920 968784984 206455474 17527050 449261413 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 113628626 977360756 482247153 963066959 
902284038 132058958 132646723 421298438 601054667 99438820...

result:

wrong answer 25th numbers differ - expected: '665922935', found: '902284038'