【Codeforces Round #427 (Div. 2) C】Star sky
给你n个星星的坐标(xi,yi);
第i个星星在第t秒,闪烁值变为(si+t)%(c+1);
给你q个询问,每个询问由时间t和一个矩形的左下角和右上角组成;
问这个矩形区域内的星星闪烁值的总和;
朴素的做法;
a[j][k][l]表示(j,k)这个点一开始闪烁值为l的星星有多少个;
for (int i = 1;i <= q;i++){
int temp = 0;
for (int j = 1;j <= 100;j++)
for (int k = 1;k <= 100;k++)
for (int l = 0;l <= c;l++)
temp += a[j][k][l]*((t[i]+l)%(c+1));
out(temp);
}
这里可以把两层1..100的for循环,用一个前缀和省掉.
0
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 100;
const int C = 10;
int a[N+10][N+10][C+10];
int n,q,c;
int getsum(int x1,int y1,int x2,int y2,int k){
int temp1 = a[x2][y2][k];
int temp2 = a[x2][y1-1][k];
int temp3 = a[x1-1][y2][k];
int temp4 = a[x1-1][y1-1][k];
return temp1-temp2-temp3+temp4;
}
main(){
scanf("%lld%lld%lld",&n,&q,&c);
for (int i = 1;i <= n;i++){
int x,y,s;
scanf("%lld%lld%lld",&x,&y,&s);
a[x][y][s]++;
}
for (int i = 1;i <= N;i++)
for (int j = 1;j <= N;j++)
for (int k = 0;k <= c;k++)
a[i][j][k] += a[i][j-1][k]+a[i-1][j][k]-a[i-1][j-1][k];
for (int i = 1;i <= q;i++){
int t,x1,y1,x2,y2;
scanf("%lld%lld%lld%lld%lld",&t,&x1,&y1,&x2,&y2);
int temp = 0;
for (int j = 0;j <= c;j++){
temp += getsum(x1,y1,x2,y2,j)*((j+t)%(c+1));
}
printf("%lld\n",temp);
}
}