JZOJ4838. I Like Matrix!
来源:互联网 发布:客服中心数据分析 编辑:程序博客网 时间:2024/05/17 22:11
题目大意
给定一个
每次操作有四种:
1 i j 把矩阵(i,j) 取反2 i 把第i 行取反3 j 把第j 列取反4 k 回到第k 次操作以后
求每次操作完成之后矩阵元素之和。
Data Constraint
题解
对于每个操作,我们可以在对应的状态间连边,构出一棵操作树。
然后我们沿边DFS每次暴力修改和撤销。
时间复杂度:
SRC
#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespace std ;#define N 1000 + 10#define M 100000 + 10struct Operator { int type ; int x , y ; Operator ( int T = 0 , int X = 0 , int Y = 0 ) { type = T , x = X , y = Y ; }} OP[M] ;int Node[M] , Next[M] , Head[M] , tot ;int Mat[N][N] , Ans[M] ;int n , m , Q , Cnt ;void link( int u , int v ) { Node[++tot] = v ; Next[tot] = Head[u] ; Head[u] = tot ;}void Modify( Operator t ) { int op = t.type ; int x = t.x , y = t.y ; if ( op == 1 ) { Mat[x][y] = !Mat[x][y] ; Cnt += ( Mat[x][y] ? 1 : -1 ) ; } else if ( op == 2 ) { for (int j = 1 ; j <= m ; j ++ ) { if ( Mat[x][j] ) Cnt -- ; else Cnt ++ ; Mat[x][j] ^= 1 ; } } else if ( op == 3 ) { for (int j = 1 ; j <= n ; j ++ ) { if ( Mat[j][y] ) Cnt -- ; else Cnt ++ ; Mat[j][y] ^= 1 ; } }}void DFS( int x ) { if ( x ) Modify( OP[x] ) ; Ans[x] = Cnt ; for (int p = Head[x] ; p ; p = Next[p] ) DFS( Node[p] ) ; if ( x ) Modify( OP[x] ) ;}int main() { freopen( "present.in" , "r" , stdin ) ; freopen( "present.out" , "w" , stdout ) ; scanf( "%d%d%d" , &n , &m , &Q ) ; for (int i = 1 ; i <= Q ; i ++ ) { int op ; scanf( "%d" , &op ) ; if ( op == 1 ) { int x , y ; scanf( "%d%d" , &x , &y ) ; OP[i] = Operator( op , x , y ) ; link( i - 1 , i ) ; } else if ( op == 2 ) { int x ; scanf( "%d" , &x ) ; OP[i] = Operator( op , x , 0 ) ; link( i - 1 , i ) ; } else if ( op == 3 ) { int y ; scanf( "%d" , &y ) ; OP[i] = Operator( op , 0 , y ) ; link( i - 1 , i ) ; } else { int k ; scanf( "%d" , &k ) ; OP[i] = Operator( op , 0 , 0 ) ; link( k , i ) ; } } DFS(0) ; for (int i = 1 ; i <= Q ; i ++ ) printf( "%d\n" , Ans[i] ) ; return 0 ;}
以上.
2 0
- [JZOJ4838]I Like Matrix!
- JZOJ4838. I Like Matrix!
- Jzoj4838 I like Matrix!
- I Like Matrix!
- SSP P2821 I Like Matrix!
- SSL P2822 I Will Like Matrix!
- I like like you
- jzoj 4838. 【GDOI2017模拟11.1】I Like Matrix! 离线处理+暴力
- 2017年11月01日普及组 I Like Matrix!
- 2017年11月01日普及组 I Will Like Matrix!
- I - Matrix
- Why I Like Windows
- i like this man
- I like small font..
- I like swimming...
- I LIKE SQA
- I LIKE SQA
- I LIKE SQA
- 基于libuinet的IPv6调试
- 11.1
- [JZOJ4838]I Like Matrix!
- 一样是做鸭,绝味与周黑鸭的利润为何相差那么大?
- js平滑滚动到顶部,底部,指定地方
- JZOJ4838. I Like Matrix!
- java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=findAll], {ExactMatcher:fDi
- 也不是每个腊鸡搬我帖,删我评论,我都会愤怒的
- hibernate delete update 失效问题
- Android 程序员必须知道的 53 个知识点
- windows2008系统缓存修改
- I,P,B帧和PTS,DTS的关系
- Android的一些命名规范
- linux下安装编译ffmpeg