-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUVA-12657.cpp
148 lines (122 loc) · 1.95 KB
/
UVA-12657.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include<stdio.h>
#define MAXN 100005
int left[MAXN];
int right[MAXN];
int inv, n, m;
void init() {
int i;
inv = 0;
for(i = 0; i <= n; ++i) {
left[i] = (i + n) % (n + 1);
right[i] = (i + 1) % (n + 1);
}
}
void swap(int x, int y) {
int xl = left[x], xr = right[x];
int yl = left[y], yr = right[y];
if(right[x] != y && right[y] != x) {
right[xl] = y;
left[y] = xl;
left[xr] = y;
right[y] = xr;
right[yl] = x;
left[x] = yl;
left[yr] = x;
right[x] = yr;
return;
}
if(right[x] == y) {
right[xl] = y;
left[y] = xl;
left[yr] = x;
right[x] = yr;
left[x] = y;
right[y] = x;
return;
}
if(right[y] == x) {
right[yl] = x;
left[x] = yl;
left[xr] = y;
right[y] = xr;
left[y] = x;
right[x] = y;
return;
}
}
void change1(int x, int y) {
if(right[x] == y) {
return;
}
int xl = left[x], xr = right[x];
int yl = left[y], yr = right[y];
right[xl] = xr;
left[xr] = xl;
right[yl] = x;
left[x] = yl;
left[y] = x;
right[x] = y;
}
void change2(int x, int y) {
if(left[x] == y) {
return;
}
int xl = left[x], xr = right[x];
int yl = left[y], yr = right[y];
right[xl] = xr;
left[xr] = xl;
left[yr] = x;
right[x] = yr;
right[y] = x;
left[x] = y;
}
long long count() {
int i;
int type, x, y;
for(i = 0; i < m; ++i) {
scanf("%d", &type);
if(type < 4) {
scanf("%d %d", &x, &y);
} else {
inv = 1 - inv;
continue;
}
if(type == 3) {
swap(x, y);
continue;
}
if(inv) {
type = 3 - type;
}
if(type == 1) {
change1(x, y);
continue;
}
if(type == 2) {
change2(x, y);
continue;
}
}
x = 0;
long long sum = 0;
for(i = 1; i <= n; ++i) {
x = right[x];
if(i % 2 == 1)
sum += x;
}
if(inv && n % 2 == 0) {
sum = n * (long long)(n + 1) / 2 - sum;
}
return sum;
}
int main() {
int i = 0;
long long sum = 0;
while(scanf("%d %d", &n, &m) == 2) {
++i;
init();
sum = count();
printf("Case %d: %lld\n", i, sum);
}
return 0;
}