結果

問題 No.2065 Sum of Min
ユーザー 👑 kakel-sankakel-san
提出日時 2022-09-04 01:28:22
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 388 ms / 2,000 ms
コード長 2,403 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 68,680 KB
実行使用メモリ 46,892 KB
最終ジャッジ日時 2023-08-11 04:30:50
合計ジャッジ時間 11,346 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
24,044 KB
testcase_01 AC 63 ms
21,960 KB
testcase_02 AC 64 ms
21,984 KB
testcase_03 AC 67 ms
22,000 KB
testcase_04 AC 328 ms
44,384 KB
testcase_05 AC 326 ms
46,892 KB
testcase_06 AC 306 ms
40,648 KB
testcase_07 AC 335 ms
43,784 KB
testcase_08 AC 360 ms
39,612 KB
testcase_09 AC 377 ms
41,996 KB
testcase_10 AC 367 ms
39,624 KB
testcase_11 AC 373 ms
41,756 KB
testcase_12 AC 383 ms
39,656 KB
testcase_13 AC 388 ms
41,792 KB
testcase_14 AC 385 ms
43,632 KB
testcase_15 AC 387 ms
39,632 KB
testcase_16 AC 384 ms
43,852 KB
testcase_17 AC 381 ms
41,812 KB
testcase_18 AC 387 ms
41,916 KB
testcase_19 AC 380 ms
41,728 KB
testcase_20 AC 384 ms
43,792 KB
testcase_21 AC 380 ms
43,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static void Main()
    {
        var c = NList;
        var (n, q) = (c[0], c[1]);
        var a = NList;

        var lq = new List<(int pos, int val)>(n);
        for (var i = 0; i < n; ++i) lq.Add((i, a[i]));
        lq.Sort((l, r) => r.val.CompareTo(l.val));
        var que = new List<(int id, int l, int r, int x)>(q);
        for (var i = 0; i < q; ++i)
        {
            c = NList;
            que.Add((i, c[0], c[1], c[2]));
        }
        que.Sort((l, r) => l.x.CompareTo(r.x));

        var ftc = new FenwickTree(n);
        for (var i = 0; i < n; ++i) ftc.Add(i, 1);
        var fta = new FenwickTree(n);
        var res = new long[q];
        foreach (var qi in que)
        {
            while (lq.Count > 0 && lq[lq.Count - 1].val < qi.x)
            {
                ftc.Add(lq[lq.Count - 1].pos, -1);
                fta.Add(lq[lq.Count - 1].pos, lq[lq.Count - 1].val);
                lq.RemoveAt(lq.Count - 1);
            }
            var sub = ftc.Sum(qi.r - 1) * qi.x + fta.Sum(qi.r - 1);
            if (qi.l > 1) sub -= ftc.Sum(qi.l - 2) * qi.x + fta.Sum(qi.l - 2);
            res[qi.id] = sub;
        }
        WriteLine(string.Join("\n", res));
    }
    class FenwickTree
    {
        int size;
        long[] tree;
        public FenwickTree(int size)
        {
            this.size = size;
            tree = new long[size + 2];
        }
        public void Add(int index, int value)
        {
            ++index;
            for (var x = index; x <= size; x += (x & -x)) tree[x] += value;
        }
        /// <summary>先頭からindexまでの和(include index)</summary>
        public long Sum(int index)
        {
            ++index;
            var sum = 0L;
            for (var x = index; x > 0; x -= (x & -x)) sum += tree[x];
            return sum;
        }
        public string Debug()
        {
            var sb = new System.Text.StringBuilder();
            for (var i = 0; i < tree.Length - 2; ++i)
            {
                var val = Sum(i);
                if (i > 0) val -= Sum(i - 1);
                sb.Append(val).Append(" ");
            }
            return sb.ToString();
        }
    }
}
0