Files
SH-Quizzes/ACMOJ-1728.hpp
2023-12-23 22:23:48 +08:00

41 lines
1017 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <algorithm>
#include <cstring>
void Init(int **&p, int n) { // 初始化n为vector的个数,传入的p指向nullptr
// todo
p = new int *[n + 1];
p[0] = new int[n + 1];
p[0][0] = n;
for (int i = 1; i <= n; i++) p[0][i] = 1;
for (int i = 1; i <= n; i++) {
p[i] = new int[2];
p[i][0] = 0;
}
}
void Add_element(int **&p, int x,
int y) { // 在第x(1base)个vector后面添加一个元素y
// todo
if (p[x][0] + 1 <= p[0][x]) {
p[x][0]++;
p[x][p[x][0]] = y;
} else {
int *new_p = new int[p[0][x] * 2 + 1];
memcpy(new_p, p[x], sizeof(int) * (p[x][0] + 1));
p[0][x] *= 2;
delete[] p[x];
p[x] = new_p;
p[x][0]++;
p[x][p[x][0]] = y;
}
}
int Get_element(int **&p, int x,
int k) { // 获取第x(1base)个vector中第k个(1-base)元素的值
// todo
return p[x][k];
}
void Clear(int **&p, int n) { // 回收空间
// todo
for (int i = 1; i <= p[0][0]; i++) delete[] p[i];
delete[] p[0];
delete[] p;
}