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.

题意:

给出 \(n*m\) 的矩阵, \(q\) 次操作,操作有如下类型:

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

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

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

题解:

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

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

#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;
}
posted @ 2017-10-07 14:32  tarjan's  阅读(158)  评论(0编辑  收藏  举报