1. 程式人生 > >codeforces 869E. The Untended Antiquity(二維樹狀陣列,隨機化)

codeforces 869E. The Untended Antiquity(二維樹狀陣列,隨機化)

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 的矩陣, 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;
}