Submission #50390978


Source Code Expand

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Tasks;

public class E
{
    public static void Main()
    {
        using var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        Solve();
        Console.Out.Flush();
    }

    public static void Solve()
    {
        var (N, Q) = Scanner.Scan<int, int>();
        var S = Scanner.Scan<string>().ToArray();
        var A = new int[N - 1];

        var st = new SegmentTree<int>(N + 1, new Oracle());
        for (var i = 0; i + 1 < N; i++)
        {
            st.Set(i + 1, S[i] == S[i + 1] ? 0 : 1);
        }

        while (Q-- > 0)
        {
            var (t, l, r) = Scanner.Scan<int, int, int>();
            if (t == 1)
            {
                st.Set(l - 1, 1 - st.Get(l - 1));
                st.Set(r, 1 - st.Get(r));
            }
            else
            {
                var answer = st.Query(l, r) == r - l;
                Console.WriteLine(answer ? "Yes" : "No");
            }
        }
    }

    public class Oracle : IOracle<int>
    {
        public int IdentityElement => 0;

        public int Operate(int a, int b)
        {
            return a + b;
        }
    }

    public interface IOracle<TMonoid>
    {
        TMonoid IdentityElement { get; }
        TMonoid Operate(TMonoid a, TMonoid b);
    }



    public class SegmentTree<TMonoid>
    {
        public int Length { get; }
        private readonly IOracle<TMonoid> _oracle;
        private readonly TMonoid[] _data;
        private readonly int _log;
        private readonly int _dataSize;
        public SegmentTree(IReadOnlyCollection<TMonoid> source, IOracle<TMonoid> oracle) : this(source.Count, oracle)
        {
            var idx = _dataSize;
            foreach (var value in source) _data[idx++] = value;
            for (var i = _dataSize - 1; i >= 1; i--) Update(i);
        }
        public SegmentTree(int length, IOracle<TMonoid> oracle)
        {
            if (length < 0) throw new ArgumentOutOfRangeException(nameof(length));
            Length = length;
            _oracle = oracle;
            while (1 << _log < Length) _log++;
            _dataSize = 1 << _log;
            _data = new TMonoid[_dataSize << 1];
            Array.Fill(_data, oracle.IdentityElement);
        }
        public void Set(int index, TMonoid value)
        {
            if (index < 0 || Length <= index) throw new ArgumentOutOfRangeException(nameof(index));
            index += _dataSize;
            _data[index] = value;
            for (var i = 1; i <= _log; i++) Update(index >> i);
        }
        public TMonoid Get(int index)
        {
            if (index < 0 || Length <= index) throw new ArgumentOutOfRangeException(nameof(index));
            return _data[index + _dataSize];
        }
        public TMonoid Query(int left, int right)
        {
            if (left < 0 || right < left || Length < right) throw new ArgumentOutOfRangeException();
            var (sml, smr) = (_oracle.IdentityElement, _oracle.IdentityElement);
            left += _dataSize;
            right += _dataSize;
            while (left < right)
            {
                if ((left & 1) == 1) sml = _oracle.Operate(sml, _data[left++]);
                if ((right & 1) == 1) smr = _oracle.Operate(_data[--right], smr);
                left >>= 1;
                right >>= 1;
            }
            return _oracle.Operate(sml, smr);
        }
        public TMonoid QueryToAll() => _data[1];
        public int MaxRight(int left, Func<TMonoid, bool> predicate)
        {
            if (left < 0 || Length < left) throw new ArgumentOutOfRangeException(nameof(left));
            if (predicate is null) throw new ArgumentNullException(nameof(predicate));
            if (!predicate(_oracle.IdentityElement)) throw new ArgumentException(nameof(predicate));
            if (left == Length) return Length;
            left += _dataSize;
            var sm = _oracle.IdentityElement;
            do
            {
                while ((left & 1) == 0) left >>= 1;
                if (!predicate(_oracle.Operate(sm, _data[left])))
                {
                    while (left < _dataSize)
                    {
                        left <<= 1;
                        var tmp = _oracle.Operate(sm, _data[left]);
                        if (!predicate(tmp)) continue;
                        sm = tmp;
                        left++;
                    }
                    return left - _dataSize;
                }
                sm = _oracle.Operate(sm, _data[left]);
                left++;
            } while ((left & -left) != left);
            return Length;
        }
        public int MinLeft(int right, Func<TMonoid, bool> predicate)
        {
            if (right < 0 || Length < right) throw new ArgumentOutOfRangeException(nameof(right));
            if (predicate is null) throw new ArgumentNullException(nameof(predicate));
            if (!predicate(_oracle.IdentityElement)) throw new ArgumentException(nameof(predicate));
            if (right == 0) return 0;
            right += _dataSize;
            var sm = _oracle.IdentityElement;
            do
            {
                right--;
                while (right > 1 && (right & 1) == 1) right >>= 1;
                if (!predicate(_oracle.Operate(_data[right], sm)))
                {
                    while (right < _dataSize)
                    {
                        right = (right << 1) + 1;
                        var tmp = _oracle.Operate(_data[right], sm);
                        if (!predicate(tmp)) continue;
                        sm = tmp;
                        right--;
                    }
                    return right + 1 - _dataSize;
                }
                sm = _oracle.Operate(_data[right], sm);
            } while ((right & -right) != right);
            return 0;
        }
        private void Update(int k) => _data[k] = _oracle.Operate(_data[k << 1], _data[(k << 1) + 1]);
    }

    public static class Scanner
    {
        public static T Scan<T>() where T : IConvertible => Convert<T>(ScanStringArray()[0]);
        public static (T1, T2) Scan<T1, T2>() where T1 : IConvertible where T2 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]));
        }
        public static (T1, T2, T3) Scan<T1, T2, T3>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]));
        }
        public static (T1, T2, T3, T4) Scan<T1, T2, T3, T4>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]), Convert<T4>(input[3]));
        }
        public static (T1, T2, T3, T4, T5) Scan<T1, T2, T3, T4, T5>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]), Convert<T4>(input[3]), Convert<T5>(input[4]));
        }
        public static (T1, T2, T3, T4, T5, T6) Scan<T1, T2, T3, T4, T5, T6>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible where T6 : IConvertible
        {
            var input = ScanStringArray();
            return (Convert<T1>(input[0]), Convert<T2>(input[1]), Convert<T3>(input[2]), Convert<T4>(input[3]), Convert<T5>(input[4]), Convert<T6>(input[5]));
        }
        public static IEnumerable<T> ScanEnumerable<T>() where T : IConvertible => ScanStringArray().Select(Convert<T>);
        private static string[] ScanStringArray()
        {
            var line = Console.ReadLine()?.Trim() ?? string.Empty;
            return string.IsNullOrEmpty(line) ? Array.Empty<string>() : line.Split(' ');
        }
        private static T Convert<T>(string value) where T : IConvertible => (T)System.Convert.ChangeType(value, typeof(T));
    }
}

Submission Info

Submission Time
Task E - Alternating String
User AconCavy
Language C# 11.0 (.NET 7.0.7)
Score 450
Code Size 8621 Byte
Status AC
Exec Time 426 ms
Memory 61756 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 2
AC × 42
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 57 ms 26380 KiB
example_01.txt AC 50 ms 26288 KiB
hand_00.txt AC 355 ms 61332 KiB
hand_01.txt AC 340 ms 61688 KiB
hand_02.txt AC 390 ms 61316 KiB
hand_03.txt AC 342 ms 61656 KiB
hand_04.txt AC 237 ms 54548 KiB
hand_05.txt AC 345 ms 61628 KiB
hand_06.txt AC 351 ms 61352 KiB
hand_07.txt AC 342 ms 61492 KiB
hand_08.txt AC 340 ms 61688 KiB
hand_09.txt AC 344 ms 61220 KiB
random_00.txt AC 386 ms 61340 KiB
random_01.txt AC 377 ms 61312 KiB
random_02.txt AC 402 ms 61368 KiB
random_03.txt AC 386 ms 61304 KiB
random_04.txt AC 100 ms 34780 KiB
random_05.txt AC 95 ms 35100 KiB
random_06.txt AC 375 ms 61400 KiB
random_07.txt AC 382 ms 61624 KiB
random_08.txt AC 406 ms 61508 KiB
random_09.txt AC 398 ms 61392 KiB
random_10.txt AC 99 ms 34864 KiB
random_11.txt AC 101 ms 34956 KiB
random_12.txt AC 386 ms 61524 KiB
random_13.txt AC 386 ms 61756 KiB
random_14.txt AC 388 ms 61436 KiB
random_15.txt AC 381 ms 61308 KiB
random_16.txt AC 97 ms 34808 KiB
random_17.txt AC 98 ms 34784 KiB
random_18.txt AC 388 ms 61732 KiB
random_19.txt AC 392 ms 61692 KiB
random_20.txt AC 413 ms 61600 KiB
random_21.txt AC 400 ms 61260 KiB
random_22.txt AC 101 ms 35148 KiB
random_23.txt AC 105 ms 35064 KiB
random_24.txt AC 406 ms 61540 KiB
random_25.txt AC 405 ms 61540 KiB
random_26.txt AC 426 ms 61556 KiB
random_27.txt AC 393 ms 61572 KiB
random_28.txt AC 101 ms 35004 KiB
random_29.txt AC 102 ms 34748 KiB