QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#223738#7616. Jump Graphucup-team266#WA 2ms11868kbC++141.9kb2023-10-22 16:17:362023-10-22 16:17:37

Judging History

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

  • [2023-10-22 16:17:37]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:11868kb
  • [2023-10-22 16:17:36]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const int Mod = 1e9 + 7;
inline void uadd(int &a, int b){ a += b-Mod, a += (a>>31) & Mod; }
inline void usub(int &a, int b){ a -= b, a += (a>>31) & Mod; }
inline void umul(int &a, int b){ a = (int)(1ll * a * b % Mod); }
inline int add(int a, int b){ a += b-Mod, a += (a>>31) & Mod; return a; }
inline int sub(int a, int b){ a -= b, a += (a>>31) & Mod; return a; }
inline int mul(int a, int b){ return (int)(1ll * a * b % Mod); }
int qpow(int b, int p){ int ret = 1; while(p){ if(p&1) umul(ret, b); umul(b, b), p >>= 1; } return ret; }

int n, p[300300];
int fa[300300], ch[2][300300], sz[300300];
ll f[2][300300], sum[2][300300], len[2][300300], ans[300300];

void dfs0(int u){
	if(!u) return ;
	dfs0(ch[0][u]), dfs0(ch[1][u]);
	sz[u] = sz[ch[0][u]] + sz[ch[1][u]] + 1;
	rep(d, 2) f[d][u] = sum[d^1][ch[d][u]] + sz[ch[d][u]];
	rep(d, 2) sum[d][u] = sum[d][ch[d][u]] + f[d^1][u], len[d][u] = len[d][ch[d][u]] + 1;
}
void dfs1(int u){
	if(!u) return ;
	rep(d, 2) if(ch[d][u]) ans[ch[d][u]] += f[d^1][u] + ans[u], dfs1(ch[d][u]);
	ans[u] += f[0][u] + f[1][u] + (n - sz[u]);
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n; for(int i = 1; i <= n; ++i) cin >> p[i];
	stack<int> stk;
	int rt = 0;
	for(int i = 1; i <= n; ++i){
		int lst = 0;
		while(!stk.empty() && p[stk.top()] < p[i]) lst = stk.top(), stk.pop();
		//cout << i << ": " << lst << "\n";
		if(lst) fa[lst] = i, ch[0][i] = lst;
		if(!stk.empty()) ch[1][stk.top()] = i, fa[i] = stk.top();
		else rt = i;
		stk.push(i);
	}

	//rep(u, n) cout << u << ": " << fa[u] << "\n";
	
	dfs0(rt);
	dfs1(rt);

	for(int i = 1; i <= n; ++i) cout << ans[i] << " ";
	cout << "\n";

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6
1 6 3 2 5 4

output:

11 7 7 7 6 8 

result:

ok single line: '11 7 7 7 6 8 '

Test #2:

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

input:

2
1 2

output:

1 1 

result:

ok single line: '1 1 '

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 11828kb

input:

36
9 29 1 3 14 31 24 21 10 18 22 16 8 7 15 12 17 19 25 28 27 34 11 6 32 4 20 13 2 35 23 26 33 36 30 5

output:

101 98 99 99 100 87 81 79 79 79 73 72 71 71 70 72 72 77 83 94 107 100 120 120 116 118 116 116 116 113 142 142 143 143 175 175 

result:

wrong answer 1st lines differ - expected: '92 89 90 90 91 78 73 71 71 71 ...110 107 136 136 137 136 168 168', found: '101 98 99 99 100 87 81 79 79 7...16 113 142 142 143 143 175 175 '