Submission #904979


Source Code Expand

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

// (づ°ω°)づミe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    class SparseTable<T>
    {
        private const int MAX_LENGTH = 200000;
        private static int[] log = new int[MAX_LENGTH + 1];

        private readonly Comparison<T> comparison;
        private readonly int[,] table;
        private readonly T[] data;
        private readonly int length;
        private readonly int height;

        static SparseTable()
        {
            int c = 1;
            for (int i = 1; c <= MAX_LENGTH; i++)
                for (; c <= MAX_LENGTH && c < 1 << i; c++)
                    log[c] = i - 1;
        }

        public SparseTable(Comparison<T> comparison, T[] array)
        {
            this.comparison = comparison;
            data = array;
            length = array.Length;
            height = log[length] + 1;

            table = new int[length, height];
            for (int i = 0; i < length; i++)
                table[i, 0] = i;

            for (int i = 1; i < height; i++)
            {
                int bi = 1 << i;
                int pbi = 1 << i - 1;
                for (int j = 0; j + bi <= length; j++)
                {
                    int left = table[j, i - 1];
                    int right = table[j + pbi, i - 1];
                    if (comparison(data[left], data[right]) < 0)
                        table[j, i] = left;
                    else
                        table[j, i] = right;
                }
            }
        }

        public int Query(int l, int r)
        {
            int w = log[r - l + 1];
            int left = table[l, w];
            int right = table[r - (1 << w) + 1, w];
            if (comparison(data[left], data[right]) < 0)
                return left;
            return right;
        }
    }

    public void Solve()
    {
        int n = ReadInt();
        var a = ReadIntArray();

        var table = new SparseTable<int>((x1, x2) => x1.CompareTo(x2), a);

        var stack = new Stack<Tuple<int, int>>();
        stack.Push(Tuple.Create(0, n - 1));
        long ans = 0;
        while (stack.Count > 0)
        {
            int l = stack.Peek().Item1;
            int r = stack.Pop().Item2;
            if (l > r)
                continue;
            int x = table.Query(l, r);
            ans += 1L * a[x] * (x - l + 1) * (r - x + 1);
            stack.Push(Tuple.Create(l, x - 1));
            stack.Push(Tuple.Create(x + 1, r));
        }

        Write(ans);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            new Solver().Solve();
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; }
    #endregion
}

Submission Info

Submission Time
Task B - Minimum Sum
User azukun
Language C# (Mono 4.6.2.0)
Score 400
Code Size 6249 Byte
Status AC
Exec Time 217 ms
Memory 33760 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 13
Set Name Test Cases
Sample example0, example1, example2
All corner0, corner1, corner2, corner3, example0, example1, example2, maxrand0, maxrand1, maxrand2, rand0, rand1, rand2
Case Name Status Exec Time Memory
corner0 AC 205 ms 33760 KB
corner1 AC 198 ms 33512 KB
corner2 AC 28 ms 3800 KB
corner3 AC 208 ms 33632 KB
example0 AC 28 ms 3800 KB
example1 AC 27 ms 3800 KB
example2 AC 27 ms 3800 KB
maxrand0 AC 217 ms 33636 KB
maxrand1 AC 215 ms 33640 KB
maxrand2 AC 215 ms 33632 KB
rand0 AC 28 ms 3800 KB
rand1 AC 28 ms 3800 KB
rand2 AC 28 ms 3800 KB