您的位置:首页 > 其它

【前缀和】hdu 5480 Conturbatio

2015-09-29 21:11 417 查看
hdu 5480 Conturbatio

http://acm.hdu.edu.cn/showproblem.php?pid=5480

问题描述:前缀和

前缀和很好理解:

s[1] = a[0]

s[2] = a[0] + a[1]

s[3] = a[0] + a[1] + a[2]

s
= a[0] + a[1] + a[2] + a[3] +…… + a[n-1]

若求任一序列和a[i]+……+a[j] = s[j] - s[i-1]

思路

a[i]表示第i行是否被占用,row[i]是行a[]的前缀和;同样的。另一列前缀和为col[i],如果从第i到j行都被占用,那么行前缀和要等于[i,j]总的行个数,列也是这样

参考代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
#include<set>
#include<sstream>
using namespace std;

typedef long long ll;

const int _max = 1e5 + 10;

int n,m,k,Q,row[_max],col[_max];
int x,y;

int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
int T;cin>>T;
while(T--){
fill(row,row+_max,0);
fill(col,col+_max,0);
scanf("%d%d%d%d",&n,&m,&k,&Q);
while(k--){
scanf("%d%d",&x,&y);
row[x] = 1;
col[y] = 1;
}
for(int i = 2; i <= n; ++ i)
row[i] += row[i-1];
for(int j = 2; j<= m; ++ j)
col[j] += col[j-1];
int x1,x2,y1,y2;
for(int i = 0; i < Q; ++ i){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(row[x2]-row[x1-1] == x2 - x1 + 1||col[y2]-col[y1-1]==y2-y1+1) puts("Yes");
else puts("No");
}
}
return 0;
}


加粗
Ctrl + B


斜体
Ctrl + I


引用
Ctrl + Q


插入链接
Ctrl + L


插入代码
Ctrl + K


插入图片
Ctrl + G


提升标题
Ctrl + H


有序列表
Ctrl + O


无序列表
Ctrl + U


横线
Ctrl + R


撤销
Ctrl + Z


重做
Ctrl + Y
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  前缀和 bestcoder