Submission #3911420


Source Code Expand

Copy
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static System.Console;
class Z { static void Main() => new K(); }
abstract class Node
{
	public long Diff, Value;
	public abstract void GetDiff(long diff);
}
class VarNode : Node
{
	public int Id;
	public VarNode(int id, long val) { Id = id; Value = val; }
	public override void GetDiff(long diff) { Diff = diff; }
}
class OpNode : Node
{
	public char Op;
	public Node Left, Right;
	public OpNode(Node l, char o, Node r)
	{
		Left = l; Op = o; Right = r;
		switch (Op)
		{
			case '+': Value = (Left.Value + Right.Value) % K.MOD; break;
			case '*': Value = Left.Value * Right.Value % K.MOD; break;
			default: Value = (Left.Value - Right.Value + K.MOD) % K.MOD; break;
		}
	}
	public override void GetDiff(long diff)
	{
		Diff = diff;
		switch (Op)
		{
			case '+': Left.GetDiff(diff); Right.GetDiff(diff); break;
			case '*': Left.GetDiff(Right.Value * diff % K.MOD); Right.GetDiff(Left.Value * diff % K.MOD); break;
			default: Left.GetDiff(diff); Right.GetDiff((K.MOD - diff) % K.MOD); break;
		}
	}
}
class K
{
	int[] G => ReadLine().Split().Select(int.Parse).ToArray();
	public const int MOD = 1000000007;
	public K()
	{
		SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
		var S = ReadLine();
		var Q = G[0];
		var leaves = new List<Node>();
		var e = Parse(S, G, leaves);
		e.GetDiff(1);
		for (var i = 0; i < Q; i++)
		{
			var q = G;
			int b = q[0] - 1, x = q[1];
			WriteLine((e.Value + (x - leaves[b].Value + MOD) * leaves[b].Diff) % MOD);
		}
		Out.Flush();
	}
	Node Parse(string S, int[] a, List<Node> leaves)
	{
		var index = 0;
		var Id = 0;
		var N = S.Length;
		var st = new Stack<object>();
		object ret = null;
		st.Push(new E0());
		while (st.Count > 0)
			switch (st.Pop())
			{
				case E0 e0: { st.Push(new E1()); st.Push(new T0()); break; }
				case E1 e1: { var n = (Node)ret; st.Push(new E2(n)); break; }
				case E2 e2:
					{
						if (index < N && (S[index] == '+' || S[index] == '-'))
						{
							var op = S[index];
							index++;
							st.Push(new E3(e2.N, op));
							st.Push(new T0());
						}
						else ret = e2.N;
						break;
					}
				case E3 e3:
					{
						var m = (Node)ret;
						var n = new OpNode(e3.N, e3.Op, m);
						st.Push(new E2(n));
						break;
					}
				case T0 t0: { st.Push(new T1()); st.Push(new V0()); break; }
				case T1 t1: { var n = (Node)ret; st.Push(new T2(n)); break; }
				case T2 t2:
					{
						if (index < N && S[index] == '*')
						{
							var op = S[index];
							index++;
							st.Push(new T3(t2.N, op));
							st.Push(new V0());
						}
						else ret = t2.N;
						break;
					}
				case T3 t3:
					{
						var m = (Node)ret;
						var n = new OpNode(t3.N, t3.Op, m);
						st.Push(new T2(n));
						break;
					}
				case V0 v0:
					{
						if (S[index] == '(')
						{
							index++;
							st.Push(new V1());
							st.Push(new E0());
						}
						else
						{
							var x = new VarNode(Id, a[Id++]);
							leaves.Add(x);
							index++;
							ret = x;
						}
						break;
					}
				case V1 v1: { index++; break; }
				default: throw new System.Exception();
		}
		return (Node)ret;
	}
}
struct E0 { }
struct E1 { }
struct E2 { public Node N; public E2(Node n) { N = n; } }
struct E3 { public Node N; public char Op; public E3(Node n, char op) { N = n; Op = op; } }
struct T0 { }
struct T1 { }
struct T2 { public Node N; public T2(Node n) { N = n; } }
struct T3 { public Node N; public char Op; public T3(Node n, char op) { N = n; Op = op; } }
struct V0 { }
struct V1 { }

Submission Info

Submission Time
Task E - 数式とクエリ
User selpo
Language C# (Mono 4.6.2.0)
Score 0
Code Size 3717 Byte
Status

Compile Error

./Main.cs(73,12): error CS1525: Unexpected symbol `e0'
./Main.cs(74,4): error CS1525: Unexpected symbol `case'
./Main.cs(74,14): error CS1525: Unexpected symbol `:'
./Main.cs(75,8): error CS1519: Unexpected symbol `case' in class, struct, or interface member declaration
./Main.cs(75,15): error CS1519: Unexpected symbol `:' in class, struct, or interface member declaration
./Main.cs(76,6): error CS9010: Primary constructor body is not allowed
./Main.cs(87,8): error CS1519: Unexpected symbol `case' in class, struct, or interface member declaration
./Main.cs(87,15): error CS1519: Unexpected symbol `:' in class, struct, or interface member declaration
./Main.cs(88,6): error CS9010: Primary constructor body is not allowed
./Main.cs(88,6): error CS8041: Primary constructor already has a body
./Main.cs(94,8): error CS1519: Unexpected symbol `case' in class, struct, or interface member declaration
./Main.cs(94,15): error CS1519: Unexpected symbol `:' in class, struct, or interface member declaration
./Main.cs(94,17):...