結果

問題 No.2942 Sigma Music Game Level Problem
ユーザー kakel-san
提出日時 2024-11-13 23:41:41
言語 C#
(.NET 8.0.404)
結果
AC  
実行時間 1,845 ms / 6,000 ms
コード長 2,575 bytes
コンパイル時間 14,913 ms
コンパイル使用メモリ 167,124 KB
実行使用メモリ 273,208 KB
最終ジャッジ日時 2024-11-15 09:25:14
合計ジャッジ時間 31,076 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (104 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

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();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, q) = (c[0], c[1]);
        var a = NList;
        var ans = new List<string>();
        var cf = new FenwickTree(200002);
        var df = new FenwickTree(200002);
        foreach (var ai in a)
        {
            cf.Add(ai, 1);
            df.Add(ai, ai);
        }
        for (var i = 0; i < q; ++i)
        {
            c = NList;
            if (c[0] == 1)
            {
                cf.Add(c[1], 1);
                df.Add(c[1], c[1]);
            }
            else if (c[0] == 2)
            {
                ans.Add($"{cf.Sum(c[2]) - cf.Sum(c[1] - 1)} {df.Sum(c[2]) - df.Sum(c[1] - 1)}");
            }
        }
        if (ans.Count == 0) WriteLine("Not Found!");
        else WriteLine(string.Join("\n", ans));
    }
    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)
        {
            if (index < 0) return 0;
            ++index;
            var sum = 0L;
            for (var x = index; x > 0; x -= (x & -x)) sum += tree[x];
            return sum;
        }
        public long Get(int index)
        {
            if (index == 0) return Sum(0);
            return Sum(index) - Sum(index - 1);
        }
        /// <summary>Sum(x) >= value となる最小のxを求める</summary>
        // 各要素は非負であること
        public int LowerBound(long value)
        {
            if (value < 0) return -1;
            var x = 0;
            var b = 1;
            while (b * 2 <= size) b <<= 1;
            for (var k = b; k > 0; k >>= 1)
            {
                if (x + k <= size && tree[x + k] < value)
                {
                    value -= tree[x + k];
                    x += k;
                }
            }
            return x;
        }
    }
}
0