杭电多校contest4 1005 Matrix from Arrays 容斥+前缀

 

中给出了样例解释More...

 

Problem E. Matrix from Arrays

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 761    Accepted Submission(s): 233

 

Problem Description

Kazari has an array A length of L, she plans to generate an infinite matrix M using A.
The procedure is given below in C/C++:

int cursor = 0;

for (int i = 0; ; ++i) {
    for (int j = 0; j <= i; ++j) { 
        M[j][i - j] = A[cursor];
        cursor = (cursor + 1) % L;
    }
}



Her friends don't believe that she has the ability to generate such a huge matrix, so they come up with a lot of queries about M, each of which focus the sum over some sub matrix. Kazari hates to spend time on these boring queries. She asks you, an excellent coder, to help her solve these queries.

 

 

Input

The first line of the input contains an integer T (1≤T≤100) denoting the number of test cases.
Each test case starts with an integer L (1≤L≤10) denoting the length of A.
The second line contains L integers A0,A1,...,AL−1 (1≤Ai≤100).
The third line contains an integer Q (1≤Q≤100) denoting the number of queries.
Each of next Q lines consists of four integers x0,y0,x1,y1 (0≤x0≤x1≤108,0≤y0≤y1≤108) querying the sum over the sub matrix whose upper-leftmost cell is (x0,y0) and lower-rightest cell is (x1,y1).

 

 

Output

For each test case, print an integer representing the sum over the specific sub matrix for each query.

 

 

Sample Input

1

3

1 10 100

5

3 3 3 3

2 3 3 3

2 3 5 8

5 1 10 10

9 99 999 1000

Sample Output

1

101

1068

2238

33076541

经过几次打表发现当L是奇数的时候M数组是L*L的循环,当为偶数2*L * 2*L的循环,因此考虑一下用容斥+前缀和来做

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b)   for(int i=a;i<=b;i++)
#define pre(i,a,b)  for(int i=a;i>=b;i--)
const int inf=1e9+7;
int t;
int L;
int M[105][105];
int prel[105][105];//保存前缀
int A[105];
int g[105];
int lenght;
int print(int l,int c)
{
    if(l==0||c==0)
    return 0;
    rep(i,1,lenght)
    g[i]=g[i-1]+prel[i][lenght]*(c/lenght)+prel[i][c%lenght];
    return g[lenght]*(l/lenght)+g[l%lenght];
}
int main()
{
//    ios::sync_with_stdio(0);
//    cin.tie(0);
    cin>>t;
    while(t--)
    {
      cin>>L;
      lenght=L*2;
      rep(i,0,L-1)
      cin>>A[i];
      int cursor = 0;
    for (int i = 0; i<=100; ++i) {
        for (int j = 0; j <= i; ++j) {
            M[j+1][i - j+1] = A[cursor];
            cursor = (cursor + 1) % L;
        }
    }
    rep(i,1,lenght)
    rep(j,1,lenght)
    prel[i][j]=M[i][j]+prel[i][j-1];
    int Q;
    cin>>Q;
    while(Q--)
    {
        int x0,y0,x1,y1;
        cin>>x0>>y0>>x1>>y1;
        x0++;
        y0++;
        x1++;
        y1++;
        ll ans1=print(x0-1,y0-1);
        ll ans2=print(x1,y1);
        ll ans3=print(x0-1,y1);
        ll ans4=print(x1,y0-1);
        //cout<<left<<setw(5)<<ans1<<setw(5)<<ans2<<setw(5)<<ans3<<setw(5)<<ans4<<setw(5)<<ans1+ans2-ans3-ans4;
        cout<<ans1+ans2-ans3-ans4;
        cout<<endl;
    }
    }

// rep(i,1,lenght)
//    {
//            rep(j,1,lenght)
//cout<<left<<setw(5)<<prel[i][j];
//cout<<endl;
//    } rep(i,1,lenght)
//    {
//            rep(j,1,lenght)
//cout<<left<<setw(5)<<M[i][j];
//cout<<endl;
//    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值