結果

問題 No.801 エレベーター
ユーザー ひばち
提出日時 2019-11-17 22:38:27
言語 C#(mono)
(mono 6.0.0.327)
結果
TLE  
実行時間 -
コード長 11,327 Byte
コンパイル時間 546 ms
使用メモリ 43,772 KB
最終ジャッジ日時 2019-11-17 22:38:35

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
0_sample01.txt AC 38 ms
16,924 KB
0_sample02.txt AC 37 ms
14,896 KB
0_sample03.txt AC 39 ms
18,980 KB
1_rand01.txt AC 240 ms
25,720 KB
1_rand02.txt AC 233 ms
21,608 KB
1_rand03.txt AC 218 ms
25,720 KB
1_rand04.txt AC 223 ms
23,748 KB
1_rand05.txt AC 228 ms
25,904 KB
1_rand06.txt AC 264 ms
25,904 KB
1_rand07.txt AC 225 ms
23,720 KB
1_rand08.txt AC 227 ms
23,748 KB
1_rand09.txt AC 221 ms
23,668 KB
1_rand10.txt AC 224 ms
23,860 KB
2_randmax01.txt TLE -
2_randmax02.txt -- -
2_randmax03.txt -- -
2_randmax04.txt -- -
2_randmax05.txt -- -
2_randmax06.txt -- -
2_randmax07.txt -- -
2_randmax08.txt -- -
2_randmax09.txt -- -
2_randmax10.txt -- -
3_max01.txt -- -
3_max02.txt -- -
3_max03.txt -- -
3_max04.txt -- -
3_max05.txt -- -
3_max06.txt -- -
テストケース一括ダウンロード

ソースコード

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