【前缀和】hdu 5480 Conturbatio

本文介绍了解决HDU5480问题的方法,该问题涉及使用前缀和来判断矩形区域是否被完全占据。通过计算行和列的前缀和,并对比指定区域内行数或列数,可以快速得出结论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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[n] = 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值