0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 13:16:26 +00:00

Compare commits

..

5 Commits

5 changed files with 841 additions and 0 deletions

183
Luogu/P3165/P3165.cpp Normal file
View File

@ -0,0 +1,183 @@
#include <iostream>
#include <algorithm>
#include <chrono>
#include <limits>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
struct node {
node *lchild, *rchild;
size_t size;
unsigned key;
int value, min;
bool reversed;
node()
: lchild(nullptr),
rchild(nullptr),
size(0),
key(rand()),
value(0),
min(std::numeric_limits<int>::max()),
reversed(false) {}
node(int _value)
: lchild(nullptr),
rchild(nullptr),
size(1),
key(rand()),
value(_value),
min(_value),
reversed(false) {}
~node() {
if (lchild != nullptr) delete lchild;
if (rchild != nullptr) delete rchild;
}
inline size_t lsize() {
return lchild == nullptr ? 0 : lchild->size;
}
inline size_t rsize() {
return rchild == nullptr ? 0 : rchild->size;
}
inline void pushup() {
size = lsize() + 1 + rsize();
min = value;
if (lchild != nullptr) {
min = std::min(min, lchild->min);
}
if (rchild != nullptr) {
min = std::min(min, rchild->min);
}
}
inline void pushdown() {
if (reversed) {
std::swap(lchild, rchild);
if (lchild != nullptr) lchild->reversed = !lchild->reversed;
if (rchild != nullptr) rchild->reversed = !rchild->reversed;
reversed = false;
}
}
};
int n, b[N];
std::pair<int, int> a[N];
node *root;
std::pair<node *, node *> split(node *u, int k) {
if (u == nullptr) return std::make_pair(nullptr, nullptr);
u->pushdown();
if (k <= u->lsize()) {
auto o = split(u->lchild, k);
u->lchild = o.second;
u->pushup();
o.second = u;
return o;
}
auto o = split(u->rchild, k - u->lsize() - 1);
u->rchild = o.first;
u->pushup();
o.first = u;
return o;
}
node *merge(node *x, node *y) {
if (x == nullptr) return y;
if (y == nullptr) return x;
if (x->key < y->key) {
x->pushdown();
x->rchild = merge(x->rchild, y);
x->pushup();
return x;
}
y->pushdown();
y->lchild = merge(x, y->lchild);
y->pushup();
return y;
}
void reverse(int k) {
auto x = split(root, k);
auto y = split(x.first, k - 1);
if (y.first != nullptr) y.first->reversed = !y.first->reversed;
delete y.second;
root = merge(y.first, x.second);
}
int find(node *p) {
int k = 1;
while (p != nullptr) {
p->pushdown();
if (p->lchild != nullptr && p->min == p->lchild->min) {
p = p->lchild;
} else if (p->rchild != nullptr && p->min == p->rchild->min) {
k += p->lsize() + 1;
p = p->rchild;
} else {
return k + p->lsize();
}
}
return -1;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;
}
std::sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) {
b[a[i].second] = i;
}
for (int i = 1; i <= n; i++) {
root = merge(root, new node(b[i]));
}
for (int i = 1; i <= n; i++) {
int k = find(root);
reverse(k);
cout << k + i - 1 << ' ';
}
cout << endl;
// Cleanup
delete root;
return 0;
}

183
Luogu/P4402/P4402.cpp Normal file
View File

@ -0,0 +1,183 @@
#include <iostream>
#include <algorithm>
#include <chrono>
#include <limits>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
struct node {
node *lchild, *rchild;
size_t size;
unsigned key;
int value, min;
bool reversed;
node()
: lchild(nullptr),
rchild(nullptr),
size(0),
key(rand()),
value(0),
min(std::numeric_limits<int>::max()),
reversed(false) {}
node(int _value)
: lchild(nullptr),
rchild(nullptr),
size(1),
key(rand()),
value(_value),
min(_value),
reversed(false) {}
~node() {
if (lchild != nullptr) delete lchild;
if (rchild != nullptr) delete rchild;
}
inline size_t lsize() {
return lchild == nullptr ? 0 : lchild->size;
}
inline size_t rsize() {
return rchild == nullptr ? 0 : rchild->size;
}
inline void pushup() {
size = lsize() + 1 + rsize();
min = value;
if (lchild != nullptr) {
min = std::min(min, lchild->min);
}
if (rchild != nullptr) {
min = std::min(min, rchild->min);
}
}
inline void pushdown() {
if (reversed) {
std::swap(lchild, rchild);
if (lchild != nullptr) lchild->reversed = !lchild->reversed;
if (rchild != nullptr) rchild->reversed = !rchild->reversed;
reversed = false;
}
}
};
int n, b[N];
std::pair<int, int> a[N];
node *root;
std::pair<node *, node *> split(node *u, int k) {
if (u == nullptr) return std::make_pair(nullptr, nullptr);
u->pushdown();
if (k <= u->lsize()) {
auto o = split(u->lchild, k);
u->lchild = o.second;
u->pushup();
o.second = u;
return o;
}
auto o = split(u->rchild, k - u->lsize() - 1);
u->rchild = o.first;
u->pushup();
o.first = u;
return o;
}
node *merge(node *x, node *y) {
if (x == nullptr) return y;
if (y == nullptr) return x;
if (x->key < y->key) {
x->pushdown();
x->rchild = merge(x->rchild, y);
x->pushup();
return x;
}
y->pushdown();
y->lchild = merge(x, y->lchild);
y->pushup();
return y;
}
void reverse(int k) {
auto x = split(root, k);
auto y = split(x.first, k - 1);
if (y.first != nullptr) y.first->reversed = !y.first->reversed;
delete y.second;
root = merge(y.first, x.second);
}
int find(node *p) {
int k = 1;
while (p != nullptr) {
p->pushdown();
if (p->lchild != nullptr && p->min == p->lchild->min) {
p = p->lchild;
} else if (p->rchild != nullptr && p->min == p->rchild->min) {
k += p->lsize() + 1;
p = p->rchild;
} else {
return k + p->lsize();
}
}
return -1;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;
}
std::sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) {
b[a[i].second] = i;
}
for (int i = 1; i <= n; i++) {
root = merge(root, new node(b[i]));
}
for (int i = 1; i <= n; i++) {
int k = find(root);
reverse(k);
cout << k + i - 1 << ' ';
}
cout << endl;
// Cleanup
delete root;
return 0;
}

182
Luogu/SP2059/SP2059.cpp Normal file
View File

@ -0,0 +1,182 @@
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <limits>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
struct node {
node *lchild, *rchild;
size_t size;
unsigned key;
int value, min;
bool reversed;
node()
: lchild(nullptr),
rchild(nullptr),
size(0),
key(rand()),
value(0),
min(std::numeric_limits<int>::max()),
reversed(false) {}
node(int _value)
: lchild(nullptr),
rchild(nullptr),
size(1),
key(rand()),
value(_value),
min(_value),
reversed(false) {}
~node() {
if (lchild != nullptr) delete lchild;
if (rchild != nullptr) delete rchild;
}
inline size_t lsize() {
return lchild == nullptr ? 0 : lchild->size;
}
inline size_t rsize() {
return rchild == nullptr ? 0 : rchild->size;
}
inline void pushup() {
size = lsize() + 1 + rsize();
min = value;
if (lchild != nullptr) {
min = std::min(min, lchild->min);
}
if (rchild != nullptr) {
min = std::min(min, rchild->min);
}
}
inline void pushdown() {
if (reversed) {
std::swap(lchild, rchild);
if (lchild != nullptr) lchild->reversed = !lchild->reversed;
if (rchild != nullptr) rchild->reversed = !rchild->reversed;
reversed = false;
}
}
};
int n, b[N];
std::pair<int, int> a[N];
node *root;
std::pair<node *, node *> split(node *u, int k) {
if (u == nullptr) return std::make_pair(nullptr, nullptr);
u->pushdown();
if (k <= u->lsize()) {
auto o = split(u->lchild, k);
u->lchild = o.second;
u->pushup();
o.second = u;
return o;
}
auto o = split(u->rchild, k - u->lsize() - 1);
u->rchild = o.first;
u->pushup();
o.first = u;
return o;
}
node *merge(node *x, node *y) {
if (x == nullptr) return y;
if (y == nullptr) return x;
if (x->key < y->key) {
x->pushdown();
x->rchild = merge(x->rchild, y);
x->pushup();
return x;
}
y->pushdown();
y->lchild = merge(x, y->lchild);
y->pushup();
return y;
}
void reverse(int k) {
auto x = split(root, k);
auto y = split(x.first, k - 1);
if (y.first != nullptr) y.first->reversed = !y.first->reversed;
delete y.second;
root = merge(y.first, x.second);
}
int find(node *p) {
int k = 1;
while (p != nullptr) {
p->pushdown();
if (p->lchild != nullptr && p->min == p->lchild->min) {
p = p->lchild;
} else if (p->rchild != nullptr && p->min == p->rchild->min) {
k += p->lsize() + 1;
p = p->rchild;
} else {
return k + p->lsize();
}
}
return -1;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
while (cin >> n, n) {
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;
}
std::sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) {
b[a[i].second] = i;
}
for (int i = 1; i <= n; i++) {
root = merge(root, new node(b[i]));
}
for (int i = 1; i <= n; i++) {
int k = find(root);
reverse(k);
cout << k + i - 1 << (i == n ? '\n' : ' ');
}
// Cleanup
delete root;
root = nullptr;
}
return 0;
}

182
Luogu/UVA1402/UVA1402.cpp Normal file
View File

@ -0,0 +1,182 @@
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <limits>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
struct node {
node *lchild, *rchild;
size_t size;
unsigned key;
int value, min;
bool reversed;
node()
: lchild(nullptr),
rchild(nullptr),
size(0),
key(rand()),
value(0),
min(std::numeric_limits<int>::max()),
reversed(false) {}
node(int _value)
: lchild(nullptr),
rchild(nullptr),
size(1),
key(rand()),
value(_value),
min(_value),
reversed(false) {}
~node() {
if (lchild != nullptr) delete lchild;
if (rchild != nullptr) delete rchild;
}
inline size_t lsize() {
return lchild == nullptr ? 0 : lchild->size;
}
inline size_t rsize() {
return rchild == nullptr ? 0 : rchild->size;
}
inline void pushup() {
size = lsize() + 1 + rsize();
min = value;
if (lchild != nullptr) {
min = std::min(min, lchild->min);
}
if (rchild != nullptr) {
min = std::min(min, rchild->min);
}
}
inline void pushdown() {
if (reversed) {
std::swap(lchild, rchild);
if (lchild != nullptr) lchild->reversed = !lchild->reversed;
if (rchild != nullptr) rchild->reversed = !rchild->reversed;
reversed = false;
}
}
};
int n, b[N];
std::pair<int, int> a[N];
node *root;
std::pair<node *, node *> split(node *u, int k) {
if (u == nullptr) return std::make_pair(nullptr, nullptr);
u->pushdown();
if (k <= u->lsize()) {
auto o = split(u->lchild, k);
u->lchild = o.second;
u->pushup();
o.second = u;
return o;
}
auto o = split(u->rchild, k - u->lsize() - 1);
u->rchild = o.first;
u->pushup();
o.first = u;
return o;
}
node *merge(node *x, node *y) {
if (x == nullptr) return y;
if (y == nullptr) return x;
if (x->key < y->key) {
x->pushdown();
x->rchild = merge(x->rchild, y);
x->pushup();
return x;
}
y->pushdown();
y->lchild = merge(x, y->lchild);
y->pushup();
return y;
}
void reverse(int k) {
auto x = split(root, k);
auto y = split(x.first, k - 1);
if (y.first != nullptr) y.first->reversed = !y.first->reversed;
delete y.second;
root = merge(y.first, x.second);
}
int find(node *p) {
int k = 1;
while (p != nullptr) {
p->pushdown();
if (p->lchild != nullptr && p->min == p->lchild->min) {
p = p->lchild;
} else if (p->rchild != nullptr && p->min == p->rchild->min) {
k += p->lsize() + 1;
p = p->rchild;
} else {
return k + p->lsize();
}
}
return -1;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
while (cin >> n, n) {
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;
}
std::sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) {
b[a[i].second] = i;
}
for (int i = 1; i <= n; i++) {
root = merge(root, new node(b[i]));
}
for (int i = 1; i <= n; i++) {
int k = find(root);
reverse(k);
cout << k + i - 1 << (i == n ? '\n' : ' ');
}
// Cleanup
delete root;
root = nullptr;
}
return 0;
}

111
utils/judger_info.cpp Normal file
View File

@ -0,0 +1,111 @@
#if defined(_WIN32)
#include <windows.h>
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/param.h>
#include <sys/types.h>
#include <unistd.h>
#if defined(BSD)
#include <sys/sysctl.h>
#endif
#else
#error "Unable to define getMemorySize( ) for an unknown OS."
#endif
#include <cpuid.h>
#include <iostream>
#include <stdint.h>
static void cpuid(uint32_t func, uint32_t sub, uint32_t data[4]) {
__cpuid_count(func, sub, data[0], data[1], data[2], data[3]);
}
/**
* Returns the size of physical memory (RAM) in bytes.
*/
size_t getMemorySize() {
#if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__))
/* Cygwin under Windows. ------------------------------------ */
/* New 64-bit MEMORYSTATUSEX isn't available. Use old 32.bit */
MEMORYSTATUS status;
status.dwLength = sizeof(status);
GlobalMemoryStatus(&status);
return (size_t)status.dwTotalPhys;
#elif defined(_WIN32)
/* Windows. ------------------------------------------------- */
/* Use new 64-bit MEMORYSTATUSEX, not old 32-bit MEMORYSTATUS */
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
return (size_t)status.ullTotalPhys;
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
/* UNIX variants. ------------------------------------------- */
/* Prefer sysctl() over sysconf() except sysctl() HW_REALMEM and HW_PHYSMEM */
#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
int mib[2];
mib[0] = CTL_HW;
#if defined(HW_MEMSIZE)
mib[1] = HW_MEMSIZE; /* OSX. --------------------- */
#elif defined(HW_PHYSMEM64)
mib[1] = HW_PHYSMEM64; /* NetBSD, OpenBSD. --------- */
#endif
int64_t size = 0; /* 64-bit */
size_t len = sizeof(size);
if (sysctl(mib, 2, &size, &len, NULL, 0) == 0)
return (size_t)size;
return 0L; /* Failed? */
#elif defined(_SC_AIX_REALMEM)
/* AIX. ----------------------------------------------------- */
return (size_t)sysconf(_SC_AIX_REALMEM) * (size_t)1024L;
#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
/* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */
return (size_t)sysconf(_SC_PHYS_PAGES) * (size_t)sysconf(_SC_PAGESIZE);
#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGE_SIZE)
/* Legacy. -------------------------------------------------- */
return (size_t)sysconf(_SC_PHYS_PAGES) * (size_t)sysconf(_SC_PAGE_SIZE);
#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM))
/* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */
int mib[2];
mib[0] = CTL_HW;
#if defined(HW_REALMEM)
mib[1] = HW_REALMEM; /* FreeBSD. ----------------- */
#elif defined(HW_PYSMEM)
mib[1] = HW_PHYSMEM; /* Others. ------------------ */
#endif
unsigned int size = 0; /* 32-bit */
size_t len = sizeof(size);
if (sysctl(mib, 2, &size, &len, NULL, 0) == 0)
return (size_t)size;
return 0L; /* Failed? */
#endif /* sysctl and sysconf variants */
#else
return 0L; /* Unknown OS. */
#endif
}
int main() {
uint32_t data[4];
char str[48];
for (int i = 0; i < 3; ++i) {
cpuid(0x80000002 + i, 0, data);
for (int j = 0; j < 4; ++j)
reinterpret_cast<uint32_t*>(str)[i * 4 + j] = data[j];
}
size_t memorySize = getMemorySize();
std::cout << "CPU: " << str << std::endl;
std::cout << "Memory: " << (memorySize / 1024 / 1024) << "MB" << std::endl;
return 0;
}