We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent bc474e1 commit d4f49a3Copy full SHA for d4f49a3
AtCoderBeginnerContest351/F/Program.cs
@@ -24,10 +24,23 @@ public void Solve()
24
var N = Ri();
25
var As = Rla();
26
27
- foreach (var A in As)
+ 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)
37
{
-
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;
41
}
42
+
43
+ Console.WriteLine(ans);
44
45
46
static string Rs(){return Console.ReadLine();}
0 commit comments