题目:
http://codeforces.com/contest/707/problem/D
将书架与层数分别与时间建立关系
大神代码
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define N 1010 4 #define M 100100 5 6 int n, m, q, type, a, b; 7 int last[M][N], f[M]; 8 bool on[M][N]; 9 10 int main() { 11 scanf("%d %d %d", &n, &m, &q); 12 for (int t = 1; t <= q; t ++) { 13 for (int i = 1; i <= n; i ++) last[t][i] = last[t-1][i]; 14 scanf("%d", &type); 15 if (type == 1 || type == 2) { 16 scanf("%d %d", &a, &b); 17 f[t] = f[t-1]; 18 for (int j = 1; j <= m; j ++) on[t][j] = on[last[t][a]][j]; 19 last[t][a] = t; 20 f[t] -= on[t][b]; 21 on[t][b] = (type == 1); 22 f[t] += on[t][b]; 23 } 24 else if (type == 3) { 25 scanf("%d", &a); 26 f[t] = f[t-1]; 27 for (int j = 1; j <= m; j ++) { 28 f[t] -= on[last[t][a]][j]; 29 on[t][j] = !on[last[t][a]][j]; 30 f[t] += on[t][j]; 31 } 32 last[t][a] = t; 33 } 34 else { 35 scanf("%d", &a); 36 f[t] = f[a]; 37 for (int i = 1; i <= n; i ++) last[t][i] = last[a][i]; 38 } 39 printf("%d ", f[t]); 40 } 41 return 0; 42 }
弱鸡代码
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e3+10; 4 const int maxn2 = 1e5+10; 5 int T[maxn][maxn]; // T[i][j] 第i次处理的第j个书架 6 bool Q[maxn][maxn]; // Q[i][j] 第i次处理的第j层 7 int num[maxn2]; //num[i] 第i次处理的书的个数 8 int main() 9 { 10 int n,m,q,a,b,t; 11 cin>>n>>m>>q; 12 for(int i = 1; i <= q; i ++){ 13 for(int j = 1; j <= n; j ++) T[i][j] = T[i-1][j]; 14 cin>>t; 15 if(t == 1 || t == 2){ 16 num[i] = num[i-1]; 17 cin>>a>>b; 18 for(int k = 1; k <= m; k ++) Q[i][k] = Q[T[i][a]][k]; 19 T[i][a] = i; 20 num[i] -= Q[i][b]; 21 Q[i][b] = (t == 1); 22 num[i] += Q[i][b]; 23 } 24 else if(t == 3){ 25 num[i] = num[i-1]; 26 cin>>a; 27 for(int k = 1; k <= m; k ++){ 28 num[i] -= Q[T[i][a]][k]; 29 Q[i][k] =! Q[T[i][a]][k]; 30 num[i] += Q[i][k]; 31 } 32 T[i][a] = i; 33 } 34 else{ 35 cin>>a; 36 num[i] = num[a]; 37 for(int j = 1; j <= n; j ++) T[i][j] = T[a][j]; 38 } 39 cout<<num[i]<<endl; 40 } 41 } 42 /* 13 43 7 2 18 44 2 5 2 45 1 3 1 46 2 7 1 47 3 4 48 4 0 49 1 6 1 50 4 0 51 4 4 52 1 6 2 53 4 7 54 3 5 55 2 2 2 56 2 6 1 57 4 7 58 1 7 2 59 2 4 2 60 3 4 61 1 1 1 62 Output 63 0 64 1 65 1 66 3 67 0 68 1 69 0 70 3 71 4 72 0 73 2 74 2 75 2 76 0 77 1 78 1 79 3 80 4 81 */ 82 /* 15 83 19 7 17 84 1 7 3 85 2 6 5 86 2 16 3 87 2 11 3 88 3 2 89 1 18 1 90 1 14 1 91 3 6 92 4 4 93 2 13 3 94 2 16 6 95 3 18 96 1 4 7 97 4 2 98 1 9 4 99 4 8 100 4 10 101 Output 102 1 103 1 104 1 105 1 106 8 107 9 108 10 109 17 110 1 111 1 112 1 113 8 114 9 115 1 116 2 117 17 118 1 119 */
只有不断学习才能进步!