예제 #1
0
        public void Test004()
        {
            var dat = new[] {
                5.2,
                3.7,
                8.4,
                7.6,
                2.3,
            };
            var solver = new CumulativeSum
            {
                Data = dat,
            }.Prepare();

            Assert.AreEqual(solver.SectionTotal(0, 0), 0);
            Assert.AreEqual(solver.SectionTotal(0, 1), dat[0]);
            Assert.AreEqual(Math.Round(solver.SectionTotal(0, 5), 5), Math.Round(dat[0] + dat[1] + dat[2] + dat[3] + dat[4], 5));
            Assert.AreEqual(Math.Round(solver.SectionTotal(1, 5), 5), Math.Round(dat[1] + dat[2] + dat[3] + dat[4], 5));
            Assert.AreEqual(Math.Round(solver.SectionTotal(1, 4), 5), Math.Round(dat[1] + dat[2] + dat[3], 5));
            Assert.AreEqual(Math.Round(solver.SectionTotal(2, 3), 5), dat[2]);
        }
예제 #2
0
        public void Test003()
        {
            var dat = new[] {
                5.2,
                3.7,
                8.4,
                7.6,
                2.3,
            };
            var solver = new CumulativeSum
            {
                Data = dat,
            }.Prepare();

            Assert.AreEqual(solver.Get(0), 0);
            Assert.AreEqual(solver.Get(1), dat[0]);
            Assert.AreEqual(solver.Get(2), dat[0] + dat[1]);
            Assert.AreEqual(solver.Get(3), dat[0] + dat[1] + dat[2]);
            Assert.AreEqual(solver.Get(4), dat[0] + dat[1] + dat[2] + dat[3]);
            Assert.AreEqual(solver.Get(5), dat[0] + dat[1] + dat[2] + dat[3] + dat[4]);
        }
 public void Init()
 {
     cumSum = new CumulativeSum();
 }
예제 #4
0
파일: 3935098.cs 프로젝트: qifanyyy/CLCDSA
    public static void Main()
    {
        int[] inp = IntLine();
        int   N = inp[0], M = inp[1], Q = inp[2];

        int[,] field = new int[N, M];
        for (int i = 0; i < N; i++)
        {
            int[] b = Console.ReadLine().ToCharArray().Select(c => int.Parse(c.ToString())).ToArray();
            for (int j = 0; j < M; j++)
            {
                field[i, j] = b[j];
            }
        }
        int[,] Qs = new int[Q, 4];
        for (int i = 0; i < Q; i++)
        {
            int[] b = IntLine().Select(v => v - 1).ToArray();
            for (int j = 0; j < 4; j++)
            {
                Qs[i, j] = b[j];
            }
        }

        CumulativeSum node = new CumulativeSum(N, M);

        Array.Copy(field, node.Info, field.Length);
        node.Run();
        CumulativeSum e = new CumulativeSum(N * 2 - 1, M * 2 - 1);

        for (int i = 0; i < N; i++)
        {
            for (int j = 1; j < M; j++)
            {
                if (field[i, j] == 1 && field[i, j - 1] == 1)
                {
                    e.Info[i * 2, j * 2 - 1] = 1;
                }
            }
        }
        for (int i = 1; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                if (field[i, j] == 1 && field[i - 1, j] == 1)
                {
                    e.Info[i * 2 - 1, j * 2] = 1;
                }
            }
        }
        e.Run();

        for (int i = 0; i < Q; i++)
        {
            int y1 = Qs[i, 0], x1 = Qs[i, 1], y2 = Qs[i, 2], x2 = Qs[i, 3];
            int ns = node.Info[y2, x2];
            if (x1 != 0 && y1 != 0)
            {
                ns += -node.Info[y2, x1 - 1] - node.Info[y1 - 1, x2] + node.Info[y1 - 1, x1 - 1];
            }
            else if (y1 != 0)
            {
                ns += -node.Info[y1 - 1, x2];
            }
            else if (x1 != 0)
            {
                ns += -node.Info[y2, x1 - 1];
            }
            y1 *= 2;
            y2 *= 2;
            x1 *= 2;
            x2 *= 2;
            int es = e.Info[y2, x2];
            if (x1 != 0 && y1 != 0)
            {
                es += -e.Info[y2, x1 - 1] - e.Info[y1 - 1, x2] + e.Info[y1 - 1, x1 - 1];
            }
            else if (y1 != 0)
            {
                es += -e.Info[y1 - 1, x2];
            }
            else if (x1 != 0)
            {
                es += -e.Info[y2, x1 - 1];
            }
            Console.WriteLine(ns - es);
        }
    }