Beispiel #1
0
 public static void Run()
 {
     using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
         using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
         {
             int            n = fs.NextInt();
             int[]          a = Array.ConvertAll(fs.ReadLine().Split(), Convert.ToInt32);
             SumSegmentTree seg = new SumSegmentTree(new long[n + 1]);
             double         expDiff = 0, denom = n * 1L * (n + 1) / 2, expInvCount = 0;
             long           inv = 0;
             for (int i = n - 1; i >= 0; i--)
             {
                 inv         += seg.QuerySum(0, a[i] - 1);
                 expInvCount += ExpectedInverseCount(n - i);
                 expDiff     += (expInvCount - inv) / denom;
                 seg.Update(a[i], n - i);
             }
             long initialInverseCount = 0;
             seg = new SumSegmentTree(new long[n + 1]);
             for (int i = n - 1; i >= 0; i--)
             {
                 initialInverseCount += seg.QuerySum(0, a[i] - 1);
                 seg.Update(a[i], 1);
             }
             writer.WriteLine((initialInverseCount + expDiff).ToString().Replace(",", "."));
         }
 }
Beispiel #2
0
        public void CanUpdateValue()
        {
            var sut = new SumSegmentTree(new[] { 1, 3, 5, 7, 9, 11 });

            Assert.Equal(15, sut.GetSegmentValue(1, 3));

            sut.Update(2, 10);

            Assert.Equal(20, sut.GetSegmentValue(1, 3));
        }
        public static void Run()
        {
            using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
            using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
            {
                int n = fs.NextInt(), q = fs.NextInt();
                LinkedList<int>[] apps = new LinkedList<int>[n];
                SumSegmentTree seg = new SumSegmentTree(new int[q]);
                for (int i = 0; i < n; i++)
			    {
			        apps[i] = new LinkedList<int>();
			    }
                int last = 0, j = 0;
                for (int i = 0; i < q; i++)
                {
                    int t = fs.NextInt(), x = fs.NextInt();
                    if (t == 1)
                    {
                        x--;
                        apps[x].AddLast(j);
                        seg.Update(j, j, 1);
                        j++;
                    }
                    else if (t == 2)
                    {
                        x--;
                        foreach (int notif in apps[x])
                        {
                            seg.Update(notif, notif, 0);
                        }
                        apps[x].Clear();
                    }
                    else
                    {
                        if (x > last)
                        {
                            last = x;
                            seg.Update(0, x - 1, 0);
                        }
                    }
                    writer.WriteLine(seg.QuerySum(0, q - 1));
                    writer.Flush();
                }
            }
        }
Beispiel #4
0
 public void CanConstructSegmentTreeFromArray()
 {
     var sut = new SumSegmentTree(new[] { 1, 3, 5, 7, 9, 11 });
 }
Beispiel #5
0
        public void CanGetSum()
        {
            var sut = new SumSegmentTree(new[] { 1, 3, 5, 7, 9, 11 });

            Assert.Equal(15, sut.GetSegmentValue(1, 3));
        }