Skip to content

Commit d4f49a3

Browse files
committed
add: ABC351 Fを解説AC
1 parent bc474e1 commit d4f49a3

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

Diff for: AtCoderBeginnerContest351/F/Program.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,23 @@ public void Solve()
2424
var N = Ri();
2525
var As = Rla();
2626

27-
foreach (var A in As)
27+
var sortedAs = As
28+
.Select((A, i) => (A, i))
29+
.OrderBy(x => x.A)
30+
.ToArray();
31+
var segTree = new SegTree<(int Cnt, long Sum)>(
32+
N,
33+
(a, b) => (a.Cnt + b.Cnt, a.Sum + b.Sum)
34+
);
35+
long ans = 0;
36+
foreach (var (A, i) in sortedAs)
2837
{
29-
38+
segTree.Set(i, (1, A));
39+
var sum = segTree.Query(0, i);
40+
if (sum.HasValue) ans += sum.Value.Cnt * A - sum.Value.Sum;
3041
}
42+
43+
Console.WriteLine(ans);
3144
}
3245

3346
static string Rs(){return Console.ReadLine();}

0 commit comments

Comments
 (0)