#P13065. [GCJ 2020 #2] Emacs++

    ID: 12870 Type: RemoteJudge 60000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 7 Uploaded By: Tags>2020最短路最近公共祖先 LCAGoogle Code Jam

[GCJ 2020 #2] Emacs++

题目描述

In 2016's Distributed Code Jam, we introduced the Lisp++ language for Lisp fans who prefer a higher density of parentheses. Here is a reminder of how the language's syntax works:

A Lisp++ program is a string of balanced parentheses. More formally, a Lisp++ program consists of one of the following. (In this specification, C stands for some program code — not necessarily the same code each time.)

  • () Literally, just an opening parenthesis and a closing parenthesis. We say that this ( matches this ), and vice versa.
  • (C) A program within a pair of enclosing parentheses. We say that this ( matches this ), and vice versa.
  • CC Two programs (not necessarily the same), back to back.

This year, we are pleased to announce Emacs++, a text viewer for Lisp++. Emacs++ displays a Lisp++ program of length K as a single long line with a cursor that you can move around. The cursor is a "block cursor" that is always located on one of the K characters in the program, rather than between characters.

At any point, you can perform one of the following three actions to move the cursor. (i represents the current position of the cursor, counting starting from 1 for the leftmost position.)

  • Move the cursor one character to the left (or, if the cursor is already on the leftmost character, does nothing). This takes LiL_i seconds.
  • Move the cursor one character to the right (or, if the cursor is already on the rightmost character, does nothing). This takes RiR_i seconds.
  • Teleport the cursor to the parenthesis matching (as described above) the parenthesis that is the i-th character. This takes PiP_i seconds.

We think Emacs++ will be simple for power users, but we still need to understand how efficient it is. We have a single Lisp++ program and list of Q queries about that program; each query consists of a start position SjS_j and an end position EjE_j. To answer the j-th query, you must determine the smallest possible amount of time NjN_j (in seconds) that it will take to take the cursor from position SjS_j to position EjE_j, if you make optimal decisions.

Please output the sum of all of those NjN_j values.

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. The first line of a test case contains two integers KK, which is the length of the Lisp++ program, and QQ, which is the number of queries.

The second line of a test case contains a string PP of KK characters, each of which is either ( or ), representing a Lisp++ program (string of balanced parentheses), as described above.

The third, fourth, and fifth lines of a test case each contain KK integers. The ii-th integers in these lines are the values LiL_i, RiR_i, and PiP_i, respectively, that are described above.

The sixth and seventh lines of a test case each contain QQ integers. The jj-th integers in these lines are SjS_j and EjE_j, respectively, that are described above.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the sum of the NjN_j values that are described above.

1
12 5
(()(((()))))
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
7 4 4 12 5
12 11 10 1 6
Case #1: 10

提示

Sample Explanation

In the sample case, which obeys the limits for Test Set 1, all of the time costs are the same (11 second per move). The shortest times for the queries are as follows:

  1. Move right from 77 five times to 1212 taking 55 seconds.
  2. Teleport from 44 to 1111 taking 11 second.
  3. Teleport from 44 to 1111, then move left to 1010 taking 22 seconds.
  4. Teleport from 1212 to 11, taking 11 second.
  5. Move right from 55 to 66 taking 11 second.

Thus, the sum of query times is 5+1+2+1+1=105+1+2+1+1 = 10 seconds.

Limits

  • 1T1001 \leq T \leq 100.
  • K=105K = 10^5 and Q=105Q = 10^5, for at most 9 test cases.
  • 2K10002 \leq K \leq 1000 and 1Q10001 \leq Q \leq 1000, in all other cases.
  • length of P=KP = K PP is a string of balanced parentheses, as described above.
  • 1SjK1 \leq S_j \leq K, for all jj.
  • 1EjK1 \leq E_j \leq K, for all jj.

Test Set 1 (12 Pts, Visible Verdict)

  • Li=1L_i = 1, for all ii.
  • Ri=1R_i = 1, for all ii.
  • Pi=1P_i = 1, for all ii.

Test Set 2 (23 Pts, Hidden Verdict)

  • 1Li1061 \leq L_i \leq 10^6, for all ii.
  • 1Ri1061 \leq R_i \leq 10^6, for all ii.
  • 1Pi1061 \leq P_i \leq 10^6, for all ii.