首页 > 其他分享 >Hard Demon Problem

Hard Demon Problem

时间:2024-12-19 09:46:02浏览次数:7  
标签:limits cdot sum Hard Demon leq ddot Problem dot

Hard Demon Problem

Swing is opening a pancake factory! A good pancake factory must be good at flattening things, so Swing is going to test his new equipment on 2D matrices.

Swing is given an $n \times n$ matrix $M$ containing positive integers. He has $q$ queries to ask you.

For each query, he gives you four integers $x_1$, $y_1$, $x_2$, $y_2$ and asks you to flatten the submatrix bounded by $(x_1, y_1)$ and $(x_2, y_2)$ into an array $A$. Formally, $A = [M_{(x1,y1)}, M_{(x1,y1+1)}, \ldots, M_{(x1,y2)}, M_{(x1+1,y1)}, M_{(x1+1,y1+1)}, \ldots, M_{(x2,y2)}]$.

The following image depicts the flattening of a submatrix bounded by the red dotted lines. The orange arrows denote the direction that the elements of the submatrix are appended to the back of $A$, and $A$ is shown at the bottom of the image.

Afterwards, he asks you for the value of $\sum_{i=1}^{|A|} A_i \cdot i$ (sum of $A_i \cdot i$ over all $i$).

Input

The first line contains an integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases.

The first line of each test contains two integers $n$ and $q$ ($1 \leq n \leq 2000, 1 \leq q \leq 10^6$) — the length of $M$ and the number of queries.

The following $n$ lines contain $n$ integers each, the $i$'th of which contains $M_{(i,1)}, M_{(i,2)}, \ldots, M_{(i,n)}$ ($1 \leq M_{(i, j)} \leq 10^6$).

The following $q$ lines contain four integers $x_1$, $y_1$, $x_2$, and $y_2$ ($1 \leq x_1 \leq x_2 \leq n, 1 \leq y_1 \leq y_2 \leq n$) — the bounds of the query.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ and the sum of $q$ over all test cases does not exceed $10^6$.

Output

For each test case, output the results of the $q$ queries on a new line.

Example

Input

2
4 3
1 5 2 4
4 9 5 3
4 5 2 3
1 5 5 2
1 1 4 4
2 2 3 3
1 2 4 3
3 3
1 2 3
4 5 6
7 8 9
1 1 1 3
1 3 3 3
2 2 2 2

Output

500 42 168 
14 42 5 

Note

In the second query of the first test case, $A = [9, 5, 5, 2]$. Therefore, the sum is $1 \cdot 9 + 2 \cdot 5 + 3 \cdot 5 + 4 \cdot 2 = 42$.

 

解题思路

  纯粹就是把累加转换成前缀和的表示形式然后推式子,关键是需要维护前缀和很多,且式子很复杂易推错。

  先从简单的情况考虑,假设询问的是某一行,即 $x_1 = x_2 = i$,此时的结果为
$$
\begin{align*}
&\sum\limits_{j=y_1}^{y_2}{(j-y_1+1)a_{i,j}} \\
=& \, (1-y_1)\sum\limits_{j=y_1}^{y_2}{a_{i,j}} + \sum\limits_{j=y_1}^{y_2}{j \cdot a_{i,j}}
\end{align*}
$$

  记 $\dot{r}_{i,j} = \sum\limits_{k=1}^{j}{a_{i,k}}$,$\ddot{r}_{i,j} = \sum\limits_{k=1}^{j}{k \cdot a_{i,k}}$,则有
$$
\begin{align*}
& \, (1-y_1)\sum\limits_{j=y_1}^{y_2}{a_{i,j}} + \sum\limits_{j=y_1}^{y_2}{j \cdot a_{i,j}} \\
=& \, (1-y_1)(\dot{r}_{i,y_2} - \dot{r}_{i,y_1-1}) + \ddot{r}_{i,y_2} - \ddot{r}_{i,y_1-1}
\end{align*}
$$

  现在考虑询问多行的情况,对于每一行,其实只需在上述结果的基础上再加上 $(i-x_1)(y_2-y_1+1)\sum\limits_{j=y_1}^{y_2}{a_{i,j}}$,以保证该行每个元素乘以的系数是正确的。

$$
\begin{align*}
&\sum\limits_{i=x_1}^{x_2} (1-y_1)(\dot{r}_{i,y_2} - \dot{r}_{i,y_1-1}) + \ddot{r}_{i,y_2} - \ddot{r}_{i,y_1-1} + (i-x_1)(y_2-y_1+1)\sum\limits_{j=y_1}^{y_2}{a_{i,j}} \\
=& \sum\limits_{i=x_1}^{x_2} (1-y_1)(\dot{r}_{i,y_2} - \dot{r}_{i,y_1-1}) + \ddot{r}_{i,y_2} - \ddot{r}_{i,y_1-1} + (i-x_1)(y_2-y_1+1)(\dot{r}_{i,y_2} - \dot{r}_{i,y_1-1}) \\
=& \, (y_2-y_1+1)\sum\limits_{i=x_1}^{x_2}{i \cdot \dot{r}_{i,y_2} - i \cdot \dot{r}_{i,y_1-1}} \\ &+(1-y_1-x_1(y_2-y_1+1))\sum\limits_{i=x_1}^{x_2}{\dot{r}_{i,y_2} - \dot{r}_{i,y_1-1}} \\ &+ \sum\limits_{i=x_1}^{x_2}{\ddot{r}_{i,y_2} - \ddot{r}_{i,y_1-1}}
\end{align*}
$$

  记 $\dot{c}_{i,j} = \sum\limits_{k=1}^{i}{\dot{r}_{k,j}}$,$\ddot{c}_{i,j} = \sum\limits_{k=1}^{i}{\ddot{r}_{k,j}}$,$\dddot{c}_{i,j} = \sum\limits_{k=1}^{i}{k \cdot\dot{r}_{k,j}}$,则有
$$
\begin{align*}
& \, (y_2-y_1+1)\sum\limits_{i=x_1}^{x_2}{i \cdot \dot{r}_{i,y_2} - i \cdot \dot{r}_{i,y_1-1}} \\ &+ (1-y_1-x_1(y_2-y_1+1))\sum\limits_{i=x_1}^{x_2}{\dot{r}_{i,y_2} - \dot{r}_{i,y_1-1}} \\ &+ \sum\limits_{i=x_1}^{x_2}{\ddot{r}_{i,y_2} - \ddot{r}_{i,y_1-1}} \\
=& \, (y_2-y_1+1)(\dddot{c}_{x_2,y_2} - \dddot{c}_{x_1-1,y_2} - (\dddot{c}_{x_2,y_1-1} - \dddot{c}_{x_1-1,y_1-1})) \\ &+ (1-y_1-x_1(y_2-y_1+1))(\dot{c}_{x_2,y_2} - \dot{c}_{x_1-1,y_2} - (\dot{c}_{x_2,y_1-1} - \dot{c}_{x_1-1,y_1-1})) \\ &+ \ddot{c}_{x_2,y_2} - \ddot{c}_{x_1-1,y_2} - (\ddot{c}_{x_2,y_1-1} - \ddot{c}_{x_1-1,y_1-1})
\end{align*}
$$
  记 $f(c) = c_{x_2,y_2} - c_{x_1-1,y_2} - (c_{x_2,y_1-1} - c_{x_1-1,y_1-1})$,则有
$$
\begin{align*}
& \, (y_2-y_1+1)(\dddot{c}_{x_2,y_2} - \dddot{c}_{x_1-1,y_2} - (\dddot{c}_{x_2,y_1-1} - \dddot{c}_{x_1-1,y_1})) \\ &+ (1-y_1-x_1(y_2-y_1+1))(\dot{c}_{x_2,y_2} - \dot{c}_{x_1-1,y_2} - (\dot{c}_{x_2,y_1-1} - \dot{c}_{x_1-1,y_1})) \\ &+ \ddot{c}_{x_2,y_2} - \ddot{c}_{x_1-1,y_2} - (\ddot{c}_{x_2,y_1-1} - \ddot{c}_{x_1-1,y_1}) \\
=& \, (y_2-y_1+1)f(\dddot{c}) + (1-y_1-x_1(y_2-y_1+1))f(\dot{c}) + f(\ddot{c})
\end{align*}
$$
  AC 代码如下,时间复杂度为 $O(n^2 + q)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2005;

LL r1[N][N], r2[N][N], c1[N][N], c2[N][N], c3[N][N];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            int x;
            cin >> x;
            r1[i][j] = r1[i][j - 1] + x;
            r2[i][j] = r2[i][j - 1] + j * x;
        }
    }
    for (int j = 1; j <= n; j++) {
        for (int i = 1; i <= n; i++) {
            c1[i][j] = c1[i - 1][j] + r1[i][j];
            c2[i][j] = c2[i - 1][j] + r2[i][j];
            c3[i][j] = c3[i - 1][j] + i * r1[i][j];
        }
    }
    while (m--) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        auto f = [&](LL c[][N]) {
            return c[x2][y2] - c[x1 - 1][y2] - c[x2][y1 - 1] + c[x1 - 1][y1 - 1];
        };
        cout << (y2 - y1 + 1) * f(c3) + (1 - y1 - x1 * (y2 - y1 + 1)) * f(c1) + f(c2) << ' ';
    }
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round 993 (Div. 4) Editorial:https://codeforces.com/blog/entry/137306

标签:limits,cdot,sum,Hard,Demon,leq,ddot,Problem,dot
From: https://www.cnblogs.com/onlyblues/p/18616401

相关文章

  • 【内向基环树】codeforces 2044 G1. Medium Demon Problem (easy version)
    前言基环树,又名环套树,是具有\(n\)个节点和\(n\)条边的图,比树多出现一个环。基环树也根据边的有向和无向分为了有向基环树和无向基环树。有向基环树又可以分为内向基环树和外向基环树。对于有向基环树,若基环树的每个节点的出度均为\(1\),则称为内向基环树;若基环树的每个节点的......
  • (补题)Codeforces Round 993 (Div. 4) E.Insane Problem
    显然不可暴力解出,因此是到数学题。已知$$y=x*k^n$$所以我们可以利用y的区间范围$$[l1,r1]$$得出x的新的区间范围$$[l2/k^n(向上取整),r2/k^n(向下取整)]$$接着与原来的范围取交集然后不断枚举n即可,注意k^n不可能超过y#include<iostream>#defineintlonglongusingnamespa......
  • 【Z函数】codeforces 2010 C2. Message Transmission Error (hard version)
    前言Z函数的定义对于一个字符串\(s\),定义Z函数\(Z[i]\)为以\(s[i]\)为起始位置的后缀与整个字符串\(s\)的最长公共前缀的长度。Z函数的应用字符串匹配问题题目https://codeforces.com/problemset/problem/2010/C2题意给定一个字符串\(s\),若其可以找到真前缀......
  • Insane Problem(思维)
    Waveisgivenfiveintegers\(k\),\(l_1\),\(r_1\),\(l_2\),and\(r_2\).Wavewantsyoutohelphercountthenumberoforderedpairs\((x,y)\)suchthatallofthefollowingaresatisfied:\(l_1\leqx\leqr_1\).\(l_2\leqy\leq......
  • STM32卡死、跑飞、进入HardFault_Handler如何精准的确定问题
    目录前言一、程序跑飞原因软件原因1.堆栈溢出2.指针操作错误3.中断优先级配置错误4.外设错误配置5.存储器管理问题6.代码逻辑错误7.时钟配置错误8.Watchdog超时硬件原因1.电气问题2.硬件故障软件复位与硬件问题结合二、调试工具如何进入仿真?进入......
  • Problem: 1338. 数组大小减半 贪心 模拟 法 简单易懂
    Problem:1338.数组大小减半思路因为要选择最小的整数集合,这里用Counter容器来统计下所有各种数字的大小,然后按照值来排序,设置target来表示要到达什么位置,这里最好不要用整除,防止要计算的大于arr,但是len(arr)是奇数,这里total表示删除到这个位置已经删除了多少数字,如果大......
  • P10370 「LAOI-4」Mex Tower (Hard ver.) 题解
    有一定难度的思维题。题目传送门思路首先,\(\operatorname{mex}(x,y)\)的结果一定为\(0,1,2\),因为只有两个数,所以结果最多为\(2\)(\(x=1,y=0\)或\(x=0,y=1\)时)。因此,可以将问题转化为最后的数是否为\(2\)。考虑倒推,当\(n=1\)时,显然只能为\(2\);要从\(n=2\)的情况变为......
  • Mod segma problem
    https://atcoder.jp/contests/abc378/tasks/abc378_e#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong#definelowbit(x)(x&(-x))#definepiipair<int,int>#definemkpmake_pairconstintN=2e5+10,mod=998244353;i......
  • Google Kickstart2022 Round G Problem C 快乐子数组
    有点思路,但还需要细想思路一眼上去,应该是写单调队列,但是不是像写滑动窗口一样写设前缀和为pre,如果一个区间\([l,r]\)满足条件,那么\(pre[l-1]<min(pre[l],pre[l+1],.....,pre[r]\)根据这一点,我们每次枚举到i,只需要统计左端有多少个相对应的j使得pre[j]<pre[i]即可,这时就可以......
  • 蓝易云 - sharding-jdbc分库连接数优化教程
    在使用Sharding-JDBC进行分库分表时,优化连接数是一个重要的考虑因素。下面是一个关于如何优化Sharding-JDBC分库连接数的简单教程。配置连接池参数:在Sharding-JDBC的数据源配置中,我们可以设置连接池相关的参数来优化连接数。以下是一些常见的连接池参数:minPoolSize:连接池中......