QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#328716#8237. Sugar Sweet IIyan_silva#WA 140ms3680kbC++202.7kb2024-02-16 02:09:122024-02-16 02:09:13

Judging History

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

  • [2024-11-04 16:59:03]
  • hack成功,自动添加数据
  • (/hack/1109)
  • [2024-02-16 02:09:13]
  • 评测
  • 测评结果:WA
  • 用时:140ms
  • 内存:3680kb
  • [2024-02-16 02:09:12]
  • 提交

answer

#include <bits/stdc++.h>

#define fastio ios_base::sync_with_stdio(0); cin.tie(0)

using namespace std;

using ll = long long;

#define int ll
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

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__); }

template <const int MOD>
struct Mint {
	int x;
	Mint(): x(0) {}
	Mint(int _x): x(_x%MOD<0?_x%MOD+MOD:_x%MOD) {}
	void operator+=(Mint rhs) { x+=rhs.x; if(x>=MOD) x-=MOD; }
	void operator-=(Mint rhs) { x-=rhs.x; if(x<0)x+=MOD; }
	void operator*=(Mint rhs) { x*=rhs.x; x%=MOD; }
	void operator/=(Mint rhs) { *this *= rhs.inv(); }
	Mint operator+(Mint rhs) { Mint res=*this; res+=rhs; return res; }
	Mint operator-(Mint rhs) { Mint res=*this; res-=rhs; return res; }
	Mint operator*(Mint rhs) { Mint res=*this; res*=rhs; return res; }
	Mint operator/(Mint rhs) { Mint res=*this; res/=rhs; return res; }
	Mint inv() { return this->pow(MOD-2); }
	Mint pow(int e) {
		Mint res(1);
		for(Mint p=*this;e>0;e/=2,p*=p) if(e%2)
			res*=p;
		return res;
	}
	friend ostream& operator<<(ostream& os, Mint x) {
		return (os << x.x);
	}
};

const int MOD = 1e9+7;
const int MAXN = 5e5+10;

using mint = Mint<MOD>;

void solve() {
	int n; cin>>n;
	vector<int> a(n), b(n), w(n);
	for (int i = 0; i < n; i++) cin>>a[i];
	for (int i = 0; i < n; i++) cin>>b[i];
	for (int i = 0; i < n; i++) cin>>w[i];

	vector<int> in_deg(n);
	for (int i = 0; i < n; i++) in_deg[--b[i]]++;

	queue<int> q;
	for (int i = 0; i < n; i++) {
		if (!in_deg[i]) q.push(i);
	}

	vector<mint> dp(n);
	for (int i = 0; i < n; i++) {
		if (a[i] < a[b[i]]) dp[i] = 1;
	}

	vector<int> topo, sz(n, 1);
	while (q.size()) {
		int u = q.front(); q.pop();
		topo.push_back(u);
		in_deg[b[u]]--;
		if (!in_deg[b[u]]) q.push(b[u]);
	}

	for (int i = 0; i < n; i++) if (in_deg[i]) {
		vector<int> cyc(1, i);
		for (int j = b[i], it = 1; j != i; j = b[j], it++) {
			cyc.push_back(j);
		}
		reverse(all(cyc));

		int st = -1, m = cyc.size();
		for (int i = 0; i < m; i++) {
			if (dp[cyc[i]].x == 1) st = i;
		}
		if (st == -1) continue;

		for (int j = 0; j < m; j++) {
			int cur = cyc[(st+j)%m],
				prv = cyc[(st+j+1)%m];

			in_deg[cur]--;
			if (a[prv] >= a[cur] and a[prv] < a[cur]+w[cur]) {
				sz[prv] = sz[cur] + 1;
				dp[prv] = dp[cur]/sz[prv];
			}
		}
	}

	for (int u: topo) {
		if (a[u] >= a[b[u]] and a[u] < a[b[u]]+w[b[u]]) {
			sz[u] = sz[b[u]] + 1;
			dp[u] = dp[b[u]]/sz[u];
		}
	}

	for (int u = 0; u < n; u++) {
		cout<<(dp[u]*w[u] + a[u])<<' ';
	}
	cout<<'\n';
}

signed main() {
	fastio;
	int tt; cin>>tt;
	while (tt--) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 140ms
memory: 3680kb

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 
665922935 577926775 132646723 421298438 601054667 99438820...

result:

wrong answer 66th numbers differ - expected: '706855769', found: '649360883'