public void Solve() { // AGC005 B int N = NextInt(); var A = NextIntList(); var index = new Map <int, int>(); A.ForeachWith((idx, item) => { index[item - 1] = idx; }); var suf = new SectionUnionFind(N); var ans = 0L; for (int i = N - 1; i >= 0; i--) { var k = index[i]; ans += A[k]; if (k - 1 >= 0 && A[k - 1] > A[k]) { //???????????????Unite ans += (long)suf.Unite(k - 1, k) * A[k]; } if (k + 1 < N && A[k + 1] > A[k]) { //???????????????Unite ans += (long)suf.Unite(k + 1, k) * A[k]; } } ans.WL(); return; }
public void Solve() { var N = NextInt(); long[] a = new long[N], aindex = new long[N]; N.REP(i => { a[i] = NextLong(); aindex[a[i] - 1] = i; }); var suf = new SectionUnionFind(N); var ans = 0L; for (var k = N - 1; k >= 0; k--) { var i = (int)aindex[k]; ans += a[i]; if (i - 1 >= 0 && a[i - 1] > a[i]) { ans += suf.Unite(i - 1, i) * a[i]; } if (i + 1 < N && a[i + 1] > a[i]) { ans += suf.Unite(i + 1, i) * a[i]; } } ans.WL(); }
public void Solve() { // AGC005 B var N = NextInt(); var A = NextLongList(); //Root??????????Union-Find??? var suf = new SectionUnionFind(N); var pos = new int[N]; for (int i = 0; i < N; i++) { pos[A[i] - 1] = i; } var ans = 0L; for (int i = N - 1; i >= 0; i--) { var idx = pos[i]; ans += A[idx]; //???idx?????????????? if (idx - 1 >= 0 && A[idx - 1] > A[idx]) { ans += suf.Unite(idx, idx - 1) * A[idx]; } if (idx + 1 < N && A[idx + 1] > A[idx]) { ans += suf.Unite(idx, idx + 1) * A[idx]; } } ans.WL(); return; }