結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー 👑 kakel-sankakel-san
提出日時 2023-12-23 20:56:38
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 318 ms / 2,000 ms
コード長 2,923 bytes
コンパイル時間 7,195 ms
コンパイル使用メモリ 158,468 KB
実行使用メモリ 180,600 KB
最終ジャッジ日時 2023-12-23 20:56:54
合計ジャッジ時間 16,069 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
33,060 KB
testcase_01 AC 298 ms
75,120 KB
testcase_02 AC 304 ms
75,120 KB
testcase_03 AC 292 ms
75,120 KB
testcase_04 AC 318 ms
75,124 KB
testcase_05 AC 294 ms
75,120 KB
testcase_06 AC 295 ms
75,116 KB
testcase_07 AC 298 ms
75,120 KB
testcase_08 AC 297 ms
75,120 KB
testcase_09 AC 291 ms
75,120 KB
testcase_10 AC 296 ms
75,124 KB
testcase_11 AC 311 ms
75,364 KB
testcase_12 AC 286 ms
75,232 KB
testcase_13 AC 285 ms
75,364 KB
testcase_14 AC 304 ms
83,228 KB
testcase_15 AC 79 ms
33,060 KB
testcase_16 AC 87 ms
33,060 KB
testcase_17 AC 93 ms
33,060 KB
testcase_18 AC 78 ms
33,060 KB
testcase_19 AC 78 ms
33,060 KB
testcase_20 AC 78 ms
33,060 KB
testcase_21 AC 78 ms
33,060 KB
testcase_22 AC 77 ms
33,060 KB
testcase_23 AC 77 ms
33,060 KB
testcase_24 AC 79 ms
180,600 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (99 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.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();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, q) = (c[0], c[1]);
        var s = ReadLine().ToCharArray();
        var query = NArr(q);
        var ft = new FenwickTree(n * 2);
        for (var i = 0; i + 1 < n; ++i) if (s[i] == '(' && s[i + 1] == ')') ft.Add(i * 2 + 1, 1);
        var ans = new List<long>();
        foreach (var que in query)
        {
            if (que[0] == 1)
            {
                var i = que[1] - 1;
                if (s[i] == '(')
                {
                    s[i] = ')';
                    if (i + 1 < n && s[i + 1] == ')') ft.Add(i * 2 + 1, -1);
                    if (i - 1 >= 0 && s[i - 1] == '(') ft.Add(i * 2 - 1, 1);
                }
                else
                {
                    s[i] = '(';
                    if (i + 1 < n && s[i + 1] == ')') ft.Add(i * 2 + 1, 1);
                    if (i - 1 >= 0 && s[i - 1] == '(') ft.Add(i * 2 - 1, -1);
                }
            }
            else
            {
                var l = que[1] - 1;
                var r = que[2] - 1;
                ans.Add(ft.Sum(r * 2) - ft.Sum(l * 2));
            }
        }
        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;
        }
        /// <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