1348: TOPOVI
时间限制: 2 Sec 内存限制: 128 MB提交: 14 解决: 1
[ 提交][ 状态][ 讨论版][命题人:]
题目描述
Mirko同时是象棋和编程的大粉丝, 但是传统象棋他感到无聊,他开始开心的玩若干个车。
他发现棋盘有N行N列,K个车。
Mirko的游戏由下列规则组成:
1、每个局都一个能力值,由整数表示
2、每个车能看见它所在的一整行和一整列,除了它自己的格子
3、我们认为某个格子被攻击了,当且仅当看见这个格子的所有车,能力值异或和不为0。
初始时,车都放在棋盘上,Mirko再走P步。
每移动一步,需要计算有多少格子被攻击。
每个车可以移动到任意格子,不一定是同行或者同列。
输入
第一行有整数N,K,P(N<=1000000000,K<=100000,P<=100000)
25%的数据:N,K<=100
接下来K行,包含三个整数R,C,X(1<=R,C<=N,1<=X<=1000000000),表示格子(R,C)有个能力值为X的车。
接下来P行,包含四个整数R1,C1,R2,C2(1<=R1,C1,R2,C2<=N),表示车由(R1,C1)移动至(R2,C2)。
class=MsoNormal style="text-align:left;" >初始时,车都放在棋盘上,Mirko再走P步。每移动一步,需要计算有多少格子被攻击。
每个车可以移动到任意格子,不一定是同行或者同列。
输出
输出共包含P行,第k行表示k步移动以后的被攻击格子总数。
样例输入
2 2 21 1 12 2 22 2 2 11 1 1 2
样例输出
42
提示
来源
题解:这题有点神奇。
我们先把每行和每列xor的情况弄下来,再统计出某个xor值出现的次数(行和列都分开算)。这时你惊奇的发现第i行j列的格子的xor值就是i行xorj列的xor值。统计的次数发挥作用了,对于每次的移动,我们就可以用这个东西来快速统计了(具体看代码,我也难以解释)。
官方题解:
Let R i denote the total XOR of all rooks located in row i . Analogously, we define C i as the total XOR of all rooks located in the column i . Let us notice that the total number of attacked fields is equal to the number of pairs ( i , j ) such that R i C j= / (this is a direct consequence of the fact that the field ( i , j ) is attacked if and only if the total XOR in row i is different that the total XOR in column j ). In the beginning, we can calculate for each k how many rows i there are such that R i = k , and how many columns j there are such that C j = k . It is easy to calculate the number of attacked fields with this information. When a move occurs, we need to be able to efficiently calculate the change in the number of attacked fields. When a rook moves from field ( r , c ), we calculate the number of attacked fields in row r or column c and subtract it from the total number of attacked fields. When a rook moves to field ( r , c ), we calculate the number of attacked fields in row r or column c and add it to the total number of attacked fields. If we use a binary tree (i.e. map in C++) for storing the data, the total time complexity of this algorithm is O( Q lg N ), and the memory complexity O( N ).
Necessary skills: binary tree, combinatorics
Category: ad-hoc
代码(看起来应该不难):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, k, q;
long long sol;
map <int, int> rcnt, ccnt;
map <int, int> rxor, cxor;
map <pair<int, int>, int> rook;
ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void moveRook(int r, int c, int val) {
sol -= n - ccnt[rxor[r]];
sol -= n - rcnt[cxor[c]];
if (rxor[r] != cxor[c])
sol += 1;
--rcnt[rxor[r]];
rxor[r] ^= val;
++rcnt[rxor[r]];
--ccnt[cxor[c]];
cxor[c] ^= val;
++ccnt[cxor[c]];
sol += n - ccnt[rxor[r]];
sol += n - rcnt[cxor[c]];
if (rxor[r] != cxor[c])
sol -= 1;
rook[make_pair(r, c)] ^= val;
}
void init(void) {
n=read();k=read();q=read();
rcnt[0] = ccnt[0] = n;
for (int i = 0; i < k; ++i) {
int r, c, val;
r=read();c=read();val=read();
--r;
--c;
moveRook(r, c, val);
//printf("%d\n",sol);
}
}
void solve(void) {
while (q-- > 0) {
int r1, c1, r2, c2;
r1=read();c1=read();r2=read();c2=read();
--r1; --c1;
--r2; --c2;
int rookValue = rook[make_pair(r1, c1)];
moveRook(r1, c1, rookValue);
moveRook(r2, c2, rookValue);
printf("%lld\n",sol);
}
}
int main(void) {
init();
solve();
return 0;
}