CodeChef:Magic Board(思维 & 树状数组)
来源:互联网 发布:淘宝黑号是什么意思 编辑:程序博客网 时间:2024/05/29 17:06
All submissions for this problem are available.
Long long ago, there is a magic board. The magic board has N*N cells: N rows and Ncolumns. Every cell contains one integer, which is 0 initially. Let the rows and the columns be numbered from 1 to N.
There are 2 types of operations can be applied to the magic board:
- RowSet i x: it means that all integers in the cells on row i have been changed to x after this operation.
- ColSet i x: it means that all integers in the cells on column i have been changed to x after this operation.
And your girlfriend sometimes has an interest in the total number of the integers 0s on some row or column.
- RowQuery i: it means that you should answer the total number of 0s on row i.
- ColQuery i: it means that you should answer the total number of 0s on column i.
Input
The first line of input contains 2 space-separated integers N and Q. They indicate the size of the magic board, and the total number of operations and queries from the girlfriend.
Then each of the next Q lines contains an operation or a query by the format mentioned above.
Output
For each query, output the answer of the query.
Constraints
1 ≤ N, Q ≤ 500000 (5 * 105)
1 ≤ i ≤ N
x ∈ {0, 1} (That is, x = 0 or 1)
Sample
Input:3 6RowQuery 1ColSet 1 1RowQuery 1ColQuery 1RowSet 1 0ColQuery 1Output:3201
Explanation
At first, the magic board is
000 <- row 1000000
So the answer of first query "RowQuery 1" is 3.
After the "ColSet 1 1", the board becomes
column 1|V100100100
So the answer of the second query "RowQuery 1" is clearly 2, since the cell (1,1)became 1. And the answer of the third query "ColQuery 1" is 0.
Finally, apply the operation "RowSet 1 0", the board has changed to
000100100
From this board, the answer of the last query "ColQuery 1" should be 1.
题意:Q个操作,修改操作为将一行或一列设置成x,查询操作为输出某一行或列中0的数量。思路:加如要查询行i的0数量,先判断最后一次修改行i是什么值及时间t,如果是1,找出有几列是t之后被修改为0的,这些可以通过时间戳+Bit树完成。
# include <bits/stdc++.h># define mp make_pair# define pii pair<int,int># define A first# define B secondusing namespace std;const int maxn = 5e5+10;pii c[maxn+3], r[maxn+3];int c0[maxn+3], c1[maxn+3], r0[maxn+3], r1[maxn+3];inline void update(int a[], int pos, int val){ if(pos == 0) return; for(int i=pos; i<=maxn; i+=i&(-i)) a[i] += val;}inline int query(int a[], int pos){ if(pos == 0) return 0; int ans = 0; for(int i=pos; i; i-=i&(-i)) ans += a[i]; return ans;}int main(){ int n, q, x, y; char s[13]; scanf("%d%d",&n,&q); for(int i=1; i<=q; ++i) { scanf("%s",s); if(s[3] == 'S') { scanf("%d%d",&x,&y); if(s[0] == 'R') { if(r[x].B) update(r1, r[x].A, -1); else update(r0, r[x].A, -1); if(y) update(r1, i, 1); else update(r0, i, 1); r[x] = mp(i, y); } else { if(c[x].B) update(c1, c[x].A, -1); else update(c0, c[x].A, -1); if(y) update(c1, i, 1); else update(c0, i, 1); c[x] = mp(i, y); } } else { scanf("%d",&x); int ans = 0; if(s[0] == 'R') { if(r[x].B) ans = query(c0, i) - query(c0, r[x].A); else ans = n - query(c1, i) + query(c1, r[x].A); } else { if(c[x].B) ans = query(r0, i) - query(r0, c[x].A); else ans = n - query(r1, i) + query(r1, c[x].A); } printf("%d\n",ans); } } return 0;}
- CodeChef:Magic Board(思维 & 树状数组)
- bzoj 4994(思维+树状数组)
- 【CodeChef】Chef and Churu 分块+树状数组
- CodeChef "Chef and Churus" 分块+树状数组
- hdu 4605-Magic Ball Game(树状数组)
- hdu 4605 Magic Ball Game(离线+树状数组)
- hdu 5125 magic balls LIS (dp+树状数组优化)
- HDOJ 5125 magic balls(树状数组优化)
- CodeForces 605D Board Game(树状数组)
- HDU 4605 Magic Ball Game 树状数组
- HDU 4605 Magic Ball Game 树状数组
- Magic Ball Game - HDU 4605 树状数组
- hdu4605 Magic Ball Game(树状数组)
- HDU 4605 Magic Ball Game 树状数组
- Magic Rankings | CodeChef
- Codechef Magic Trick
- HDU 4777 思维 + 树状数组
- 【树状数组--思维】poj1990 MooFest
- Jackson与Hibernate配合时产生一些问题
- Listview、RecyclerView中的item中包含可以点击的控件,点击冲突解决办法
- Others(理论知识)
- STL deque的一些函数练习
- 使用github+hexo搭建免费个人博客
- CodeChef:Magic Board(思维 & 树状数组)
- Python学习篇 之 如何实现多态机制
- POJ3145: Harmony Forever
- linux多线程之mutex
- PAT[1046]划拳
- 线性代数之行列式(1) ——行列式的定义以及二阶行列式
- MyEclipse启动Tomcat报outOfMerorgError
- html 应用记录
- 接口自动化测试:mock server之Moco工具