結果
| 問題 |
No.749 クエリ全部盛り
|
| コンテスト | |
| ユーザー |
EmKjp
|
| 提出日時 | 2019-06-01 07:52:53 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 1,831 ms / 3,000 ms |
| コード長 | 11,969 bytes |
| コンパイル時間 | 3,495 ms |
| コンパイル使用メモリ | 120,392 KB |
| 実行使用メモリ | 145,308 KB |
| 最終ジャッジ日時 | 2024-09-17 19:53:41 |
| 合計ジャッジ時間 | 12,722 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using E = System.Linq.Enumerable;
partial class Solver
{
const int Mod = 1000000007;
public struct Monoid
{
public long Value;
public long F;
}
public struct MonoidOperator
{
public long Add;
public long Mul;
public long AddF;
}
public class LazySegmentTreeMonoidOperator : ILazySegmentTreeMonoidOperator<Monoid, MonoidOperator>
{
public Monoid Apply(MonoidOperator operation, Monoid monoid, int dataNum)
{
return new Monoid
{
Value = (monoid.Value * operation.Mul + monoid.F * operation.AddF + dataNum * operation.Add) % Mod,
F = monoid.F,
};
}
public Monoid Merge(Monoid left, Monoid right)
{
return new Monoid
{
Value = (left.Value + right.Value) % Mod,
F = (left.F + right.F) % Mod,
};
}
// (monoid.Value * operation.Mul + monoid.F * operation.AddF + dataNum * operation.Add)
// (monoid.Value * operation.Mul + monoid.F * operation.AddF + dataNum * operation.Add) * operation2.Mul + monoid.F * operation2.AddF + dataNum * operation2.Add)
// monoid.Value * operation.Mul * operation2.Mul + monoid.F * operation.AddF * operation2.Mul + dataNum * operation.Add * operation2.Mul + monoid.F * operation2.AddF + dataNum * operation2.Add
// monoid.Value * operation.Mul * operation2.Mul
// + monoid.F * operation.AddF * operation2.Mul + monoid.F * operation2.AddF
// + dataNum * operation.Add * operation2.Mul + dataNum * operation2.Add
// monoid.Value * operation.Mul * operation2.Mul
// + monoid.F * (operation.AddF * operation2.Mul +operation2.AddF)
// + dataNum * (operation.Add * operation2.Mul * operation2.Add)
public MonoidOperator MergeOperator(MonoidOperator left, MonoidOperator right)
{
return new MonoidOperator
{
Add = (left.Add * right.Mul + right.Add) % Mod,
Mul = (left.Mul * right.Mul) % Mod,
AddF = (left.AddF * right.Mul + right.AddF) % Mod,
};
}
}
public void Run()
{
var N = ni();
var Q = ni();
var tree = new LazySegmentTreeMonoidOperator().Create();
var f = new long[N + 2];
f[0] = 0;
f[1] = 1;
for (int i = 2; i < N; i++) f[i] = (f[i - 1] + f[i - 2]) % Mod;
tree.Build(N, i => new Monoid { F = f[i] });
for (int i = 0; i < Q; i++)
{
var q = ni();
var l = ni();
var r = ni();
var k = ni();
switch (q)
{
case 0:
cout.WriteLine(tree.Query(l, r + 1).Value * k % Mod);
break;
case 1:
tree.LazyUpdate(l, r + 1, new MonoidOperator { Add = k, Mul = 0, AddF = 0 });
break;
case 2:
tree.LazyUpdate(l, r + 1, new MonoidOperator { Add = k, Mul = 1, AddF = 0 });
break;
case 3:
tree.LazyUpdate(l, r + 1, new MonoidOperator { Add = 0, Mul = k, AddF = 0 });
break;
case 4:
tree.LazyUpdate(l, r + 1, new MonoidOperator { Add = 0, Mul = 1, AddF = k });
break;
}
}
}
}
public interface ILazySegmentTreeMonoidOperator<TMonoid, TOperatorMonoid>
where TMonoid : struct
where TOperatorMonoid : struct
{
TMonoid Merge(TMonoid left, TMonoid right);
TOperatorMonoid MergeOperator(TOperatorMonoid left, TOperatorMonoid right);
TMonoid Apply(TOperatorMonoid operation, TMonoid monoid, int dataNum);
}
public static class OperatorLazySegmentTreeExtension
{
static public OperatorLazySegmentTree<TMonoid, TOperatorMonoid> Create<TMonoid, TOperatorMonoid>
(this ILazySegmentTreeMonoidOperator<TMonoid, TOperatorMonoid> lazySegmentTreeMonoidOperator)
where TMonoid : struct
where TOperatorMonoid : struct
{
return new OperatorLazySegmentTree<TMonoid, TOperatorMonoid>(lazySegmentTreeMonoidOperator);
}
}
public abstract class SegmentTree<TMonoid> where TMonoid : struct
{
protected int N { get; private set; }
protected TMonoid?[] node;
protected static int Left(int p) { return p * 2 + 1; }
protected static int Right(int p) { return p * 2 + 2; }
public SegmentTree() { }
public void Build(IList<TMonoid> array)
{
Build(array.Count, i => array[i]);
}
virtual public void Build(int _n, Func<int, TMonoid> create)
{
N = 1;
while (N < _n) N *= 2;
node = new TMonoid?[2 * N];
for (int i = 0; i < _n; i++) node[i + N - 1] = create(i);
for (int i = N - 2; i >= 0; i--) node[i] = Merge(node[Left(i)], node[Right(i)]);
}
protected TMonoid? Merge(TMonoid? left, TMonoid? right)
{
if (left != null && right != null) return Merge(left.Value, right.Value);
if (left == null) return right;
if (right == null) return left;
return null;
}
protected abstract TMonoid Merge(TMonoid left, TMonoid right);
public void Update(int index, TMonoid val)
{
int p = index + N - 1;
node[p] = val;
while (p > 0)
{
p = (p - 1) / 2;
node[p] = Merge(node[Left(p)], node[Right(p)]);
}
}
protected virtual TMonoid? Query(int begin, int end, int k, int l, int r)
{
if (r <= begin || end <= l) return null;
if (begin <= l && r <= end) return node[k];
int mid = (l + r) / 2;
return Merge(
Query(begin, end, Left(k), l, mid),
Query(begin, end, Right(k), mid, r)
);
}
public TMonoid Query(int begin, int end)
{
return Query(begin, end, 0, 0, N).Value;
}
};
public class OperatorLazySegmentTree<TMonoid, TOperatorMonoid> : SegmentTree<TMonoid>
where TMonoid : struct
where TOperatorMonoid : struct
{
readonly ILazySegmentTreeMonoidOperator<TMonoid, TOperatorMonoid> monoidOperator;
private TOperatorMonoid?[] operators;
public OperatorLazySegmentTree(ILazySegmentTreeMonoidOperator<TMonoid, TOperatorMonoid> lazySegmentTreeMonoidOperator)
{
monoidOperator = lazySegmentTreeMonoidOperator;
}
public override void Build(int _n, Func<int, TMonoid> create)
{
base.Build(_n, create);
operators = new TOperatorMonoid?[2 * N];
}
protected override TMonoid Merge(TMonoid left, TMonoid right)
{
return monoidOperator.Merge(left, right);
}
protected void MergeOperator(ref TOperatorMonoid? left, TOperatorMonoid? right)
{
if (left.HasValue && right.HasValue) {
left = monoidOperator.MergeOperator(left.Value, right.Value);
return;
}
left = left ?? right;
}
protected void LazyPropagate(int k, int l, int r)
{
if (!operators[k].HasValue) return;
if (k < N - 1)
{
int childL = Left(k), childR = Right(k);
MergeOperator(ref operators[childL], operators[k]);
MergeOperator(ref operators[childR], operators[k]);
}
node[k] = monoidOperator.Apply(operators[k].Value, node[k].Value, r - l);
operators[k] = null;
}
protected override TMonoid? Query(int begin, int end, int k, int l, int r)
{
LazyPropagate(k, l, r);
return base.Query(begin, end, k, l, r);
}
private void LazyUpdate(
int begin, int end, TOperatorMonoid @operator, int k, int l, int r)
{
if (node[k] == null) return;
if (r <= begin || end <= l)
{
LazyPropagate(k, l, r);
return;
}
if (begin <= l && r <= end)
{
MergeOperator(ref operators[k], @operator);
LazyPropagate(k, l, r);
return;
}
LazyPropagate(k, l, r);
int mid = (l + r) / 2;
LazyUpdate(begin, end, @operator, Left(k), l, mid);
LazyUpdate(begin, end, @operator, Right(k), mid, r);
node[k] = Merge(node[Left(k)], node[Right(k)]);
}
public void LazyUpdate(int begin, int end, TOperatorMonoid @operator)
{
LazyUpdate(begin, end, @operator, 0, 0, N);
}
}
// PREWRITEN CODE BEGINS FROM HERE
partial class Solver : Scanner
{
public static void Main(string[] args)
{
#if LOCAL
new Solver(Console.In, Console.Out).Run();
#else
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
new Solver(Console.In, Console.Out).Run();
Console.Out.Flush();
#endif
}
#pragma warning disable IDE0052
private readonly TextReader cin;
private readonly TextWriter cout;
#pragma warning restore IDE0052
public Solver(TextReader reader, TextWriter writer)
: base(reader)
{
this.cin = reader;
this.cout = writer;
}
public Solver(string input, TextWriter writer)
: this(new StringReader(input), writer)
{
}
#pragma warning disable IDE1006
#pragma warning disable IDE0051
private int ni() { return NextInt(); }
private int[] ni(int n) { return NextIntArray(n); }
private long nl() { return NextLong(); }
private long[] nl(int n) { return NextLongArray(n); }
private double nd() { return NextDouble(); }
private double[] nd(int n) { return NextDoubleArray(n); }
private string ns() { return Next(); }
private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}
public class Scanner
{
private readonly TextReader Reader;
private readonly Queue<string> TokenQueue = new Queue<string>();
private readonly CultureInfo ci = CultureInfo.InvariantCulture;
public Scanner()
: this(Console.In)
{
}
public Scanner(TextReader reader)
{
this.Reader = reader;
}
public int NextInt() { return int.Parse(Next(), ci); }
public long NextLong() { return long.Parse(Next(), ci); }
public double NextDouble() { return double.Parse(Next(), ci); }
public string[] NextArray(int size)
{
var array = new string[size];
for (int i = 0; i < size; i++) array[i] = Next();
return array;
}
public int[] NextIntArray(int size)
{
var array = new int[size];
for (int i = 0; i < size; i++) array[i] = NextInt();
return array;
}
public long[] NextLongArray(int size)
{
var array = new long[size];
for (int i = 0; i < size; i++) array[i] = NextLong();
return array;
}
public double[] NextDoubleArray(int size)
{
var array = new double[size];
for (int i = 0; i < size; i++) array[i] = NextDouble();
return array;
}
public string Next()
{
if (TokenQueue.Count == 0)
{
if (!StockTokens()) throw new InvalidOperationException();
}
return TokenQueue.Dequeue();
}
public bool HasNext()
{
if (TokenQueue.Count > 0)
return true;
return StockTokens();
}
static readonly char[] _separator = new[] { ' ' };
private bool StockTokens()
{
while (true)
{
var line = Reader.ReadLine();
if (line == null) return false;
var tokens = line.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length == 0) continue;
foreach (var token in tokens)
TokenQueue.Enqueue(token);
return true;
}
}
}
EmKjp