解析:
询问矩形内点的值的和,二维前缀和,注意ll
ac:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll mp[3000][3000];
ll dp[3000][3000];
ll w;
int main()
{
memset(dp,0,sizeof(dp));
memset(mp,0,sizeof(mp));
int n,m,a,b,c,d;
int x,y;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d%lld",&x,&y,&w);
x=x+1;
y=y+1;
mp[x][y]+=w;
}
for(int i=1;i<=2001;i++)
for(int j=1;j<=2001;j++)
dp[i][j]=mp[i][j]-dp[i-1][j-1]+dp[i][j-1]+dp[i-1][j];
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
a=a+1,b=b+1,c=c+1,d=d+1;
cout<<dp[c][d]-dp[c][b-1]-(dp[a-1][d]-dp[a-1][b-1])<<endl;
}
return 0;
}
Monitor
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others)
Total Submission(s): 914 Accepted Submission(s): 292
Problem Description
Xiaoteng has a large area of land for growing crops, and the land can be seen as a rectangle of n×m.
But recently Xiaoteng found that his crops were often stolen by a group of people, so he decided to install some monitors to find all the people and then negotiate with them.
However, Xiao Teng