# T1、矩阵乘法

## $Source$：

//#pragma GCC optimize(2)
#include <cstdio>
#include <cstring>
#include <algorithm>
inline int in() {
int x = 0; char c = getchar(); bool f = 0;
while (c < '0' || c > '9')
f |= c == '-', c = getchar();
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return f ? -x : x;
}
template<typename T>inline void chk_min(T &_, T __) { _ = _ < __ ? _ : __; }
template<typename T>inline void chk_max(T &_, T __) { _ = _ > __ ? _ : __; }

const int N = 505, Q = 60005;

struct info {
int id, x1, y1, x2, y2, k;
} b[N * N + Q];
int n, m, q, id[N * N + Q], tmp1[N * N + Q], tmp2[N * N + Q], pos[N * N], res[Q];
int nn, mp[N * N];

struct persistable_segment_tree {
int sum[N * N * 11], c[N * N * 11][2], rt[N * N], tot;
void clear() {
tot = 0;
}
int new_node() {
++tot;
sum[tot] = c[tot][0] = c[tot][1] = 0;
}
void modify(int pos, int tl, int tr, int pre, int &p) {
p = new_node(), sum[p] = sum[pre];
++sum[p];
if (tl == tr)
return ;
int mid = (tl + tr) >> 1;
if (mid >= pos) {
c[p][1] = c[pre][1];
modify(pos, tl, mid, c[pre][0], c[p][0]);
} else {
c[p][0] = c[pre][0];
modify(pos, mid + 1, tr, c[pre][1], c[p][1]);
}
}
int query(int l, int r, int tl, int tr, int pre, int p) {
if (l <= tl && tr <= r)
return sum[p] - sum[pre];
int mid = (tl + tr) >> 1;
if (mid < l)
return query(l, r, mid + 1, tr, c[pre][1], c[p][1]);
if (mid >= r)
return query(l, r, tl, mid, c[pre][0], c[p][0]);
return query(l, r, tl, mid, c[pre][0], c[p][0]) +
query(l, r, mid + 1, tr, c[pre][1], c[p][1]);
}
} T;

void binary_search(int l, int r, int s, int t) {
if (l > r || s > t)
return ;
if (l == r) {
for (int i = s; i <= t; ++i)
if (b[id[i]].id)
res[b[id[i]].id] = mp[l];
return ;
}
int mid = (l + r) >> 1, p1 = 0, p2 = 0, tot = 0;
T.clear();
for (int i = s; i <= t; ++i) {
if (!b[id[i]].id) {
if (b[id[i]].k <= mp[mid]) {
T.modify(b[id[i]].y1, 1, n, T.rt[tot], T.rt[tot + 1]);
tmp1[++p1] = id[i];
pos[++tot] = b[id[i]].x1;
} else {
tmp2[++p2] = id[i];
}
} else {
int x, y, w;
x = std::lower_bound(pos + 1, pos + 1 + tot, b[id[i]].x1) - pos;
y = std::upper_bound(pos + 1, pos + 1 + tot, b[id[i]].x2) - pos - 1;
w = T.query(b[id[i]].y1, b[id[i]].y2, 1, n, T.rt[x - 1], T.rt[y]);
if (w >= b[id[i]].k) {
tmp1[++p1] = id[i];
} else {
b[id[i]].k -= w;
tmp2[++p2] = id[i];
}
}
}
for (int i = 1; i <= p1; ++i)
id[s + i - 1] = tmp1[i];
for (int i = 1; i <= p2; ++i)
id[s + p1 + i - 1] = tmp2[i];
binary_search(l, mid, s, s + p1 - 1);
binary_search(mid + 1, r, s + p1, t);
}

int main() {
//freopen("in", "r", stdin);
n = in(), q = in();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
b[++m] = (info){0, i, j, 0, 0, in()};
mp[++nn] = b[m].k;
}
for (int i = 1; i <= q; ++i)
b[++m] = (info){i, in(), in(), in(), in(), in()};
for (int i = 1; i <= m; ++i)
id[i] = i;
std::sort(mp + 1, mp + 1 + nn);
nn = std::unique(mp + 1, mp + 1 + nn) - mp - 1;
binary_search(0, nn, 1, m);
for (int i = 1; i <= q; ++i)
printf("%d\n", res[i]);
return 0;
}


# T2、Tree

## $Sol$：

$f(x) = \sqrt{ \frac{\sum (C_i - x) ^ 2}{n - 1} }$

## $Source$：

#include <cstdio>
#include <algorithm>
#include <cmath>
typedef double db;
int in() {
int x = 0; char c = getchar(); bool f = 0;
while (c < '0' || c > '9')
f |= c == '-', c = getchar();
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return f ? -x : x;
}
template<typename T>inline void chk_min(T &_, T __) { _ = _ < __ ? _ : __; }
template<typename T>inline void chk_max(T &_, T __) { _ = _ > __ ? _ : __; }

const int N = 105, M = 2005;
const db eps = 1e-5;

db res, now;
int n, m;
int fa[N];
struct edge {
int u, v, w;
inline bool operator < (const edge &b) const {
return (this->w - now) * (this->w - now) < (b.w - now) * (b.w - now);
}
} e[M];

int get_fa(int u) {
return fa[u] == u ? u : fa[u] = get_fa(fa[u]);
}

void calc() {
res = 1 << 30;
for (now = 100; now > 0; now -= 0.25) {
std::sort(e + 1, e + 1 + m);
int now_edge = 0, _sum = 0, sum = 0;
for (int i = 1; i <= n; ++i)
fa[i] = i;
for (int i = 1; i <= m && now_edge < n - 1; ++i) {
int fx = get_fa(e[i].u), fy = get_fa(e[i].v);
if (fx == fy)
continue;
fa[fx] = fy;
++now_edge;
_sum += e[i].w * e[i].w, sum += e[i].w;
}
db tmp = sqrt(((db)(n - 1) * _sum - sum * sum) / (n - 1) / (n - 1));
if (res - tmp > eps)
res = tmp;
}
}

int main() {
//freopen("in", "r", stdin);
n = in(), m = in();
if (n == 1) {
puts("0.0000");
return 0;
}
for (int i = 1; i <= m; ++i)
e[i] = (edge){in(), in(), in()};

calc();

printf("%.4lf\n", res);
return 0;
}


# T3、Points and Segments

## $Sol$：

$l$$r + 1$ 连无向边，$l$$r + 1$ 即为染成蓝色，否则染成红色；

## $Source$：

#include <cstdio>
#include <cstring>
#include <algorithm>
inline int in() {
int x = 0; char c = getchar(); bool f = 0;
while (c < '0' || c > '9')
f |= c == '-', c = getchar();
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return f ? -x : x;
}
template<typename T>inline void chk_min(T &_, T __) { _ = _ < __ ? _ : __; }
template<typename T>inline void chk_max(T &_, T __) { _ = _ > __ ? _ : __; }

const int N = 1e5 + 5;

struct node {
int l, r;
} a[N];
struct edge {
int next, to;
} e[N << 2];
int ecnt = 1, head[N << 1];
int n, nn, mp[N << 1], res[N], deg[N << 1];
bool vis[N << 1], used[N << 1];

inline void jb(const int u, const int v) {
++deg[u], ++deg[v];
}

void prep() {
for (int i = 1; i <= n; ++i) {
a[i] = (node){in(), in()};
mp[++nn] = a[i].l, mp[++nn] = a[i].r;
}
std::sort(mp + 1, mp + 1 + nn);
nn = std::unique(mp + 1, mp + 1 + nn) - mp - 1;
for (int i = 1; i <= n; ++i) {
a[i].l = std::lower_bound(mp + 1, mp + 1 + nn, a[i].l) - mp;
a[i].r = std::lower_bound(mp + 1, mp + 1 + nn, a[i].r) - mp;
jb(a[i].l, a[i].r);
}
int last = 0;
for (int i = 1; i <= nn; ++i)
if (deg[i] & 1) {
if (!last)
last  = i;
else
jb(last, i), last = 0;
}
}

void dfs(const int u) {
used[u] = 1;
for (int i = head[u]; i; i = e[i].next) {
if (!vis[i >> 1]) {
vis[i >> 1] = 1;
dfs(e[i].to);
if (u > e[i].to)
res[i >> 1] = 1;
else
res[i >> 1] = 0;
}
}
}

void work() {
for (int i = 1; i <= nn; ++i)
if (!used[i])
dfs(i);
}

int main() {
//freopen("in", "r", stdin);
n = in();
prep();
work();
for (int i = 1; i <= n; ++i)
printf("%d ", res[i]);
puts("");
return 0;
}

posted @ 2019-08-23 20:15  15owzLy1  阅读(120)  评论(0编辑  收藏