結果

問題 No.801 エレベーター
ユーザー ひばちひばち
提出日時 2019-11-17 22:38:27
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 11,327 bytes
コンパイル時間 1,326 ms
コンパイル使用メモリ 121,764 KB
実行使用メモリ 52,148 KB
最終ジャッジ日時 2024-09-25 06:11:29
合計ジャッジ時間 7,685 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
34,456 KB
testcase_01 AC 31 ms
25,652 KB
testcase_02 AC 32 ms
25,440 KB
testcase_03 AC 214 ms
33,944 KB
testcase_04 AC 236 ms
33,784 KB
testcase_05 AC 206 ms
32,132 KB
testcase_06 AC 211 ms
32,236 KB
testcase_07 AC 213 ms
31,872 KB
testcase_08 AC 220 ms
32,180 KB
testcase_09 AC 210 ms
33,912 KB
testcase_10 AC 208 ms
31,712 KB
testcase_11 AC 205 ms
33,804 KB
testcase_12 AC 210 ms
32,260 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using static Template;
using static System.Console;
using static System.Convert;
using static System.Math;
using Pi = Pair<int, int>;
using Number = System.Int64;

class Solver
{
    Scanner sc = new Scanner();
    //

    public void Solve()
    {
        int N, M, K;
        sc.Make(out N, out M, out K);
        var lrs = sc.ArrInt2D(M);
        var dp = new LazySegmentTree<ModInt, long>(N, 0, 0, (a, b) => a + b, (a, b) => a + b, (a, b) => a + b, (a, b) => a * b);
        dp[0] = 1;
        dp.All_Update();
        while (K-- > 0)
        {
            var newdp= new LazySegmentTree<ModInt, long>(N, 0, 0, (a, b) => a + b, (a, b) => a + b, (a, b) => a + b, (a, b) => a * b);
            foreach (var lr in lrs)
            {
                int l = lr[0]-1, r = lr[1];
                newdp.Update(l, r, dp.Query(l, r));
            }
            swap(ref dp, ref newdp);
        }
        WriteLine(dp.Query(N - 1, N));
    }
}

public class LazySegmentTree<T, E>
where E : IComparable<E>
{
    protected readonly int num;
    protected readonly T minT;
    protected readonly T[] item;
    protected readonly E minE;
    protected readonly E[] lazy;
    protected readonly Func<T, T, T> func;
    protected readonly Func<T, E, T> updateFunc;
    protected readonly Func<E, E, E> lazyFunc;
    protected readonly Func<E, int, E> secf;

    public T this[int i]
    {
        get { return item[i + num - 1]; }
        set { item[i + num - 1] = value; }
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected int Parent(int index)
        => (index - 1) >> 1;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected int Left(int index)
        => (index << 1) + 1;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected int Right(int index)
        => (index + 1) << 1;
    public LazySegmentTree(int num, T minT, E minE, Func<T, T, T> func, Func<T, E, T> updateFunc = null, Func<E, E, E> lazyFunc = null, Func<E, int, E> secf = null)
    {
        this.func = func;
        this.num = 1;
        this.minT = minT;
        this.minE = minE;
        this.updateFunc = updateFunc ?? ((a, b) => a);
        this.lazyFunc = lazyFunc ?? ((a, b) => b);
        this.secf = secf ?? ((a, b) => a);

        while (this.num <= num)
            this.num <<= 1;
        item = new T[2 * this.num - 1];
        for (var i = 0; i < 2 * this.num - 1; i++)
            item[i] = minT;
        lazy = Enumerable.Repeat(0, 2 * this.num + 1).Select(_ => minE).ToArray();
    }
    protected void eval(int len, int k)
    {
        if (lazy[k].CompareTo(minE) == 0) return;
        if (Right(k) < num * 2)
        {
            lazy[Left(k)] = lazyFunc(lazy[Left(k)], lazy[k]);
            lazy[Right(k)] = lazyFunc(lazy[Right(k)], lazy[k]);
        }
        item[k] = updateFunc(item[k], secf(lazy[k], len));
        lazy[k] = minE;
    }
    public void All_Update()
    {
        for (int i = num - 2; i >= 0; i--)
            item[i] = func(item[Left(i)], item[Right(i)]);
    }
    public void Update(int left, int right, E value)
        => Update(left, right, 0, 0, num, value);
    protected void Update(int left, int right, int k, int l, int r, E value)
    {
        eval(r - l, k);
        if (r <= left || right <= l) return;
        if (left <= l && r <= right)
        {
            lazy[k] = lazyFunc(lazy[k], value);
            eval(r - l, k);
        }
        else
        {
            Update(left, right, Left(k), l, (l + r) >> 1, value);
            Update(left, right, Right(k), (l + r) >> 1, r, value);
            item[k] = func(item[Left(k)], item[Right(k)]);
        }
    }
    public T Query(int left, int right)
        => Query(left, right, 0, 0, num);
    protected T Query(int left, int right, int k, int l, int r)
    {
        if (r <= left || right <= l) return minT;
        eval(r - l, k);
        if (left <= l && r <= right) return item[k];
        else
            return func(Query(left, right, Left(k), l, (l + r) >> 1), Query(left, right, Right(k), (l + r) >> 1, r));
    }
}

public struct ModInt
{
    public const long MOD = (int)1e9 + 7;
    //public const long MOD = 998244353;
    public long num { get; set; }
    public ModInt(long n = 0) { num = n; }
    private static ModInt[] _fac;//階乗
    private static ModInt[] _inv;//逆数
    private static ModInt[] _facrev;//1/(i!)
    public override string ToString()
        => num.ToString();
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator +(ModInt l, ModInt r)
    {
        l.num += r.num;
        if (l.num >= MOD) l.num -= MOD;
        return l;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator -(ModInt l, ModInt r)
    {
        l.num -= r.num;
        if (l.num < 0) l.num += MOD;
        return l;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator *(ModInt l, ModInt r)
        => new ModInt(l.num * r.num % MOD);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator /(ModInt l, ModInt r)
        => l * Pow(r, MOD - 2);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static implicit operator long(ModInt l)
        => l.num;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static implicit operator ModInt(long n)
    {
        n %= MOD; if (n < 0) n += MOD;
        return new ModInt(n);
    }

    public static ModInt Pow(ModInt m, long n)
    {
        if (n == 0) return 1;
        if (n % 2 == 0) return Pow(m * m, n >> 1);
        else return Pow(m * m, n >> 1) * m;
    }

    public static void CombBuild(int n)
    {
        _fac = new ModInt[n + 1];
        _facrev = new ModInt[n + 1];
        _inv = new ModInt[n + 1];
        _inv[1] = 1;
        _fac[0] = _fac[1] = 1;
        _facrev[0] = _facrev[1] = 1;
        for (var i = 2; i <= n; i++)
        {
            _fac[i] = _fac[i - 1] * i;
            _inv[i] = MOD - _inv[MOD % i] * (MOD / i);
            _facrev[i] = _facrev[i - 1] * _inv[i];
        }
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt Fac(ModInt n)
        => _fac[n];
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt Div(ModInt n)
        => _inv[n];
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt FacRev(ModInt n)
        => _facrev[n];
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt Comb(ModInt n, ModInt r)
    {
        if (n < r) return 0;
        if (n == r) return 1;
        var calc = _fac[n];
        calc = calc * _facrev[r];
        calc = calc * _facrev[n - r];
        return calc;
    }
}
#region Template
public class Template
{
    static void Main(string[] args)
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver().Solve();
        Console.Out.Flush();
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmin<T>(ref T num, T val) where T : IComparable<T>
    { if (num.CompareTo(val) == 1) { num = val; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmax<T>(ref T num, T val) where T : IComparable<T>
    { if (num.CompareTo(val) == -1) { num = val; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void swap<T>(ref T v1, ref T v2)
    { var t = v2; v2 = v1; v1 = t; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static T[] Create<T>(int n, Func<T> f)
    {
        var rt = new T[n];
        for (var i = 0; i < rt.Length; ++i)
            rt[i] = f();
        return rt;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static T[] Create<T>(int n, Func<int, T> f)
    {
        var rt = new T[n];
        for (var i = 0; i < rt.Length; ++i)
            rt[i] = f(i);
        return rt;
    }
    public static void Fail<T>(T s) { Console.WriteLine(s); Console.Out.Close(); Environment.Exit(0); }
}

public class Scanner
{
    public string Str => ReadLine().Trim();
    public int Int => int.Parse(Str);
    public long Long => long.Parse(Str);
    public double Double => double.Parse(Str);
    public int[] ArrInt => Str.Split(' ').Select(int.Parse).ToArray();
    public long[] ArrLong => Str.Split(' ').Select(long.Parse).ToArray();
    public char[][] Grid(int n) => Create(n, () => Str.ToCharArray());
    public int[] ArrInt1D(int n) => Create(n, () => Int);
    public long[] ArrLong1D(int n) => Create(n, () => Long);
    public int[][] ArrInt2D(int n) => Create(n, () => ArrInt);
    public long[][] ArrLong2D(int n) => Create(n, () => ArrLong);
    public Pair<T1, T2> PairMake<T1, T2>() => new Pair<T1, T2>(Next<T1>(), Next<T2>());
    public Pair<T1, T2, T3> PairMake<T1, T2, T3>() => new Pair<T1, T2, T3>(Next<T1>(), Next<T2>(), Next<T3>());
    private Queue<string> q = new Queue<string>();
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public T Next<T>() { if (q.Count == 0) foreach (var item in Str.Split(' ')) q.Enqueue(item); return (T)Convert.ChangeType(q.Dequeue(), typeof(T)); }
    public void Make<T1>(out T1 v1) => v1 = Next<T1>();
    public void Make<T1, T2>(out T1 v1, out T2 v2) { v1 = Next<T1>(); v2 = Next<T2>(); }
    public void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3) { Make(out v1, out v2); v3 = Next<T3>(); }
    public void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4) { Make(out v1, out v2, out v3); v4 = Next<T4>(); }
    public void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5) { Make(out v1, out v2, out v3, out v4); v5 = Next<T5>(); }
    public void Make<T1, T2, T3, T4, T5, T6>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6) { Make(out v1, out v2, out v3, out v4, out v5); v6 = Next<T6>(); }
}
public class Pair<T1, T2> : IComparable<Pair<T1, T2>>
{
    public T1 v1;
    public T2 v2;
    public Pair() { }
    public Pair(T1 v1, T2 v2)
    { this.v1 = v1; this.v2 = v2; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int CompareTo(Pair<T1, T2> p)
    {
        var c = Comparer<T1>.Default.Compare(v1, p.v1);
        if (c == 0)
            c = Comparer<T2>.Default.Compare(v2, p.v2);
        return c;
    }
    public override string ToString()
        => $"{v1.ToString()} {v2.ToString()}";
    public void Deconstruct(out T1 a, out T2 b) { a = v1; b = v2; }
}

public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>>
{
    public T3 v3;
    public Pair() : base() { }
    public Pair(T1 v1, T2 v2, T3 v3) : base(v1, v2)
    { this.v3 = v3; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int CompareTo(Pair<T1, T2, T3> p)
    {
        var c = base.CompareTo(p);
        if (c == 0)
            c = Comparer<T3>.Default.Compare(v3, p.v3);
        return c;
    }
    public override string ToString()
        => $"{base.ToString()} {v3.ToString()}";
    public void Deconstruct(out T1 a, out T2 b, out T3 c) { Deconstruct(out a, out b); c = v3; }
}
#endregion
0