For this question certainly think of a prefix and, but there is a problem that we find every color of the stars not necessarily the same, so one more variable control is not good
So we expect the state dp f [k] [i] [j] represents the energy star as a prefix of k and, thus easy to maintain. Because we find that the energy of the stars does not exceed 10
#include<iostream> #include<cstring> #include<cstdio> #include<map> #include<algorithm> #define ull unsigned long long using namespace std; typedef long long ll; const int N=1e5+10; int f[12][110][110]; int main(){ int n; cin>>n; int q,c; cin>>q>>c; int k,i,j; for(i=1;i<=n;i++){ int x,y,s; scanf("%d%d%d",&x,&y,&s); f[s][x][y]++; } for(k=0;k<=c;k++){ for(i=1;i<=110;i++){ for(j=1;j<=110;j++){ f[k][i][j]+=(f[k][i-1][j]+f[k][i][j-1]-f[k][i-1][j-1]); } } } ll ans=0; while(q--){ ans=0; int t,x1,y1,x2,y2; scanf("%d%d%d%d%d",&t,&x1,&y1,&x2,&y2); ll res=0; for(i=0;i<=c;i++){ res=f[i][x2][y2]-f[i][x1-1][y2]-f[i][x2][y1-1]+f[i][x1-1][y1-1]; ans+=res*((i+t)%(c+1)); } cout<<ans<<endl; } }