Brush title notes - and the prefix

Prefix and is quick to strike a static array a method within a certain range and the number of all. If a change occurs in the process of ascertaining the array, then Fenwick tree, tree line and other methods can be used.

Examples 1 and one-dimensional prefix

Enter a sequence of length n is an integer.
Next, and then the m input query, each pair of input query l, r.
For each query, the output from the original sequence number to the number of l and r.
Input format
The first line contains two integers n and m.
The second line contains n integer representing the number of an integer column.
Subsequently m rows, each row comprising two integers l and r, representing the interval range of a query.
Output format
co m rows, each row outputs a result of the inquiry.

data range

\ (\ 1≤l≤r≤n)
\ (1 ≦ n, m≤100000 \)
\ (- value of the number of elements in 1000≤ ≤1000 \)

#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int n,m;
int s[N];
int a[N];
int main(void)
{
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++)
    {
        scanf("%d",&a[i]);
        s[i] = s[i-1] + a[i];
    } 
    while(m--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",s[r] - s[l-1]);
    }
    return 0;
    
}

Example 2 dimensional prefix and

Enter an integer matrix of n rows and m columns, then enter a query q, each challenge comprising four integers x1, y1, x2, y2, upper-left coordinates and lower right coordinates represents a sub-matrices.
For each sub-query output matrix and all the numbers.
Input format
The first line contains three integers n, m, q.
Next n lines, each line contains integers m represents an integer matrix.
Next q rows, each row comprising four integers x1, y1, x2, y2, represents a group of query.
Output format
co q rows, each row outputs a result of the inquiry.
Data range
\ (1 ≦ n, m≤1000 \)
\ (1≤q≤200000 \)
(\ 1≤x1≤x2≤n) \
\ (- 1000≤ matrix element values within ≤1000 \)

#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1010;
int a[N][N];
int s[N][N];
int n,m,q;
int main(void)
{
    scanf("%d%d%d",&n,&m,&q);
    for(int i = 1; i <= n;i++)
        for(int j = 1; j <= m;j++)
        {
            scanf("%d",&a[i][j]);
            s[i][j] = s[i -1][j] + s[i][j-1] - s[i-1][j-1] + a[i][j];
        }
    while(q--)
    {
        int x1,x2,y1,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        printf("%d\n",s[x2][y2] - s[x2][y1-1] - s[x1-1][y2] + s[x1-1][y1-1]);
    }
    return 0;
    
            

}

Exercise 1 K interval times

Given a length of the number of columns N, wherein if a contiguous subsequence is a multiple of the sum of K, we call this interval [i, j] is K times the interval.
You can find the number of columns in the total number of K times the interval it?

data range

\(1≤N,K≤100000\)
\(1≤Ai≤100000\)

Ideas:

  1. By double loop start and end positions of the control sequence, each traversal request of each subsequence and S [r] - S [l -1]% k is equal to 0, the time complexity is \ (O (n ^ 2) \) , about \ (10 ^ 9 \) of the order, it is necessary to consider lowering the time complexity.
    2. The general manner is to reduce the complexity of the time space for time .
  2. S [r] - S [l -1]% k == 0 is equivalent to s [r]% k == s [l-1]% k, it is possible to declare an array used to store a remainder of s [i] % k of the number, so that only one is circulated.
    4. Note that data overflow

Code:

#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010;
int n,k;
LL s[N],cnt[N];
int main(void)
{
    scanf("%d %d",&n,&k);
    //计算前缀和
    for(int i = 1; i <= n;i++){
        scanf("%lld",&s[i]);     //注意LL的读取
        s[i] += s[i-1];
    }
    LL ans = 0;                 //注意答案可能溢出
    for(int i = 0;i <= n;i++)   //考虑和为0也也是k倍区间,所以从0开始遍历
    {
        //假设已经有两个S[i]的余数为x,则又有一个S[i]%k == x时,会产生两个新的答案,所以有下面两句
        ans += cnt[s[i] % k];
        cnt[s[i] % k]++;
    }
    printf("%lld",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zy200128/p/12616856.html