世纪难题:
我的 :
# include <bits/stdc++.h>
# define LF putchar('\n')
# define SP putchar(' ')
# define rep(i, a, b) for(int i = (a); i <= (b); i++)
# define rep_(i, a, b) for(int i = (a); i >= (b); i--)
# define repi(i, b) for(; (i) <= (b); (i)++)
# define repi_(i, b) for(; (i) >= (b); (i)--)
# define paix(a, n) sort((a) + 1, (a) + 1 + (n));
# define BUG(z) cout << endl << (z) << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<long long, long long> pii;
const int N1 = 1e6 + 5, N2 = 1e3 + 5, M1 = 1e9 + 7, M2 = 998244353, INF1 = 0x7fffffff, INF2 = 0x3f3f3f3f;
const ll INF = 9223372036854775807;
template <typename T>
void read(T& x) { x = 0; char ch = getchar(); ll f = 1; while (!isdigit(ch)) { if (ch == '-') f *= -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getchar(); } x *= f; }
template <typename T, typename... Args>
void read(T& first, Args&... args) { read(first); read(args...); }
template <typename T>
void write(T arg) { T x = arg; if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); }
template <typename T, typename... Ts>
void write(T arg, Ts... args) { write(arg); if (sizeof...(args) != 0) { putchar(' '); write(args...); } }
void CLOSE() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
struct node {
int l, r;
char val[25], valx[25], valy[25];
int len, lenx, leny;
} ;
node tree[2 << 2];
# define tr tree[rt]
# define L (rt << 1)
# define R (rt << 1 | 1)
int f[3];
ll dis[3], c[3], t_ = 1, tt_ = 1;
typedef struct LNode {
ll data;
struct LNode *nxt;
} LNode, *LinkList;
void inputb(char *b) {
b[0] = getchar();
while(!isdigit(b[0])) {
if (b[0] == '-')
tt_ = -1;
b[0] = getchar();
}
int cnt = 1;
b[cnt] = getchar();
while(isdigit(b[cnt]))
b[++cnt] = getchar();
b[cnt] = '\0';
}
void pushup(int rt) {
rep(i, 0, tree[L].len)
tr.valx[i] = (tree[L].val[i] - '0') * t_ + '0';
rep(i, 0, tree[R].len)
tr.valy[i] = (tree[R].val[i] - '0') * tt_ + '0';
tr.lenx = tree[L].len, tr.leny = tree[R].len;
char x[25], y[25];
rep(i, 0, tr.lenx)
x[tr.lenx - i] = tr.valx[i];
rep(i, 0, tr.leny)
y[tr.leny - i] = tr.valy[i];
int maxn = max(tr.lenx, tr.leny) + 1;
if (maxn > 9) maxn = 9;
char z[25];
int t = 0;
rep(i, 0, maxn) {
if (!isdigit(x[i]))
x[i] = '0';
if (!isdigit(y[i]))
y[i] = '0';
z[i] = x[i] + y[i] - '0' + t;
if (z[i] > '9')
t = 1, z[i] -= 10;
else if (z[i] < '0')
t = -1, z[i] += 10;
else
t = 0;
}
while(z[maxn] == '0')
maxn--;
rep(i, 0, maxn)
tr.val[maxn - i] = z[i];
tr.len = maxn;
}
void build(int rt, int l, int r) {
tr.l = l, tr.r = r;
if (l == r)
return ;
int mid = l + r >> 1;
build(L, l, mid);
build(R, mid + 1, r);
}
void update(int rt, int x, char v[]) {
if (tr.l == tr.r) {
tr.len = strlen(v) - 1;
rep(i, 0, tr.len) tr.val[i] = v[i];
return ;
}
int mid = tr.l + tr.r >> 1;
if (x <= mid) update(L, x, v);
else update(R, x, v);
pushup(rt);
}
pii query(int rt) {
ll t = 1;
pii p = make_pair(0, 0);
rep_(i, tr.lenx, 0)
p.first += t * (tr.valx[i] - '0'), t *= 10;
t = 1;
rep_(i, tr.leny, 0)
p.second += t * (tr.valy[i] - '0'), t *= 10;
return p;
}
void update_c(int x, ll y) {
for(int i = x; i <= 2; i += (i & (-i)))
c[i] += y;
for(int i = x + 1; i <= 2; i += (i & (-i)))
c[i] -= y;
}
ll getsum(int x) {
ll ans = 0;
for(int i = x; i >= 1; i -= (i & (-i)))
ans += c[i];
return ans;
}
void InitList_L(LinkList &l) {
l = new LNode;
l -> nxt = NULL;
}
void CreateList_H(LinkList l, int a) {
LinkList x = new LNode;
x -> data = a, x -> nxt = l -> nxt;
l -> nxt = x;
}
void CreateList_R(LinkList l, int a) {
while(l -> nxt != NULL)
l = l -> nxt;
LinkList x = new LNode;
x -> nxt = NULL, x -> data = a, l -> nxt = x;
}
ll Listplus_L(LinkList l) {
ll ans = 0;
while(l -> nxt != NULL)
l = l -> nxt, ans += l -> data;
return ans;
}
void LNode_a_b(ll &a, ll b) {
LinkList l;
InitList_L(l);
CreateList_H(l, a);
CreateList_R(l, b);
a = Listplus_L(l);
}
void finit(int n) {
rep(i, 1, n)
f[i] = i;
}
int Find(int x) {
if (x == f[x])
return x;
int rt = Find(f[x]);
LNode_a_b(dis[x], dis[rt]);
return f[x] = rt;
}
void merge(int x, int y) {
int xx = Find(x), yy = Find(y);
if (xx != yy)
f[xx] = yy;
}
signed main() {
char a[12], b[12];
scanf("%s", a);
if (a[0] == '-') {
t_ = -1;
int l = strlen(a) - 1;
rep(i, 0, l)
a[i] = a[i + 1];
}
inputb(b);
if (a[0] == '0' && b[0] == '0'){
putchar('0');
LF;
return 0;
}
build(1, 1, 2);
update(1, 1, a);
update(1, 2, b);
pii p = query(1);
update_c(1, p.first);
update_c(2, p.second);
finit(2);
dis[1] = getsum(1), dis[2] = getsum(2);
merge(1, 2);
Find(1);
write(dis[1]);
LF;
return 0;
}~