【CodeForces - 869E】 The Untended Antiquity 【二维树状数组 + 坐标hash】

Adieu l’ami.

Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino’s makeshift residence.

The space is represented by a rectangular grid of n × m cells, arranged into n rows and m columns. The c-th cell in the r-th row is denoted by (r, c).

Oshino places and removes barriers around rectangular areas of cells. Specifically, an action denoted by “1 r1 c1 r2 c2” means Oshino’s placing barriers around a rectangle with two corners being (r1, c1) and (r2, c2) and sides parallel to squares sides. Similarly, “2 r1 c1 r2 c2” means Oshino’s removing barriers around the rectangle. Oshino ensures that no barriers staying on the ground share any common points, nor do they intersect with boundaries of the n × m area.

Sometimes Koyomi tries to walk from one cell to another carefully without striding over barriers, in order to avoid damaging various items on the ground. “3 r1 c1 r2 c2” means that Koyomi tries to walk from (r1, c1) to (r2, c2) without crossing barriers.

And you’re here to tell Koyomi the feasibility of each of his attempts.

Input
The first line of input contains three space-separated integers n, m and q (1 ≤ n, m ≤ 2 500, 1 ≤ q ≤ 100 000) — the number of rows and columns in the grid, and the total number of Oshino and Koyomi’s actions, respectively.

The following q lines each describes an action, containing five space-separated integers t, r1, c1, r2, c2 (1 ≤ t ≤ 3, 1 ≤ r1, r2 ≤ n, 1 ≤ c1, c2 ≤ m) — the type and two coordinates of an action. Additionally, the following holds depending on the value of t:

If t = 1: 2 ≤ r1 ≤ r2 ≤ n - 1, 2 ≤ c1 ≤ c2 ≤ m - 1;
If t = 2: 2 ≤ r1 ≤ r2 ≤ n - 1, 2 ≤ c1 ≤ c2 ≤ m - 1, the specified group of barriers exist on the ground before the removal.
If t = 3: no extra restrictions.
Output
For each of Koyomi’s attempts (actions with t = 3), output one line — containing “Yes” (without quotes) if it’s feasible, and “No” (without quotes) otherwise.

Example
Input
5 6 5
1 2 2 4 5
1 3 3 3 3
3 4 4 1 1
2 2 2 4 5
3 1 1 4 4
Output
No
Yes
Input
2500 2500 8
1 549 1279 1263 2189
1 303 795 1888 2432
1 2227 622 2418 1161
3 771 2492 1335 1433
1 2017 2100 2408 2160
3 48 60 798 729
1 347 708 1868 792
3 1940 2080 377 1546
Output
No
Yes
No
Note
For the first example, the situations of Koyomi’s actions are illustrated below.
这里写图片描述

题意:
题意:在 n×m 的二维图上,有三种操作:
1 r1 c1 r2 c2 表示沿着 (r1, c1, r2, c2) 所表示的矩形的外边框建围墙。(其中 (r1, c1) 为矩形左上角,(r2, c2) 表示矩形右下角)。
2 r1 c1 r2 c2 表示取消 (r1, c1, r2, c2) 所示矩形的围墙。(保证最初图不存在围墙,删除的围墙一定是之前通过操作 1 建立的)。
3 r1 c1 r2 c2 表示询问 (r1, c1) 到 (r2, c2) 是否可以不翻越围墙到达。对于每个操作 3,可以输出 Yes ,否则输出 No 。
(n, m <= 2500, q <= 1e5, 保证围墙没有相交)

分析: 我们将每个矩形区域都加上自身独特的值,这样的话,对于两个点,如果值不相同,则不是一个区域。(就相当于矩形区域覆盖,当覆盖了之后 当前的值为 自身特殊值+其下方被覆盖的区域的值,但是同一个区域中的值都会加那么多,值还是会一样。可以画个图很清新)。
如果确保每个矩形区域都有唯一的值,这个时候我们可以将4个坐标hash为一个值(这样就会导致,每个区域加的值就与确定这个区域的坐标 一 一对应),同时这样删除的时候,也可以o(1)得到矩形区域应该给删除多少。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 2500+10;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int seed = 2333 ;

#define lowbit(x) x&(-x) 
LL hash(int a,int b,int c,int d) { return (LL)a*seed*seed*seed+b*seed*seed+c*seed+d ;  }
struct BIT{
    int nx,ny;
    LL c[MAXN][MAXN];
    void add(int x,int y,LL val){
        for(int i=x;i<=nx;i+=lowbit(i))
            for(int j=y;j<=ny;j+=lowbit(j)) 
                c[i][j]+=val;
    }
    LL sum(int x,int y){
        LL ans=0;
        for(int i=x;i>0;i-=lowbit(i))
            for(int j=y;j>0;j-=lowbit(j)) 
                ans+=c[i][j];
        return ans;
    }
} bit; 


int main(){
    CLOSE();
//  fread();
//  fwrite();
    memset(bit.c,0,sizeof(bit.c));
    int q;scanf("%d%d%d",&bit.nx,&bit.ny,&q);
    int op;int x1,x2,y1,y2; LL val;
    while(q--){
        scanf("%d%d%d%d%d",&op,&x1,&y1,&x2,&y2);
        val=hash(x1,y1,x2,y2);
        if(op==3){
            LL a=bit.sum(x1,y1);
            LL b=bit.sum(x2,y2);
            puts(a==b?"Yes":"No");
            continue;
        }
        if(op==2) val=-val;  
        y2++;x2++;
        bit.add(x1,y1,val);
        bit.add(x1,y2,-val);
        bit.add(x2,y1,-val);
        bit.add(x2,y2,val);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值