-
Notifications
You must be signed in to change notification settings - Fork 0
/
A2.cpp
295 lines (290 loc) · 7.46 KB
/
A2.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//Implements Sparse Matrix with Circular Multi Linked List
#include <cmath>
#include <iostream>
using namespace std;
struct node{
int long r = 0;
int long c = 0;
bool flag = false;
union{
int long val = 0;
struct node* next;
};
struct node* row = NULL; //traverse down btw rows
struct node* col = NULL; //traverse right btw columns
};
struct node* sptr = (struct node *)malloc(sizeof(struct node)); //Super point longer
//Defining all functions that have to be carried of by this program.
void AddLink(int long i, int long j)
{
bool a = true, b = true;
if(i == j)
b = false;
struct node* traverse = sptr;
struct node* trav;
struct node* travs;
while (a || b)
{
if(traverse->next->r > i && a)
{
auto *temp = (struct node *) malloc(sizeof(struct node));
temp->next = traverse->next;
temp->r = i;
temp->c = i;
temp->flag = true;
temp->col = temp;
temp->row = temp;
traverse->next = temp;
trav = temp;
a = false;
}
else if(traverse->next->r > j && b)
{
auto *temp = (struct node *) malloc(sizeof(struct node));
temp->next = traverse->next;
temp->r = j;
temp->c = j;
temp->flag = true;
temp->col = temp;
temp->row = temp;
traverse->next = temp;
travs = temp;
b = false;
}
else if(traverse->next->r == i && a)
{
trav = traverse->next;
a = false;
}
else if(traverse->next->r == j && b)
{
travs = traverse->next;
b = false;
}
traverse = traverse->next;
}
if(i == j)
travs = trav;
traverse = trav;
while(true)
{
//cout << 74 << endl;
if(traverse->col->c < j && !traverse->col->flag)
{
traverse = traverse->col;
continue;
}
else if(traverse->col->c == j && !traverse->col->flag)
{
traverse->col->val++;
break;
}
else if(traverse->col->flag || traverse->col->c > j)
{
auto *temp = (struct node *)malloc(sizeof(struct node));
temp->col = traverse->col;
traverse->col = temp;
traverse->col->r = i;
traverse->col->c = j;
traverse->col->val = 1;
traverse->col->flag = false;
//Starting Vertical linking
traverse = travs;
while(true)
{
//cerr << 99 << endl;
if(traverse->row->r > i)
{
temp->row = traverse->row;
traverse->row = temp;
break;
}
else if(traverse->row->flag)
{
temp->row = traverse->row;
traverse->row = temp;
break;
}
traverse = traverse->row;
}
break;
}
}
}
void DeleteLink(int long i, int long j)
{
struct node *traverse = sptr->next;
while(traverse->r != i)
{
traverse = traverse->next;
if(traverse == sptr)
return;
}
while(traverse->col->c != j)
{
traverse = traverse->col;
if(traverse->col->flag)
return;
}
traverse->col->val--;
if (traverse->col->val == 0)
{
struct node* travs;
struct node* temp = traverse->col->col;
travs = traverse->col;
traverse->col = temp;
while(true)
{
if(travs->row->val == 0 && !travs->row->flag)
{
temp = travs->row->row;
free(travs->row);
travs->row = temp;
break;
}
travs = travs->row;
}
return;
}
}
int long RetrieveValue(int long i, int long j)
{
struct node *traverse = sptr->next;
while(traverse->r != i)
{
traverse = traverse->next;
if(traverse == sptr)
return 0;
}
while(traverse->col->c != j)
{
traverse = traverse->col;
if(traverse->col->flag)
return 0;
}
if (traverse->col->flag)
return 0;
else
return (traverse->col->val);
}
int long RetrieveRowSum(int long i, int long k)
{
struct node* traverse = sptr->next;
while(traverse->r != i)
{
if(traverse == sptr)
return 0;
traverse = traverse->next;
}
int long sum = 0;
traverse = traverse->row;
while(traverse->r < k && !traverse->flag)
{
sum = sum + traverse->val;
traverse = traverse->row;
}
return sum;
}
int long RetrieveColumnSum(int long j, int long k)
{
struct node *traverse = sptr->next;
while(traverse->c != j)
{
traverse = traverse->next;
if (traverse == sptr)
return 0;
}
int long sum = 0;
traverse = traverse->col;
while (traverse->c < k && !traverse->flag)
{
sum = sum + traverse->val;
traverse = traverse->col;
}
return sum;
}
void MultiplyVector()
{
long int n;
cin >> n;
if(n <= 0)
{
cout << 0 << endl;
return;
}
long int arr[n];
for(int long t = 0; t < n; t++)
{
cin >> arr[t];
}
int long sarr[n] = {0};
struct node* traverse = sptr->next;
struct node* travs = sptr->next;
while(traverse->c < n && traverse != sptr)
{
if(traverse->row->flag != true )
{
travs = traverse->row;
while(travs->r<n && travs->flag != true)
{
sarr[travs->c] = sarr[travs->c] + travs->val*arr[travs->r];
travs = travs->row;
}
}
traverse= traverse->next;
}
for(int r = 0; r < n - 1; r++)
{
cout << sarr[r] << " ";
}
cout << sarr[n-1] << endl;
}
int main() {
int long q = 0, x = 0, i = 0, j = 0, k = 0, n = 0; //x for query number, i,j are row and column.
cin >> q; //storing number of queries
sptr->r = 1000000000;
sptr->c = 1000000000;
sptr->flag = true;
sptr->next = sptr;
sptr->row = nullptr;
sptr->col = nullptr;
for (int long a = 0; a < q; a++)
{
cin >> x;
if(x == 1)
{
cin >> i >> j;
AddLink(i, j);
//continue;
}
else if(x == 2)
{
cin >> i >> j;
DeleteLink(i, j);
//continue;
}
else if(x == 3)
{
cin >> i >> j;
cout << RetrieveValue(i, j) << endl;
//continue;
}
else if(x == 4)
{
cin >> i >> k;
cout << RetrieveRowSum(i, k) << endl;
//continue;
}
else if(x == 5)
{
cin >> j >> k;
cout << RetrieveColumnSum(j, k) << endl;
//continue;
}
else if(x == 6)
{
MultiplyVector();
continue;
}
}
}