Synchronized BZOJ solutions
parent
dacba453cf
commit
75440536c2
@ -0,0 +1,8 @@
|
||||
#include <cstdio>
|
||||
int main()
|
||||
{
|
||||
int a, b;
|
||||
scanf("%d%d", &a, &b);
|
||||
printf("%d", a + b);
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,96 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <queue>
|
||||
int pathlen[3000001];
|
||||
bool inque[3000001];
|
||||
typedef struct Edge
|
||||
{
|
||||
int to, len;
|
||||
Edge* next;
|
||||
Edge(int t, int l, Edge *n) : to(t), len(l), next(n) {}
|
||||
}*lpEdge;
|
||||
lpEdge G[3000001];
|
||||
#define addEdge(x, y, z) G[x] = new Edge(y, z, G[x])
|
||||
#define addEdge2(x, y, z) addEdge(x, y, z), addEdge(y, x, z)
|
||||
void spfa(int s)
|
||||
{
|
||||
std::queue<int> Q;
|
||||
memset(pathlen, 0x3f, sizeof(pathlen));
|
||||
memset(inque, 0, sizeof(inque));
|
||||
inque[s] = true;
|
||||
pathlen[s] = 0;
|
||||
Q.push(s);
|
||||
while (!Q.empty())
|
||||
{
|
||||
int x = Q.front();
|
||||
Q.pop();
|
||||
inque[x] = false;
|
||||
for (lpEdge cur = G[x]; cur; cur = cur->next)
|
||||
if (pathlen[cur->to] > pathlen[x] + cur->len)
|
||||
{
|
||||
pathlen[cur->to] = pathlen[x] + cur->len;
|
||||
if (!inque[cur->to]) inque[cur->to] = true, Q.push(cur->to);
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n, m;
|
||||
scanf("%d%d", &n, &m);
|
||||
if (n == 1 || m == 1)
|
||||
if (n == 1 && m == 1) putchar(0);
|
||||
else
|
||||
{
|
||||
int ans = 0x3f3f3f3f;
|
||||
for (int i = n > m ? n : m, x; i; i--)
|
||||
{
|
||||
scanf("%d", &x);
|
||||
if (x < ans) ans = x;
|
||||
}
|
||||
printf("%d", ans);
|
||||
}
|
||||
else
|
||||
{
|
||||
int target = (n - 1) * (m - 1) * 2 + 1, x;
|
||||
for (int i = 1; i < m; i++)
|
||||
{
|
||||
scanf("%d", &x);
|
||||
addEdge2(2 * i, target, x);
|
||||
}
|
||||
for (int i = 1; i < n - 1; i++)
|
||||
for (int j = 1; j < m; j++)
|
||||
{
|
||||
scanf("%d", &x);
|
||||
addEdge2((i - 1) * ((m - 1) << 1) + j * 2 - 1,
|
||||
i * ((m - 1) << 1) + j * 2, x);
|
||||
}
|
||||
for (int i = 1; i < m; i++)
|
||||
{
|
||||
scanf("%d", &x);
|
||||
addEdge2(0, (n - 2) * ((m - 1) << 1) + i * 2 - 1, x);
|
||||
}
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
scanf("%d", &x);
|
||||
addEdge2(0, (i - 1) * ((m - 1) << 1) + 1, x);
|
||||
for (int j = 1; j < m - 1; j++)
|
||||
{
|
||||
scanf("%d", &x);
|
||||
addEdge2((i - 1) * ((m - 1) << 1) + j * 2,
|
||||
(i - 1) * ((m - 1) << 1) + j * 2 + 1, x);
|
||||
}
|
||||
scanf("%d", &x);
|
||||
addEdge2(target, i * ((m - 1) << 1), x);
|
||||
}
|
||||
for (int i = 1; i < n; i++)
|
||||
for (int j = 1; j < m; j++)
|
||||
{
|
||||
scanf("%d", &x);
|
||||
addEdge2((i - 1) * ((m - 1) << 1) + (j - 1) * 2 + 1,
|
||||
(i - 1) * ((m - 1) << 1) + (j - 1) * 2 + 2, x);
|
||||
}
|
||||
spfa(0);
|
||||
printf("%d", pathlen[target]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
inline double min(double a, double b) { return a < b ? a : b; }
|
||||
int a, b, n;
|
||||
double g[11][11], sum[11][11], f[11][11][11][11][11], avg;
|
||||
double dfs(int x1, int y1, int x2, int y2, int v)
|
||||
{
|
||||
if (f[x1][y1][x2][y2][v] < f[0][0][0][0][0] - 100) return f[x1][y1][x2][y2][v];
|
||||
else if (v == 0) return f[x1][y1][x2][y2][v] =
|
||||
(sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1] - avg)*
|
||||
(sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1] - avg);
|
||||
else
|
||||
{
|
||||
double ans = *****f;
|
||||
for (int i = x1; i < x2; i++)
|
||||
for (int j = 0; j < v; j++)
|
||||
ans = min(ans, dfs(x1, y1, i, y2, j) + dfs(i + 1, y1, x2, y2, v - j - 1));
|
||||
for (int i = y1; i < y2; i++)
|
||||
for (int j = 0; j < v; j++)
|
||||
ans = min(ans, dfs(x1, y1, x2, i, j) + dfs(x1, i + 1, x2, y2, v - j - 1));
|
||||
return f[x1][y1][x2][y2][v] = ans;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
memset(f, 0x7f, sizeof(f));
|
||||
scanf("%d%d%d", &a, &b, &n);
|
||||
for (int i = 1; i <= a; i++)
|
||||
for (int j = 1; j <= b; j++)
|
||||
scanf("%lf", &g[i][j]);
|
||||
for (int i = 1; i <= a; i++)
|
||||
for (int j = 1; j <= b; j++)
|
||||
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + g[i][j];
|
||||
avg = sum[a][b] / n;
|
||||
printf("%.2f", sqrt(dfs(1, 1, a, b, n - 1) / n));
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
int main()
|
||||
{
|
||||
int m;
|
||||
scanf("%d", &m);
|
||||
printf("%d", (int)log2(m) + 1);
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,190 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#define sz(x) (x == 0 ? 0 :x->size)
|
||||
#define update(x) if(x) x->size = sz(x->child[0]) + sz(x->child[1]) + 1
|
||||
#define getVal(x) (x == 0 ? 0 : x->val)
|
||||
typedef int valType;
|
||||
struct Node
|
||||
{
|
||||
valType val;
|
||||
int size;
|
||||
Node *child[2], *father;
|
||||
};
|
||||
typedef Node* lpNode;
|
||||
struct allocator
|
||||
{
|
||||
private:
|
||||
int i;
|
||||
Node* data;
|
||||
lpNode* pt;
|
||||
public:
|
||||
allocator(int size)
|
||||
{
|
||||
data = new Node[size];
|
||||
pt = new lpNode[size];
|
||||
this->i = 0;
|
||||
for (int j = 0; j < size; j++) pt[j] = &data[j];
|
||||
}
|
||||
lpNode alloc(int x, lpNode f)
|
||||
{
|
||||
lpNode newNode = pt[i++];
|
||||
newNode->val = x;
|
||||
newNode->father = f;
|
||||
newNode->size = 1;
|
||||
newNode->child[0] = newNode->child[1] = 0;
|
||||
return newNode;
|
||||
}
|
||||
void free(lpNode ptr) { pt[--i] = ptr; }
|
||||
};
|
||||
|
||||
lpNode root = 0;
|
||||
allocator* mem;
|
||||
struct SplayTree
|
||||
{
|
||||
private:
|
||||
void rotate(lpNode x)
|
||||
{
|
||||
if (x == 0 || x->father == 0) return;
|
||||
int d = (x == x->father->child[0]);
|
||||
lpNode y = x->father;
|
||||
y->child[!d] = x->child[d];
|
||||
if (x->child[d]) x->child[d]->father = y;
|
||||
x->father = y->father;
|
||||
if (y->father) y->father->child[y == y->father->child[1]] = x;
|
||||
y->father = x;
|
||||
x->child[d] = y;
|
||||
update(y);
|
||||
update(x);
|
||||
}
|
||||
void Splay(lpNode x, lpNode& target)
|
||||
{
|
||||
lpNode targetFather = target->father;
|
||||
while (x->father != targetFather)
|
||||
if (x->father == target) rotate(x);
|
||||
else
|
||||
if ((x->father->father->child[0] == x->father) == (x->father->child[0] == x))
|
||||
rotate(x->father), rotate(x);
|
||||
else
|
||||
rotate(x), rotate(x);
|
||||
target = x;
|
||||
}
|
||||
lpNode find(int x)
|
||||
{
|
||||
lpNode s = root;
|
||||
while (true)
|
||||
if (s == 0) return 0;
|
||||
else if (s->val == x)
|
||||
{
|
||||
Splay(s, root);
|
||||
return s;
|
||||
}
|
||||
else s = s->child[x > s->val];
|
||||
}
|
||||
lpNode join(lpNode x, lpNode y)
|
||||
{
|
||||
if (x == 0) return y;
|
||||
else
|
||||
{
|
||||
lpNode m = max_min(x, 1);
|
||||
Splay(m, x);
|
||||
m->child[1] = y;
|
||||
if (y) y->father = m;
|
||||
return x;
|
||||
}
|
||||
}
|
||||
public:
|
||||
SplayTree(int _) { mem = new allocator(_); }
|
||||
int size() { return sz(root); }
|
||||
lpNode max_min(lpNode x, int i)
|
||||
{
|
||||
while (x&&x->child[i]) x = x->child[i];
|
||||
return x;
|
||||
}
|
||||
void insert(int x)
|
||||
{
|
||||
if (root == 0)
|
||||
root = mem->alloc(x, 0);
|
||||
else
|
||||
{
|
||||
lpNode s = root, p = 0;
|
||||
while (s)
|
||||
p = s, s = s->child[x > s->val];
|
||||
s = mem->alloc(x, p);
|
||||
p->child[x > p->val] = s;
|
||||
Splay(s, root);
|
||||
}
|
||||
}
|
||||
void remove(int x)
|
||||
{
|
||||
lpNode p = find(x);
|
||||
if (p)
|
||||
{
|
||||
root = join(p->child[0], p->child[1]);
|
||||
if(root) root->father = 0;
|
||||
mem->free(p);
|
||||
}
|
||||
}
|
||||
lpNode pred(int x)
|
||||
{
|
||||
lpNode s = root, ans = 0;
|
||||
while (s)
|
||||
if (s->val < x)
|
||||
ans = s, s = s->child[1];
|
||||
else
|
||||
s = s->child[0];
|
||||
return ans;
|
||||
}
|
||||
lpNode succ(int x)
|
||||
{
|
||||
lpNode s = root, ans = 0;
|
||||
while (s)
|
||||
if (s->val > x)
|
||||
ans = s, s = s->child[0];
|
||||
else
|
||||
s = s->child[1];
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
int main()
|
||||
{
|
||||
SplayTree st(10001);
|
||||
int n, a, b, type, ans = 0;
|
||||
scanf("%d", &n);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d%d", &a, &b);
|
||||
if (st.size() == 0) type = a;
|
||||
if (a == type) st.insert(b);
|
||||
else
|
||||
{
|
||||
lpNode pr = st.pred(b), su = st.succ(b);
|
||||
if (pr == 0)
|
||||
{
|
||||
ans += su->val - b;
|
||||
st.remove(su->val);
|
||||
}
|
||||
else if (su == 0)
|
||||
{
|
||||
ans += b - pr->val;
|
||||
st.remove(pr->val);
|
||||
}
|
||||
else
|
||||
{
|
||||
int d1 = su->val - b, d2 = b - pr->val;
|
||||
if (d1 < d2)
|
||||
{
|
||||
ans += d1;
|
||||
st.remove(su->val);
|
||||
}
|
||||
else
|
||||
{
|
||||
ans += d2;
|
||||
st.remove(pr->val);
|
||||
}
|
||||
}
|
||||
}
|
||||
ans %= 1000000;
|
||||
}
|
||||
printf("%d", ans);
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
#include<cstdio>
|
||||
int main()
|
||||
{
|
||||
int n, k;
|
||||
scanf("%d%d", &n, &k);
|
||||
printf("%d", (n == 1) ? 1 : (k < n - k + 1 ? k : n - k + 1) << 1);
|
||||
}
|
||||
@ -0,0 +1,126 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
static const int arrSize = 40000;
|
||||
typedef int valType;
|
||||
struct Node
|
||||
{
|
||||
valType val;
|
||||
Node *child[2], *father;
|
||||
};
|
||||
typedef Node* lpNode;
|
||||
struct allocator
|
||||
{
|
||||
private:
|
||||
int i;
|
||||
Node data[arrSize];
|
||||
lpNode pt[arrSize];
|
||||
public:
|
||||
allocator()
|
||||
{
|
||||
this->i = 0;
|
||||
for (int j = 0; j < arrSize; j++) pt[j] = data + j;
|
||||
}
|
||||
lpNode alloc() { return pt[i++]; }
|
||||
void free(lpNode ptr) { pt[--i] = ptr; }
|
||||
} mem;
|
||||
void rotate(lpNode x, int direction)
|
||||
{
|
||||
lpNode p = x->father;
|
||||
p->child[!direction] = x->child[direction];
|
||||
if (x->child[direction]) x->child[direction]->father = p;
|
||||
x->father = p->father;
|
||||
if (p->father) p->father->child[p != p->father->child[0]] = x;
|
||||
p->father = x;
|
||||
x->child[direction] = p;
|
||||
}
|
||||
void Splay(lpNode x, lpNode target)
|
||||
{
|
||||
lpNode p;
|
||||
target = target->father;
|
||||
while (x->father != target)
|
||||
{
|
||||
p = x->father;
|
||||
if (p->father == target)
|
||||
{
|
||||
rotate(x, x == p->child[0]);
|
||||
break;
|
||||
}
|
||||
else
|
||||
if (x == p->child[0])
|
||||
if (p == p->father->child[0]) { rotate(p, 1); rotate(x, 1); }
|
||||
else { rotate(x, 1); rotate(x, 0); }
|
||||
else
|
||||
if (p == p->father->child[1]) { rotate(p, 0); rotate(x, 0); }
|
||||
else { rotate(x, 0); rotate(x, 1); }
|
||||
}
|
||||
}
|
||||
lpNode max(lpNode x)
|
||||
{
|
||||
while (x && x->child[1]) x = x->child[1];
|
||||
return x;
|
||||
}
|
||||
lpNode min(lpNode x)
|
||||
{
|
||||
while (x && x->child[0]) x = x->child[0];
|
||||
return x;
|
||||
}
|
||||
lpNode find(valType x, lpNode s)
|
||||
{
|
||||
lpNode p = s;
|
||||
while (true)
|
||||
if (p == 0) return p;
|
||||
else if (p->val == x) { Splay(p, s); return p; }
|
||||
else p = p->child[x > p->val];
|
||||
}
|
||||
lpNode pred(valType x, lpNode s) { return max(find(x, s)->child[0]); }
|
||||
lpNode succ(valType x, lpNode s) { return min(find(x, s)->child[1]); }
|
||||
void ins(valType x, lpNode& s)
|
||||
{
|
||||
if (s == 0)
|
||||
{
|
||||
s = mem.alloc();
|
||||
s->val = x;
|
||||
}
|
||||
else
|
||||
{
|
||||
lpNode p = 0, _s = s;
|
||||
while (s)
|
||||
{
|
||||
p = s;
|
||||
s = s->child[x > s->val];
|
||||
}
|
||||
if (p == 0) s->val = x;
|
||||
else
|
||||
{
|
||||
lpNode n = mem.alloc();
|
||||
n->val = x;
|
||||
n->father = p;
|
||||
p->child[x > p->val] = n;
|
||||
s = _s;
|
||||
Splay(n, s);
|
||||
s = n;
|
||||
}
|
||||
}
|
||||
}
|
||||
valType _min(valType a, valType b) { return (a < b ? a : b); }
|
||||
lpNode root = 0;
|
||||
int main()
|
||||
{
|
||||
int n, tmp, ans = 0;
|
||||
scanf("%d", &n);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d", &tmp);
|
||||
ins(tmp, root);
|
||||
if (i == 0) ans += tmp;
|
||||
else
|
||||
{
|
||||
lpNode p = pred(tmp, root), s = succ(tmp, root);
|
||||
if (p == 0) ans += abs(tmp - s->val);
|
||||
else if (s == 0) ans += abs(tmp - p->val);
|
||||
else ans += _min(abs(tmp - s->val), abs(tmp - p->val));
|
||||
}
|
||||
}
|
||||
printf("%d", ans);
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
#include <cstdio>
|
||||
int m, n;
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
for (int i = 1; i <= n; i++) m += n / i;
|
||||
printf("%d", m);
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
#include <cstdio>
|
||||
int n, x, t, tot;
|
||||
int main()
|
||||
{
|
||||
scanf("%d", &n);
|
||||
while (n--)
|
||||
{
|
||||
scanf("%d", &x);
|
||||
if (tot == 0) { t = x; tot = 1; }
|
||||
else if (t == x) tot++;
|
||||
else tot--;
|
||||
}
|
||||
printf("%d", t);
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
#include <cstdio>
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while (scanf("%d", &n) && n)
|
||||
puts(n & 1 ? "Bob" : "Alice");
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,122 @@
|
||||
#include <cstdio>
|
||||
inline int max(int a, int b) { return a > b ? a : b; }
|
||||
const int maxn = 100005;
|
||||
int val[maxn], lch[maxn], rch[maxn], sz[maxn], cap[maxn], arr[maxn], aend, root, pool[maxn], idx = 1;
|
||||
bool exist[maxn];
|
||||
inline int newNode() { return pool[idx++]; }
|
||||
inline void delNode(int x) { pool[--idx] = x; }
|
||||
inline void setNode(int x, int v = 0) { val[x] = v, lch[x] = rch[x] = 0, exist[x] = true, sz[x] = cap[x] = 1; }
|
||||
inline bool isBad(int x)
|
||||
{
|
||||
return (sz[x] + 5 < cap[x] >> 1) ||
|
||||
(max(sz[lch[x]], sz[rch[x]]) - 5 > (sz[x] * 3) >> 2);
|
||||
}
|
||||
inline void update(int x)
|
||||
{
|
||||
sz[x] = sz[lch[x]] + sz[rch[x]] + exist[x];
|
||||
cap[x] = cap[lch[x]] + cap[rch[x]] + 1;
|
||||
}
|
||||
void rebuild_impl(int x)
|
||||
{
|
||||
if (x == 0) return;
|
||||
rebuild_impl(lch[x]);
|
||||
if (exist[x]) arr[aend++] = val[x];
|
||||
rebuild_impl(rch[x]);
|
||||
delNode(x);
|
||||
}
|
||||
void rebuild_impl(int& x, int b, int e)
|
||||
{
|
||||
if (b < e)
|
||||
{
|
||||
setNode(x = newNode(), 0);
|
||||
int m = (b + e) >> 1;
|
||||
val[x] = arr[m];
|
||||
rebuild_impl(lch[x], b, m);
|
||||
rebuild_impl(rch[x], m + 1, e);
|
||||
update(x);
|
||||
}
|
||||
}
|
||||
inline void rebuild(int& x)
|
||||
{
|
||||
if (x == 0) return;
|
||||
aend = 0;
|
||||
rebuild_impl(x);
|
||||
rebuild_impl(x, 0, aend);
|
||||
}
|
||||
int rnk(int x)
|
||||
{
|
||||
int cur = root, ans = 1;
|
||||
while (cur)
|
||||
if (x <= val[cur])
|
||||
cur = lch[cur];
|
||||
else
|
||||
ans += sz[lch[cur]] + exist[cur], cur = rch[cur];
|
||||
return ans;
|
||||
}
|
||||
int kth(int x)
|
||||
{
|
||||
int cur = root;
|
||||
while (cur)
|
||||
{
|
||||
if (sz[lch[cur]] + 1 == x && exist[cur]) break;
|
||||
else if (sz[lch[cur]] >= x) cur = lch[cur];
|
||||
else x -= sz[lch[cur]] + exist[cur], cur = rch[cur];
|
||||
}
|
||||
return val[cur];
|
||||
}
|
||||
int* insert_impl(int& node, int x)
|
||||
{
|
||||
int* res = 0;
|
||||
if (node == 0) setNode(node = newNode(), x);
|
||||
else
|
||||
{
|
||||
res = insert_impl(x < val[node] ? lch[node] : rch[node], x);
|
||||
update(node);
|
||||
if (isBad(node)) res = &node;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
int* delete_impl(int& node, int x)
|
||||
{
|
||||
if (node == 0) return 0;
|
||||
int* ret = 0;
|
||||
sz[node]--;
|
||||
int pos = sz[lch[node]] + exist[node];
|
||||
if (pos == x&&exist[node]) exist[node] = false;
|
||||
else
|
||||
{
|
||||
ret = x <= pos ? delete_impl(lch[node], x) : delete_impl(rch[node], x - pos);
|
||||
update(node);
|
||||
if (isBad(node)) ret = &node;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
void ins(int x)
|
||||
{
|
||||
int* ret = insert_impl(root, x);
|
||||
if (ret) rebuild(*ret);
|
||||
}
|
||||
void del(int x)
|
||||
{
|
||||
int rk = rnk(x);
|
||||
if (x != kth(rk)) return;
|
||||
int* ret = delete_impl(root, rk);
|
||||
if (ret) rebuild(*ret);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
for (int i = 0; i < maxn; i++) pool[i] = i;
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
for (int i = 0, op, x; i < n; i++)
|
||||
{
|
||||
scanf("%d%d", &op, &x);
|
||||
if (op == 1) ins(x);
|
||||
if (op == 2) del(x);
|
||||
if (op == 3) printf("%d\n", rnk(x));
|
||||
if (op == 4) printf("%d\n", kth(x));
|
||||
if (op == 5) printf("%d\n", kth(rnk(x) - 1));
|
||||
if (op == 6) printf("%d\n", kth(rnk(x + 1)));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,201 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#define sz(x) (x == 0 ? 0 :x->size)
|
||||
#define update(x) if(x) x->size = sz(x->child[0]) + sz(x->child[1]) + 1
|
||||
#define getVal(x) (x == 0 ? 0 : x->val)
|
||||
typedef int valType;
|
||||
const int arrSize = 100005;
|
||||
struct Node
|
||||
{
|
||||
valType val;
|
||||
int size;
|
||||
Node *child[2], *father;
|
||||
};
|
||||
typedef Node* lpNode;
|
||||
struct allocator
|
||||
{
|
||||
private:
|
||||
int i;
|
||||
Node data[arrSize];
|
||||
lpNode pt[arrSize];
|
||||
public:
|
||||
allocator(int size)
|
||||
{
|
||||
this->i = 0;
|
||||
for (int j = 0; j < size; j++) pt[j] = data + j;
|
||||
}
|
||||
lpNode alloc(int x, lpNode f)
|
||||
{
|
||||
lpNode newNode = pt[i++];
|
||||
newNode->val = x;
|
||||
newNode->father = f;
|
||||
newNode->size = 1;
|
||||
newNode->child[0] = newNode->child[1] = 0;
|
||||
return newNode;
|
||||
}
|
||||
void free(lpNode ptr) { pt[--i] = ptr; }
|
||||
};
|
||||
|
||||
lpNode root = 0;
|
||||
allocator* mem;
|
||||
struct SplayTree
|
||||
{
|
||||
private:
|
||||
void rotate(lpNode x)
|
||||
{
|
||||
if (x == 0 || x->father == 0) return;
|
||||
int d = (x == x->father->child[0]);
|
||||
lpNode y = x->father;
|
||||
y->child[!d] = x->child[d];
|
||||
if (x->child[d]) x->child[d]->father = y;
|
||||
x->father = y->father;
|
||||
if (y->father) y->father->child[y == y->father->child[1]] = x;
|
||||
y->father = x;
|
||||
x->child[d] = y;
|
||||
update(y);
|
||||
update(x);
|
||||
}
|
||||
void Splay(lpNode x, lpNode& target)
|
||||
{
|
||||
lpNode targetFather = target->father;
|
||||
while (x->father != targetFather)
|
||||
if (x->father == target) rotate(x);
|
||||
else
|
||||
if ((x->father->father->child[0] == x->father) == (x->father->child[0] == x))
|
||||
rotate(x->father), rotate(x);
|
||||
else
|
||||
rotate(x), rotate(x);
|
||||
target = x;
|
||||
}
|
||||
lpNode find(int x)
|
||||
{
|
||||
lpNode s = root;
|
||||
while (true)
|
||||
if (s == 0) return 0;
|
||||
else if (s->val == x)
|
||||
{
|
||||
Splay(s, root);
|
||||
return s;
|
||||
}
|
||||
else s = s->child[x > s->val];
|
||||
}
|
||||
lpNode join(lpNode x, lpNode y)
|
||||
{
|
||||
if (x == 0) return y;
|
||||
else
|
||||
{
|
||||
lpNode m = max_min(x, 1);
|
||||
Splay(m, x);
|
||||
m->child[1] = y;
|
||||
if (y) y->father = m;
|
||||
return x;
|
||||
}
|
||||
}
|
||||
public:
|
||||
SplayTree(int _) { mem = new allocator(_); }
|
||||
lpNode max_min(lpNode x, int i)
|
||||
{
|
||||
while (x&&x->child[i]) x = x->child[i];
|
||||
return x;
|
||||
}
|
||||
void insert(int x)
|
||||
{
|
||||
if (root == 0)
|
||||
root = mem->alloc(x, 0);
|
||||
else
|
||||
{
|
||||
lpNode s = root, p = 0;
|
||||
while (s)
|
||||
p = s, s = s->child[x > s->val];
|
||||
s = mem->alloc(x, p);
|
||||
p->child[x > p->val] = s;
|
||||
Splay(s, root);
|
||||
}
|
||||
}
|
||||
void remove(int x)
|
||||
{
|
||||
lpNode p = find(x);
|
||||
if (p)
|
||||
{
|
||||
root = join(p->child[0], p->child[1]);
|
||||
root->father = 0;
|
||||
mem->free(p);
|
||||
}
|
||||
}
|
||||
int rank(int x)
|
||||
{
|
||||
lpNode s = root;
|
||||
int ans = 0;
|
||||
while (s)
|
||||
if (x <= s->val)
|
||||
s = s->child[0];
|
||||
else
|
||||
ans = ans + 1 + sz(s->child[0]), s = s->child[1];
|
||||
return ans + 1;
|
||||
}
|
||||
lpNode select(int x)
|
||||
{
|
||||
lpNode s = root;
|
||||
while (s)
|
||||
if (x == sz(s->child[0]) + 1) return s;
|
||||
else if (x <= sz(s->child[0])) s = s->child[0];
|
||||
else
|
||||
x = x - 1 - sz(s->child[0]), s = s->child[1];
|
||||
return 0;
|
||||
}
|
||||
lpNode pred(int x)
|
||||
{
|
||||
lpNode s = root, ans = 0;
|
||||
while (s)
|
||||
if (s->val < x)
|
||||
ans = s, s = s->child[1];
|
||||
else
|
||||
s = s->child[0];
|
||||
return ans;
|
||||
}
|
||||
lpNode succ(int x)
|
||||
{
|
||||
lpNode s = root, ans = 0;
|
||||
while (s)
|
||||
if (s->val > x)
|
||||
ans = s, s = s->child[0];
|
||||
else
|
||||
s = s->child[1];
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
SplayTree st(arrSize);
|
||||
int n, op, num;
|
||||
scanf("%d", &n);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d%d", &op, &num);
|
||||
switch (op)
|
||||
{
|
||||
case 1:
|
||||
st.insert(num);
|
||||
break;
|
||||
case 2:
|
||||
st.remove(num);
|
||||
break;
|
||||
case 3:
|
||||
printf("%d\n", st.rank(num));
|
||||
break;
|
||||
case 4:
|
||||
printf("%d\n", getVal(st.select(num)));
|
||||
break;
|
||||
case 5:
|
||||
printf("%d\n", getVal(st.pred(num)));
|
||||
break;
|
||||
case 6:
|
||||
printf("%d\n", getVal(st.succ(num)));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,142 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cassert>
|
||||
#define sz(x) ((x) == 0 ? 0 : (x)->sz)
|
||||
template<typename T>
|
||||
struct Allocator
|
||||
{
|
||||
int idx;
|
||||
T *data;
|
||||
T **ptr;
|
||||
Allocator(int size)
|
||||
{
|
||||
idx = 0;
|
||||
data = new T[size];
|
||||
ptr = new T*[size];
|
||||
for (int i = 0; i < size; i++) ptr[i] = &data[i];
|
||||
}
|
||||
~Allocator()
|
||||
{
|
||||
delete[] data;
|
||||
delete[] ptr;
|
||||
}
|
||||
T* alloc() { return ptr[idx++]; }
|
||||
void free(T* pt) { ptr[--idx] = pt; }
|
||||
};
|
||||
struct Treap
|
||||
{
|
||||
typedef struct Node
|
||||
{
|
||||
int val, pri, sz;
|
||||
Node *ch[2];
|
||||
void Init(int x = 0)
|
||||
{
|
||||
pri = rand();
|
||||
ch[0] = ch[1] = 0;
|
||||
val = x;
|
||||
sz = 1;
|
||||
}
|
||||
void update()
|
||||
{
|
||||
sz = 1 + sz(ch[0]) + sz(ch[1]);
|
||||
}
|
||||
}*lpNode;
|
||||
Treap(int max_size, int _seed)
|
||||
{
|
||||
srand(_seed);
|
||||
alloc = new Allocator<Node>(max_size);
|
||||
root = 0;
|
||||
}
|
||||
void ins(int x)
|
||||
{
|
||||
insert(root, x);
|
||||
}
|
||||
void del(int x)
|
||||
{
|
||||
remove(root, x);
|
||||
}
|
||||
int rnk(int x)
|
||||
{
|
||||
int ans = 1;
|
||||
lpNode cur = root;
|
||||
while (cur)
|
||||
if (x <= cur->val) cur = cur->ch[0];
|
||||
else
|
||||
ans += sz(cur->ch[0]) + 1, cur = cur->ch[1];
|
||||
return ans;
|
||||
}
|
||||
int kth(int x)
|
||||
{
|
||||
lpNode cur = root;
|
||||
while (cur)
|
||||
if (x == sz(cur->ch[0]) + 1) return cur->val;
|
||||
else if (x <= sz(cur->ch[0])) cur = cur->ch[0];
|
||||
else
|
||||
x -= sz(cur->ch[0]) + 1,
|
||||
cur = cur->ch[1];
|
||||
return 0;
|
||||
}
|
||||
private:
|
||||
Allocator<Node> *alloc;
|
||||
lpNode root;
|
||||
void rotate(lpNode &node, int d)
|
||||
{
|
||||
if (node == 0) return;
|
||||
lpNode ch = node->ch[d ^ 1];
|
||||
if (ch)
|
||||
{
|
||||
node->ch[d ^ 1] = ch->ch[d];
|
||||
ch->ch[d] = node;
|
||||
node = ch;
|
||||
node->ch[d]->update();
|
||||
node->update();
|
||||
}
|
||||
}
|
||||
void insert(lpNode &node, int x)
|
||||
{
|
||||
if (node == 0) (node = alloc->alloc())->Init(x);
|
||||
else
|
||||
{
|
||||
int cmp = x < node->val;
|
||||
insert(node->ch[cmp ^ 1], x);
|
||||
node->update();
|
||||
if (node->ch[cmp ^ 1]->pri < node->pri) rotate(node, cmp);
|
||||
}
|
||||
}
|
||||
void remove(lpNode &node, int x)
|
||||
{
|
||||
if (node == 0) return;
|
||||
else
|
||||
{
|
||||
if (x < node->val) remove(node->ch[0], x);
|
||||
else if (x > node->val) remove(node->ch[1], x);
|
||||
else
|
||||
if (node->ch[0] == 0) node = node->ch[1];
|
||||
else if (node->ch[1] == 0) node = node->ch[0];
|
||||
else
|
||||
{
|
||||
int d = node->ch[0]->pri < node->ch[1]->pri;
|
||||
rotate(node, d);
|
||||
remove(node->ch[d], x);
|
||||
}
|
||||
if (node) node->update();
|
||||
}
|
||||
}
|
||||
};
|
||||
int main()
|
||||
{
|
||||
Treap T(100005, 3224);
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
for (int i = 0, op, x; i < n; i++)
|
||||
{
|
||||
scanf("%d%d", &op, &x);
|
||||
if (op == 1) T.ins(x);
|
||||
if (op == 2) T.del(x);
|
||||
if (op == 3) printf("%d\n", T.rnk(x));
|
||||
if (op == 4) printf("%d\n", T.kth(x));
|
||||
if (op == 5) printf("%d\n", T.kth(T.rnk(x) - 1));
|
||||
if (op == 6) printf("%d\n", T.kth(T.rnk(x + 1)));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
inline double min(double a, double b) { return a < b ? a : b; }
|
||||
int a, b, n;
|
||||
double g[11][11], sum[11][11], f[11][11][11][11][11], avg;
|
||||
double dfs(int x1, int y1, int x2, int y2, int v)
|
||||
{
|
||||
if (f[x1][y1][x2][y2][v] < f[0][0][0][0][0] - 100) return f[x1][y1][x2][y2][v];
|
||||
else if (v == 0) return f[x1][y1][x2][y2][v] =
|
||||
(sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1] - avg)*
|
||||
(sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1] - avg);
|
||||
else
|
||||
{
|
||||
double ans = *****f;
|
||||
for (int i = x1; i < x2; i++)
|
||||
for (int j = 0; j < v; j++)
|
||||
ans = min(ans, dfs(x1, y1, i, y2, j) + dfs(i + 1, y1, x2, y2, v - j - 1));
|
||||
for (int i = y1; i < y2; i++)
|
||||
for (int j = 0; j < v; j++)
|
||||
ans = min(ans, dfs(x1, y1, x2, i, j) + dfs(x1, i + 1, x2, y2, v - j - 1));
|
||||
return f[x1][y1][x2][y2][v] = ans;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
memset(f, 0x7f, sizeof(f));
|
||||
scanf("%d%d%d", &a, &b, &n);
|
||||
for (int i = 1; i <= a; i++)
|
||||
for (int j = 1; j <= b; j++)
|
||||
scanf("%lf", &g[i][j]);
|
||||
for (int i = 1; i <= a; i++)
|
||||
for (int j = 1; j <= b; j++)
|
||||
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + g[i][j];
|
||||
avg = sum[a][b] / n;
|
||||
printf("%.2f", sqrt(dfs(1, 1, a, b, n - 1) / n));
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in new issue