Xianyujunzhu @ 2024-03-30 19:15:09
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,a[105][105];
cin>>n>>m;
for(int i=1;i<=m;i++){
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
for(int j=x1;j<=x2;j++){
for(int k=y1;k<=y2;k++){
a[j][k]++;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;i++){
if(j==n)
cout<<a[i][j];
else
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
by CTGU_23_sxy @ 2024-03-30 19:17:46
数组开小了,要开到 1005 @Xianyujunzhu
by Xianyujunzhu @ 2024-03-30 19:30:39
@CTGU_23_sxy 还是不行啊
by CTGU_23_sxy @ 2024-03-30 20:27:23
@Xianyujunzhu 会TLE,这要用到二维前缀和的知识
by v11v11blabla @ 2024-04-15 20:24:56
@Xianyujunzhu 见第16行
i++应该为j++
by v11v11blabla @ 2024-04-15 20:26:50
欸您好像过了对不起眼瞎
by wangderui111 @ 2024-05-30 21:00:35
#include<bits/stdc++.h>
using namespace std;
int ans[1005][1005];
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
for(int x=x1;x<=x2;x++){
for(int y=y1;y<=y2;y++){
ans[x][y]++;
}
}
}for(int x=1;x<=n;x++){
for(int y=1;y<=n;y++){
cout<<ans[x][y]<<" ";
}cout<<endl;
}return 0;
}
来自萌新的疑问,为什么要用差分
O(mnn)才1000000000(最坏)
by lihaoran2012 @ 2024-07-30 09:58:24
叮~您的代码已改好,请注意签收!
AC代码:```
using namespace std; int a[1001][1001]; int main(){ int n,m; cin>>n>>m; for(int i=1;i<=m;i++){ int x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2; for(int j=x1;j<=x2;j++){ for(int k=y1;k<=y2;k++){ a[j][k]++; } } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(j==n) cout<<a[i][j]; else cout<<a[i][j]<<" "; } cout<<endl; } return 0; }
by lihaoran2012 @ 2024-07-30 09:59:28
对不起格式写错了
by lihaoran2012 @ 2024-07-30 09:59:47
#include<bits/stdc++.h>
using namespace std;
int a[1001][1001];
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++){
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
for(int j=x1;j<=x2;j++){
for(int k=y1;k<=y2;k++){
a[j][k]++;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(j==n)
cout<<a[i][j];
else
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
by lihaoran2012 @ 2024-07-30 10:04:28
数组最好在外面开,而且还要开大一点。然后就能解决超时的问题。要把你第五个循环的i++改成j++就能AC了。