Line-Based Matrix Addition

시간 제한2초메모리 제한1024 MB

요약
상승 대각선 구간과 하강 대각선 구간을 하나씩 골라 두 구간의 교집합에 속한 모든 칸에 값을 더하고, 최종 행렬을 출력한다.
난이도

보통10점 중 7점

유형
누적 합, 행렬, 구현, 수학
정답자
아직 제출이 없습니다

문제

You are given an N×NN \times N matrix AA.

Two types of lines exist on the matrix: rising and falling. There are 2N−12N-1 lines for each type, where rising lines are numbered RL_i\mathrm{RL}\_i and falling lines are numbered FL_i\mathrm{FL}\_i for each 1≤i≤2N−11 \le i \le 2N-1. The below picture illustrates the line layout when N=5N=5.

You have to perform QQ cumulative queries in order on this matrix, which is given by the input as:

  • s_Rs\_\mathrm{R} e_Re\_\mathrm{R} s_Fs\_\mathrm{F} e_Fe\_\mathrm{F} vv.

Each query specifies two line ranges of different types on the matrix, which is \left\\{ \mathrm{RL}\_i \mid s\_\mathrm{R} \le i \le e\_\mathrm{R} \right\\} and \left\\{ \mathrm{FL}\_i \mid s\_\mathrm{F} \le i \le e\_\mathrm{F} \right\\}, and a value vv. You should add the value vv to every element in the intersection of such range, i.e., \left\\{ \mathrm{RL}\_i \mid s\_\mathrm{R} \le i \le e\_\mathrm{R} \right\\} \quad \cap \quad \left\\{ \mathrm{FL}\_i \mid s\_\mathrm{F} \le i \le e\_\mathrm{F} \right\\}.

The following example illustrates the elements to update when N=5N=5, \[s_R,e_R]=\[5,6]\[s\_\mathrm{R},e\_\mathrm{R}]=\[5,6], and \[s_F,e_F]=\[3,7]\[s\_\mathrm{F},e\_\mathrm{F}]=\[3,7]:

Write a program to perform the queries and output the final status of the matrix AA.

입력

The first line of input contains a single integer, NN, denoting the matrix size. (1≤N≤1,0001 \le N \le 1\\,000)

The next NN lines of input contain N2N^2 integers, where each line has NN space-separated integers, denoting the value of the matrix. Here, the jj-th integer of the ii-th line denotes A_ijA\_{ij}. (−109≤A_ij≤109-10^9 \le A\_{ij} \le 10^9)

The next line contains a single integer, QQ, denoting the query count. (1≤Q≤200,0001 \le Q \le 200\\,000)

The ii-th of the next QQ lines of input contain five space-separated integers: s_Rs\_\mathrm{R}, e_Re\_\mathrm{R}, s_Fs\_\mathrm{F}, e_Fe\_\mathrm{F}, and vv, denoting the ii-th query explained earlier. (1≤s_R≤e_R≤2N−1;1 \le s\_\mathrm{R} \le e\_\mathrm{R} \le 2N-1; 1≤s_F≤e_F≤2N−1;1 \le s\_\mathrm{F} \le e\_\mathrm{F} \le 2N-1; −109≤v≤109-10^9 \le v \le 10^9)

출력

Output NN lines denoting the updated value of the matrix AA. Each line should contain NN space-separated integers. The jj-th integer of the ii-th line should represent A_ijA\_{ij}.

힌트

(For Sogang students:) Note that this problem is an improvised version that matches the format of a problem in a general programming contest. While in the exam, the original scoring was:

  • 4040 points for Subtask 1.
  • 3030 points for Subtask 2.
  • 2525 points for Subtask 3.
  • 55 points for Subtask 4.

예제1

  1. 예제 1

    입력
    5
    110 120 130 140 150
    210 220 230 240 250
    310 320 330 340 350
    410 420 430 440 450
    510 520 530 540 550
    2
    5 6 3 7 15
    2 3 4 5 24
    
    예상 출력
    110 144 130 140 150
    210 244 230 255 250
    310 320 345 355 350
    410 435 445 440 450
    510 520 530 540 550