codeforces 869E. The Untended Antiquity(二维树状数组,随机化)

codeforces 869E

E. The Untended Antiquity
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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.

Examples
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.

题意:

给出 nm n ∗ m 的矩阵, q q 次操作,操作有如下类型:

1.加一个左上角为 (x1,y1) 右下角为 (x2,y2) ( x 2 , y 2 ) 的围墙。

2.删除左上角为 (x1,y1) ( x 1 , y 1 ) 右下角为 (x2,y2) ( x 2 , y 2 ) 的围墙。

3.询问 (x1,y1) ( x 1 , y 1 ) 有没有方法到 (x2,y2) ( x 2 , y 2 ) 且不经过围墙。

题解:

比赛的时候想到了用二维树状数组,对于左上角为 (x1,y1) ( x 1 , y 1 ) ,右下角为 (x2,y2) ( x 2 , y 2 ) 的矩阵,在四个点上打标记,四个点分别为 (x1,y1),(x2+1,y2+1),(x1,y2+1),(x2+1,y1) ( x 1 , y 1 ) , ( x 2 + 1 , y 2 + 1 ) , ( x 1 , y 2 + 1 ) , ( x 2 + 1 , y 1 ) .对于询问就直接判断 sum(x1,y1) s u m ( x 1 , y 1 ) sum(x2,y2) s u m ( x 2 , y 2 ) 是否相等即可。

不过这里标记的值就不能随便选成 1,1 1 , − 1 了,因为这样很容易找到反例,使用随机数来标记每个矩阵( map m a p 存一下每个矩阵对应的值,删除这个矩阵的时候用),随机数也有不同姿势,详见代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<ctime>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
#define dbg(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<endl;
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=2500+10;

map<pair<PII,PII>,ll> mp;

ll c[maxn][maxn];
int n,m,q;
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int y,ll v)
{
    for(int i=x;i<=n;i+=lowbit(i))
        for(int j=y;j<=m;j+=lowbit(j))
            c[i][j]^=1ll*v;
}

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^=1ll*c[i][j];
    return ans;
}

int rd()
{
    return rand() << 15|rand();
}

ll gen()
{
    ll x = rand();
    x ^= ((ll)rand()<<15);
    x ^= ((ll)rand()<<30);
    x ^= ((ll)rand()<<45);
    return x;
}

int main()
{
    scanf("%d%d%d",&n,&m,&q);
    srand(time(NULL));
    rep(i,1,q+1)
    {
        int op,x1,y1,x2,y2;
        scanf("%d%d%d%d%d",&op,&x1,&y1,&x2,&y2);
        if(op==1)
        {
            ll g=gen();  //ll g=rd();也可
            while(!g) g=gen();
            mp[make_pair(PII(x1,y1),PII(x2,y2))]=g;
            add(x1,y1,g);
            add(x2+1,y2+1,g);
            add(x1,y2+1,g);
            add(x2+1,y1,g);
        }
        else if(op==2)
        {
            ll g=mp[make_pair(PII(x1,y1),PII(x2,y2))];
            add(x1,y1,g);
            add(x2+1,y2+1,g);
            add(x1,y2+1,g);
            add(x2+1,y1,g);
        }
        else if(op==3)
        {
            if(x1==x2&&y1==y2)
            {
                puts("Yes");
            }
            else
            {
                ll t1=sum(x1,y1),t2=sum(x2,y2);
                if(t1==t2) puts("Yes");
                else puts("No");
            }
        }
    }
    return 0;
}

这里包括另外一种随机化方法,不过感觉没必要?

不过这里二维树状数组不是加减,而是异或,感觉这样能够加速运算,而且不容易被构造的数据卡掉

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<ctime>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
#define dbg(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<endl;
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=2500+10;

map<pair<PII,PII>,ll> mp;

ll c[maxn][maxn];
int n,m,q;
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int y,ll v)
{
    for(int i=x;i<=n;i+=lowbit(i))
        for(int j=y;j<=m;j+=lowbit(j))
            c[i][j]^=1ll*v;
}

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^=1ll*c[i][j];
    return ans;
}

int main()
{
    scanf("%d%d%d",&n,&m,&q);
    srand(n^m^q^time(0)^19260817);
    rep(i,1,q+1)
    {
        int op,x1,y1,x2,y2;
        scanf("%d%d%d%d%d",&op,&x1,&y1,&x2,&y2);
        srand(555^(rand()*32768+rand())^op^x1^y1^x2^y2);
        ll g=0;
        for(int i=1;i<=6;++i) g=g*32768+rand();
        if(!g) g^=rand();
        if(op==1)
        {
            mp[make_pair(PII(x1,y1),PII(x2,y2))]=g;
            add(x1,y1,g);
            add(x2+1,y2+1,g);
            add(x1,y2+1,g);
            add(x2+1,y1,g);
        }
        else if(op==2)
        {
            g=mp[make_pair(PII(x1,y1),PII(x2,y2))];
            add(x1,y1,g);
            add(x2+1,y2+1,g);
            add(x1,y2+1,g);
            add(x2+1,y1,g);
        }
        else if(op==3)
        {
            if(x1==x2&&y1==y2)
            {
                puts("Yes");
            }
            else
            {
                ll t1=sum(x1,y1),t2=sum(x2,y2);
                if(t1==t2) puts("Yes");
                else puts("No");
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
树状数组(Fenwick Tree)是一种用于高效处理区间和查询的数据结构,常用于解一维数组的前缀和、区间更新和查询等问题。 在 Codeforces 上,树状数组常被用来解决一些与区间和查询有关的问题。它可以在 O(logn) 的时间内完成单点更新和查询,以及区间求和等操作。 下面是一个简单的示例代码,展示了如何实现一个基本的树状数组: ```cpp #include <iostream> #include <vector> using namespace std; // 获取最低位的 1 int getLowbit(int x) { return x & -x; } // 树状数组的单点更新操作 void update(vector<int>& fenwick, int index, int delta) { while (index < fenwick.size()) { fenwick[index] += delta; index += getLowbit(index); } } // 树状数组的前缀和查询操作 int query(vector<int>& fenwick, int index) { int sum = 0; while (index > 0) { sum += fenwick[index]; index -= getLowbit(index); } return sum; } int main() { int n; cin >> n; vector<int> fenwick(n + 1, 0); // 初始化树状数组 for (int i = 1; i <= n; i++) { int val; cin >> val; update(fenwick, i, val); } // 进行查询操作 int q; cin >> q; while (q--) { int type; cin >> type; if (type == 1) { int index, delta; cin >> index >> delta; update(fenwick, index, delta); } else if (type == 2) { int l, r; cin >> l >> r; int sum = query(fenwick, r) - query(fenwick, l - 1); cout << sum << endl; } } return 0; } ``` 在这个示例中,我们使用了一个长度为 n 的数组 `fenwick` 来表示树状数组。`update` 函数用于更新树状数组中的某个元素,`query` 函数用于查询树状数组中某个区间的和。 你可以根据具体问题的要求进行相应的修改和扩展。希望对你有所帮助!如果有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值