跳到正文

剪贴板

剪贴板 h3mzcfo1

原文
剪贴板信息

剪贴板信息

收录
更新
C++
//Program written by Liu Zhaozhou ~~~
//模板集!!!
// 1: Tarjan
inline void tarjan(int p) {
	dfn[p] = low[p] = ++tot;
	vis[p] = true;
	sta[+ cnt] = p;
	for (int e = head[p]; e; e = l[e].nex) {
		int u = l[e].to;
		if (!dfn[u]) {
			tarjan(u);
			low[p] = min(low[p], low[u]);
		}
		else if (vis[u]) low[p] = min(low[p], dfn[u]);
	}
	if (dfn[p] == low[p]) {
		int fir = 0; num++;
		while(fir != p) {
			fir = sta[cnt--];
			vis[fir] = false;
			bel[fir] = num;
			sum[num]++;
		}
	}
}
// 2: Tubao
enum {
	Maxn = 100005
};
const double PI = acos(-1.0);

int n, top, tag;
double ans;

struct Vector {
	double x, y;
	Vector(double _x = 0, double _y = 0) : x(_x), y(_y) {}
	inline void Scanner(void) { read(x), read(y); }

	inline bool operator < (const Vector&rhs)
	const { return x == rhs.x ? y < rhs.y : x < rhs.x; }
	inline Vector operator - (const Vector&rhs)
	const { return Vector(x - rhs.x, y - rhs.y); }
	inline Vector operator + (const Vector&rhs)
	const { return Vector(x + rhs.x, y + rhs.y); }

	inline Vector operator * (const double&rhs)
	const { return Vector(x * rhs, y * rhs); }
	inline Vector operator / (const double&rhs)
	const { return Vector(x / rhs, y / rhs); }
} a[Maxn], sta[Maxn];

inline double pow2(const double num) { return num * num; }
inline double dist(const Vector&a, const Vector&b) { return sqrt(pow2(a.x - b.x) + pow2(a.y - b.y)); }
inline double cross(const Vector&a, const Vector&b) { return a.x * b.y - a.y * b.x; }
inline double dot(const Vector&a, const Vector&b) {return a.x * b.x + a.y * b.y; }
inline double tod(const Vector&a, const Vector&b) {return a.x * b.x - a.y * b.y; }

inline bool check(int i) { return cross(sta[top] - sta[top - 1], a[i] - sta[top]) <= 0; }

inline void GetConvexHull(void) {
	top = ans = 0; sort(a + 1, a + n + 1);
	for (int i = 1; i <= n; i++) {
		for (; top > 1 && check(i); --top);
		sta[++top] = a[i];
	} tag = top;
	for (int i = n - 1; i; i--) {
		for (; top > tag && check(i); --top);
		sta[++top] = a[i];
	}
	for (int i = 1; i < top; i++) ans += dist(sta[i], sta[i + 1]);
}
// 3: Suffix Array
enum {
	Maxn = 1000005
};
char str[Maxn];
int n, m, Rank[Maxn], SA[Maxn];
int cnt[Maxn], tmp[Maxn];

inline void GetStringData(void) {
	readstr(str + 1); m = 75;
	n = strlen(str + 1); return;
}

inline void RadixSort(void) {
	for (int i = 0; i <= m; i++) cnt[i] = 0;
	for (int i = 1; i <= n; i++) ++cnt[Rank[i]];
	for (int i = 1; i <= m; i++) cnt[i] += cnt[i - 1];
	for (int i = n; i >= 1; i--) SA[cnt[Rank[tmp[i]]]--] = tmp[i];
}

inline void SuffixSort(void) {
	for (int i = 1; i <= n; i++)
		Rank[i] = str[i] - '0' + 1, tmp[i] = i; RadixSort();
	for (int k = 1, tot = 0; tot < n; m = tot, k <<= 1) {
		tot = 0;
		for (int i = 1; i <= k; i++) tmp[++tot] = n - k + i;
		for (int i = 1; i <= n; i++) if (SA[i] > k) tmp[++tot] = SA[i] - k;
		RadixSort(); swap(tmp, Rank); Rank[SA[1]] = tot = 1;
		for (int i = 2; i <= n; i++)
			Rank[SA[i]] = (tmp[SA[i - 1]] == tmp[SA[i]] && tmp[SA[i - 1] + k] == tmp[SA[i] + k]) ? tot : ++tot;
	}
}

inline void PrintSuffixArray(void) {
	for (int i = 1; i <= n; i++)
		if (i == n) writeln(SA[i]);
		else writeln(SA[i], ' ');
}
// 4: FHQTreap
struct FHQTreap {
	int lson[Maxn], rson[Maxn], data[Maxn];
	int sze[Maxn], rnd[Maxn], tot, seed, root;

	FHQTreap(void) {
		Ms(lson, 0), Ms(data, 0);
		Ms(sze, 0), Ms(rnd, 0), root = 0;
		Ms(rson, 0), tot = 0, seed = 1;
	}

	inline int _rand(void) {return seed *= 482711;}
	inline void pushup(int pos) {
		sze[pos] = sze[lson[pos]] + sze[rson[pos]] + 1;
	}

	inline void split(int pos, int val, int &x, int &y)
	{
		if (!pos) {x = y = 0; return;}
		if (data[pos] <= val)
			x = pos, split(rson[pos], val, rson[pos], y);
		else y = pos, split(lson[pos], val, x, lson[pos]);
		pushup(pos);
	}

	inline int merge(int x, int y) {
		if (!x || !y) return x + y;
		if (rnd[x] < rnd[y])
			return rson[x] = merge(rson[x], y), pushup(x), x;
		else return lson[y] = merge(x, lson[y]), pushup(y), y;
	}

	inline void insert(int val) {
		int x, y, pos = ++tot; sze[pos] = 1;
		data[pos] = val, rnd[pos] = _rand();
		split(root, val, x, y);
		root = merge(merge(x, pos), y);
	}

	inline void remove(int val) {
		int x, y, z;
		split(root, val - 1, x, y);
		split(y, val, y, z);
		if (!y) return;
		y = merge(lson[y], rson[y]);
		root = merge(x, merge(y, z));
	}

	inline int query_rank(int val) {
		int x, y, ret;
		split(root, val - 1, x, y);
		ret = sze[x] + 1, root = merge(x, y);
		return ret;
	}

	inline int select(int kth) {
		int pos = root;
		while (kth != sze[lson[pos]] + 1)
			if (kth <= sze[lson[pos]]) pos = lson[pos];
			else kth -= sze[lson[pos]] + 1, pos = rson[pos];
		return data[pos];
	}

	inline int pred(int val) {return select(query_rank(val) - 1);}
	inline int succ(int val) {return select(query_rank(val + 1));}
} treap;
// 5: 可持久化FHQTreap
class FHQTreap {
public:
	int lson[Maxn], rson[Maxn], data[Maxn];
	int sze[Maxn], rnd[Maxn], tot, seed, root[Maxn / 50 + 1];

	FHQTreap(void) {
		Ms(lson, 0), Ms(data, 0);
		Ms(sze, 0), Ms(rnd, 0), Ms(root, 0);
		Ms(rson, 0), tot = 0, seed = 1;
	}

	inline int _rand(void) {return seed *= 482711;}
	inline void pushup(int pos) {
		sze[pos] = sze[lson[pos]] + sze[rson[pos]] + 1;
	}

	inline void inherit_root(int now, int old) {
		root[now] = root[old]; return;
	}

	inline void inherit_info(int now, int old) {
		lson[now] = lson[old], rson[now] = rson[old];
		data[now] = data[old], sze[now] = sze[old];
		rnd[now] = rnd[old]; return;
	}

	inline void split(int pos, int val, int &x, int &y)
	{
		if (!pos) {x = y = 0; return;}
		if (data[pos] <= val)
			x = ++tot, inherit_info(x, pos), split(rson[x], val, rson[x], y), pushup(x);
		else y = ++tot, inherit_info(y, pos), split(lson[y], val, x, lson[y]), pushup(y);
	}

	inline int merge(int x, int y) {
		if (!x || !y) return x + y;
		if (rnd[x] < rnd[y]) {
			int p = ++tot; inherit_info(p, x);
			return rson[p] = merge(rson[p], y), pushup(p), p;
		} else {
			int q = ++tot; inherit_info(q, y);
			return lson[q] = merge(x, lson[q]), pushup(q), q;
		}
	}

	inline void insert(int now, int val) {
		int x = 0, y = 0, pos = ++tot; sze[pos] = 1;
		data[pos] = val, rnd[pos] = _rand();
		split(root[now], val, x, y);
		root[now] = merge(merge(x, pos), y);
	}

	inline void remove(int now, int val) {
		int x, y, z; x = y = z = 0;
		split(root[now], val - 1, x, y);
		split(y, val, y, z);
		if (!y) return;
		y = merge(lson[y], rson[y]);
		root[now] = merge(x, merge(y, z));
	}

	inline int query_rank(int now, int val) {
		int x, y, ret;
		split(root[now], val - 1, x, y);
		ret = sze[x] + 1, root[now] = merge(x, y);
		return ret;
	}

	inline int select(int now, int kth) {
		int pos = root[now];
		while (kth != sze[lson[pos]] + 1)
			if (kth <= sze[lson[pos]]) pos = lson[pos];
			else kth -= sze[lson[pos]] + 1, pos = rson[pos];
		return data[pos];
	}

	inline int pred(int now, int val) {return select(now, query_rank(now, val) - 1);}
	inline int succ(int now, int val) {return select(now, query_rank(now, val + 1));}
} treap;
// 6: 块状链表
enum {
    block_s = 21000
};

int T, pos; char opt[7];
vector <char> temp;

class BlockList {
public:
    typedef list< vector <char> > :: iterator ptr;

    list < vector<char> > List;

    inline ptr next(ptr x) {return ++x;}

    inline ptr find(int&x) {
        for (register ptr i = List.begin(); i != List.end(); ++i) {
            if (x <= i->size()) return i;
            x -= i->size();
        } return List.begin();
    }

    inline void println(int l, int r) {
        register ptr L = find(l), R = find(r);
        if (L == R) for (register int i = l; i < r; ++i) ot(L->at(i));
        else {
            for (register int i = l; i < L->size(); ++i) ot(L->at(i));
            for (++L; L != R; ++L)
                for (register int i = 0; i < L->size(); ++i) ot(L->at(i));
            for (register int i = 0; i < r; ++i) ot(R->at(i));
        } ot('\n');
    }

    inline void merge(ptr x) {
        x->insert(x->end(), next(x)->begin(), next(x)->end());
        List.erase(next(x));
    }

    inline void split(ptr x, int pos) {
        if (pos == x->size()) return;
        List.insert(next(x), vector <char> (x->begin() + pos, x->end()));
        x->erase(x->begin() + pos, x->end());
    }

    inline void modify(void)
    {
        for (register ptr i = List.begin(); i != List.end(); ++i)
        {
            while (i->size() >= (block_s << 1)) split(i, i->size() - block_s);
            while (next(i) != List.end() && i->size() + next(i)->size() <= block_s) merge(i);
            while (next(i) != List.end() && next(i)->empty()) List.erase(next(i));
        }
    }

    inline void insert(int pos, const vector <char>&ins) {
        register ptr curr = find(pos);
        if (!List.empty()) split(curr, pos);
        List.insert(next(curr), ins); modify();
    }

    inline void erase(int l, int r)
    {
        register ptr L, R;
        L = find(l), split(L, l);
        R = find(r), split(R, r);
        for (++R; next(L) != R; List.erase(next(L)));
        modify();
    }

} blt;
// 7: 块状树
using std :: vector;
using std :: sort;
using std :: upper_bound;
using std :: lower_bound;

enum {
	N = 60005,
	M = 120005
};

int n, m, block_s, ans;
int size[N], w[N], top[N];
vector <int> List[N];

class Node {
public:
	int head[N], ver[M], nxt[M], tot;
	inline void AddEdge(int u, int v) {
		ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;
	}
	inline void Insert(int u, int v) {
		AddEdge(u, v), AddEdge(v, u); return;
	}
} Map, Block, Link;

inline void dfs(int u, int f) {
	register int root = top[u];
	List[root].push_back(w[u]);
	for (int i = Map.head[u]; i; i = Map.nxt[i])
	{
		if (Map.ver[i] == f) continue;
		Block.AddEdge(u, Map.ver[i]);
		if (size[root] < block_s)
			++size[root], top[Map.ver[i]] = root;
		else Link.AddEdge(root, Map.ver[i]);
		dfs(Map.ver[i], u);
	}
}

inline void query(int u, int x) {
	if (u == top[u]) {
		ans += List[u].end() - upper_bound(List[u].begin(), List[u].end(), x);
		for (int i = Link.head[u]; i; i = Link.nxt[i]) query(Link.ver[i], x);
	} else { if (w[u] > x) ++ans;
		for (int i = Block.head[u]; i; i = Block.nxt[i]) query(Block.ver[i], x);
	}
}

inline void setup(void) {
	read(n); block_s = (int) ceil(2.0 * sqrt(n) * log2(n));
	for (int i = 1, u, v; i < n; i++)
		read(u), read(v), Map.Insert(u, v);
	for (int i = 1; i <= n; i++)
		read(w[i]), top[i] = i, size[i] = 1; dfs(1, 0);
	for (int i = 1; i <= n; i++)
		if (top[i] == i) sort(List[i].begin(), List[i].end());
}

inline void solve(void) {
	int opt, u, x, _top, lastans = 0; read(m);
	while (m--) {
		read(opt), read(u), read(x);
		u ^= lastans, x ^= lastans;
		if (opt == 0) {
			ans = 0; query(u, x);
			writeln(lastans = ans);
		} else if (opt == 1) {
			_top = top[u];
			List[_top].erase(lower_bound(List[_top].begin(), List[_top].end(), w[u]));
			List[_top].insert(lower_bound(List[_top].begin(), List[_top].end(), x), x);
			w[u] = x;
		} else {
			w[++n] = x; _top = top[u]; Block.AddEdge(u, n);
			if (size[_top] < block_s) {
				top[n] = _top; ++size[_top];
				List[_top].insert(lower_bound(List[_top].begin(), List[_top].end(), x), x);
			} else top[n] = n, size[u] = 1, List[n].push_back(x), Link.AddEdge(_top, n);
		}
	}
}
// 8:线性基
int n; ll a[63];

struct LinearBasis {
	ll p[63], tmp[63]; bool flg;
	LinearBasis(void) {
		Ms(p, 0), flg = false;
	}

	inline void insert(ll opt) {
		for (int i = 62; ~i; i--) {
			if (!(opt >> (ll)i)) continue;
			if (!p[i]) {p[i] = opt; return;}
			opt ^= p[i];
		} flg = true;
	}

	inline ll querymax(void) {
		ll ret = 0ll;
		for (int i = 62; ~i; i--)
			chkmax(ret, ret ^ p[i]);
		return ret;
	}

	inline ll querymin(void) {
		if (flg) return 0ll;
		for (int i = 0; i <= 62; i++)
			if (p[i]) return p[i];
	}

	inline ll select(int kth) {
		ll ret = 0; int cnt = 0; kth -= flg;
		if (!kth) return 0ll;
		for (int i = 0; i <= 62; i++) {
			for (int j = i - 1; ~j; j--)
				if (p[i] & (1ll << j)) p[i] ^= p[j];
			if (p[i]) tmp[cnt++] = p[i];
		}
		if (kth >= (1ll << cnt)) return -1ll;
		for (int i = 0; i < cnt; i++)
			if (kth & (1ll << i)) ret ^= tmp[i];
		return ret;
	}

} lbs;
// 9: PST
enum {
	Maxn = 200005
};

int n, q, cnt = 0, a[Maxn], b[Maxn];

class PST {
public:
	int L[Maxn * 40], R[Maxn * 40];
	int sum[Maxn * 40], root[Maxn * 40];

	inline void build(int &pos, int l, int r) {
		pos = ++cnt; sum[pos] = 0;
		if (l == r) return;
		int mid = l + r >> 1;
		build(L[pos], l, mid);
		build(R[pos], mid + 1, r);
		return;
	}

	inline void update(int &pos, int pre, int l, int r, int idx)
	{
		pos = ++cnt;
		L[pos] = L[pre]; R[pos] = R[pre]; sum[pos] = sum[pre] + 1;
		if (l == r) return;
		int mid = l + r >> 1;
		if (idx <= mid) update(L[pos], L[pre], l, mid, idx);
		else update(R[pos], R[pre], mid + 1, r, idx);
	}

	inline int query(int u, int v, int l, int r, int k)
	{
	    if (l >= r) return l;
	    int x = sum[L[v]] - sum[L[u]], mid = l + r >> 1;
	    if (x >= k) return query(L[u], L[v], l, mid, k);
	    else return query(R[u], R[v], mid+1, r, k - x);
	}
} pst;

int m, x, y, z, tmp;

signed main(void) {
	//file("");
	read(n); read(q);
    for (int i = 1; i <= n; i++)
        read(a[i]), b[i] = a[i];

    sort(b + 1, b + n + 1);
    m = unique(b + 1, b + n + 1) - (b + 1);
    pst.build(pst.root[0], 1, m);
    for (int i = 1; i <= n; i++){
        tmp = lower_bound(b + 1, b + m + 1, a[i]) - b;
        pst.update(pst.root[i], pst.root[i - 1], 1, m, tmp);
    }

    while (q--) {
        read(x), read(y), read(z);
        tmp = pst.query(pst.root[x - 1], pst.root[y], 1, m, z);
        writeln(b[tmp]);
    } fwrite(pf, 1, o1 - pf, stdout);
    return 0;
}
// 10: slpf
int n, m, head[Maxn], w[Maxn];
int cnt = 0, nxt[Maxe], ver[Maxe];

int dfn[Maxn], fat[Maxn], top[Maxn], son[Maxn];
int tot = 0, sze[Maxn], dep[Maxn], val[Maxn];

inline void AddEdge(int u, int v) {
    ver[++cnt] = v, nxt[cnt] = head[u];
    head[u] = cnt, ver[++cnt] = u;
    nxt[cnt] = head[v], head[v] = cnt;
}

class SegmentTree {
public:
    class Node {
    public:
        int l, r, lc, rc, sum, col;
        Node(void) {l = r = lc = rc = sum = col = 0;}
        Node(int _l, int _r, int _lc, int _rc, int _sum, int _col)
        : l(_l), r(_r), lc(_lc), rc(_rc), sum(_sum), col(_col) {}
    } t[Maxn << 2 | 1];

    inline void pushup(int pos) {
        t[pos].sum = t[pos << 1].sum + t[pos << 1 | 1].sum;
        if (t[pos << 1].rc == t[pos << 1 | 1].lc) --t[pos].sum;
        t[pos].lc = t[pos << 1].lc, t[pos].rc = t[pos << 1 | 1].rc;
    }

    inline void addtag(int pos, int col) {
        t[pos].lc = t[pos].rc = col;
        t[pos].sum = 1, t[pos].col = col;
    }

    inline void pushdown(int pos, int l, int r) {
        if (t[pos].col) {
            register int mid = l + r >> 1;
            addtag(pos << 1, t[pos].col),
            addtag(pos << 1 | 1, t[pos].col);
            t[pos].col = 0; return;
        }
    }

    inline void build(int pos, int l, int r) {
        if (l == r) {
            t[pos].lc = t[pos].rc = val[l];
            t[pos].sum = 1; return;
        }
        register int mid = l + r >> 1;
        build(pos << 1, l, mid),
        build(pos << 1 | 1, mid + 1, r);
        pushup(pos);
    }

    inline void modify(int pos, int l, int r, int L, int R, int v)
    {
        if (L <= l && r <= R) {addtag(pos, v); return;}
        register int mid = l + r >> 1; pushdown(pos, l, r);
        if (L <= mid) modify(pos << 1, l, mid, L, R, v);
        if (R > mid) modify(pos << 1 | 1, mid + 1, r, L, R, v);
        pushup(pos);
    }

    inline Node query(int pos, int l, int r, int L, int R)
    {
        if (L <= l && r <= R) return t[pos];
        register int mid = l + r >> 1; pushdown(pos, l, r);
        if (L <= mid && R > mid) {
            Node res, itl = query(pos << 1, l, mid, L, R),
            itr = query(pos << 1 | 1, mid + 1, r, L, R);
            res.sum = itl.sum + itr.sum + (itl.rc == itr.lc ? -1 : 0);
            res.lc = itl.lc; res.rc = itr.rc; return res;
        }
        if (L <= mid) return query(pos << 1, l, mid, L, R);
        if (R > mid) return query(pos << 1 | 1, mid + 1, r, L, R);
    }

} sgt;

typedef SegmentTree :: Node Node;

inline void DfsFir(int u) {
    sze[u] = 1;
    for (int i = head[u]; i; i = nxt[i]) {
        if (ver[i] == fat[u]) continue;
        fat[ver[i]] = u;
        dep[ver[i]] = dep[u] + 1;
        DfsFir(ver[i]);
        sze[u] += sze[ver[i]];
        if (sze[ver[i]] > sze[son[u]])
            son[u] = ver[i];
    }
}

inline void DfsSec(int u, int t) {
    top[u] = t; dfn[u] = ++tot;
    val[dfn[u]] = w[u];
    if (son[u]) DfsSec(son[u], t);
    for (int i = head[u]; i; i = nxt[i]) {
        if (ver[i] == fat[u] || ver[i] == son[u]) continue;
        DfsSec(ver[i], ver[i]);
    }
}

inline void Modify(int x, int y, int v) {
    while (top[x] ^ top[y]) {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        sgt.modify(1, 1, n, dfn[top[x]], dfn[x], v);
        x = fat[top[x]];
    }
    if (dep[x] > dep[y]) swap(x, y);
    sgt.modify(1, 1, n, dfn[x], dfn[y], v);
}

inline int Query(int x, int y) {
    int sum = 0, itl = 0, itr = 0; Node res;
    while (top[x] ^ top[y]) {
        if (dep[top[x]] < dep[top[y]]) swap(x, y), swap(itl, itr);
        res = sgt.query(1, 1, n, dfn[top[x]], dfn[x]);
        if (res.rc == itl) --sum; itl = res.lc;
        sum += res.sum, x = fat[top[x]];
    }
    if (dep[x] > dep[y]) swap(x, y), swap(itl, itr);
    res = sgt.query(1, 1, n, dfn[x], dfn[y]);
    if (res.lc == itl) --sum;
    if (res.rc == itr) --sum;
    return sum + res.sum;
}
// 11:splay
struct Splay {
	int root, total, tag[Maxn];
	int child[Maxn][2], size[Maxn];
	int father[Maxn], data[Maxn];

	Splay () {
		root = total = 1;
		Ms(child, 0); Ms(size, 0);
		Ms(father, 0); Ms(data, 0);
		size[1] = 1, data[1] = 0;
	}

	inline bool get(int pos) {
		return pos == child[father[pos]][1];
	}

	inline void pushup(int pos) {
		size[pos] = size[child[pos][0]];
		size[pos] += size[child[pos][1]] + 1;
	}

	inline void pushdown(int pos) {
		if (tag[pos]) {
			tag[child[pos][0]] ^= 1;
			tag[child[pos][1]] ^= 1;
			swap(child[pos][0], child[pos][1]);
			tag[pos] = 0; return;
		}
	}

	inline void rotate(int pos) {
		if (father[pos] == 0) return;
		int f = father[pos], g = father[f];
		bool tmp = get(pos), tnp = get(f);
		child[f][tmp] = child[pos][tmp ^ 1];
		father[child[pos][tmp ^ 1]] = f;
		child[pos][tmp ^ 1] = f;
		father[f] = pos, father[pos] = g;
		if (g) child[g][tnp] = pos;
		pushup(f), pushup(pos); return;
	}

	inline void splay(int pos) {
		for (int f = father[pos]; (f = father[pos]); rotate(pos))
			if (father[f]) rotate(get(pos) == get(f) ? f : pos);
		root = pos; return;
	}

	inline void splayII(int pos) {
		for (int f = father[pos]; (f = father[pos]) != root; rotate(pos))
			if (father[f] != root) rotate(get(pos) == get(f) ? f : pos);
	}

	inline void insert(int val) {
		for (int now = root;;) {
			pushdown(now);
			bool tmp = true;
			if (child[now][tmp]) now = child[now][tmp];
			else {
				child[now][tmp] = ++total;
				father[total] = now;
				data[total] = val;
				size[total] = 1;
				splay(total); return;
			}
		}
	}

	inline int select(int kth) {
		for (int now = root;;) {
			pushdown(now);
			if (kth <= size[child[now][0]]) now = child[now][0];
			else {
				kth -= size[child[now][0]];
				if (kth == 1) return now;
				--kth; now = child[now][1];
			}
		}
	}

	inline void reverse(int l, int r) {
		l = select(l), r = select(r + 2);
		splay(l), splayII(r);
		tag[child[child[root][1]][0]] ^= 1;
	}

	inline void print(int pos) {
		pushdown(pos);
		if (child[pos][0]) print(child[pos][0]);
		if (data[pos] > 0 && data[pos] < N + 1)
			writeln(data[pos], ' ');
		if (child[pos][1]) print(child[pos][1]);
	}
} splaytree;
// 12: FFT
namespace Maths {

typedef complex <double> comp;

const int N = (1 << 20) + 10 << 1;
const double PI2 = 2.0 * acos(-1.0);

int n, m;
comp a[N], b[N];

inline void FFT(int n, comp *a, int type)
{
    if (n == 1) return;
    comp a1[n >> 1], a2[n >> 1];
    for (register int i = 0; i < n; i += 2)
        a1[i >> 1] = a[i], a2[i >> 1] = a[i + 1];
    FFT(n >> 1, a1, type), FFT(n >> 1, a2, type);
    comp w(1, 0), wn(cos(PI2 / n), type * sin(PI2 / n));
    for (register int i = 0; i < n >> 1; i ++, w *= wn)
        a[i] = a1[i] + w * a2[i],
        a[i + (n >> 1)] = a1[i] - w * a2[i];
}

inline void Init(void) {
	read(n), read(m); int opt;
    for (int i = 0; i <= n; i++)
		read(opt), a[i] = opt;
    for (int i = 0; i <= m; i++)
		read(opt), b[i] = opt;
}

inline void Work(void) {
	int lim = 1;
    while (lim <= n + m)
		lim <<= 1;
	FFT(lim, a, 1), FFT(lim, b, 1);
    for (register int i = 0; i <= lim; i++)
		a[i] *= b[i]; FFT(lim, a, -1);

    for (int i = 0; i < n + m; i++)
        writeln((int) (0.5 + a[i].real() / lim), ' ');
    writeln((int) (0.5 + a[n + m].real() / lim));
}
}
// 13: fhqtreap && segmenttree
const int MAXN = 5e4 + 5, MAXM = 1e7;
const int INF = 0x7fffffff;
int a[MAXN];

int n, m;

int val[MAXM], sz[MAXM], heap[MAXM], l[MAXM], r[MAXM];
int tot = 0;

class FHQ_Treap
{
private:
    inline int randint(void) {
    	static int seed = 233;
    	return seed = int(seed * 48271LL % 2147483647);
    }
public:
    int root;

    inline void Update(int x)
    {
        sz[x] = sz[l[x]] + sz[r[x]] + 1;

        return;
    }

    inline int Merge(int x, int y)
    {
        if (!x || !y)
            return x + y;
        if (heap[x] < heap[y])
            return r[x] = Merge(r[x], y), Update(x), x;
        return l[y] = Merge(x, l[y]), Update(y), y;
    }

    inline void Split(int x, int key, int &u, int &v)
    {
        if (!x)
        {
            u = v = 0;
            return;
        }

        if (val[x] <= key)
            u = x, Split(r[x], key, r[u], v);
        else
            v = x, Split(l[x], key, u, l[v]);

        Update(x);
        return;
    }

    inline int Create(int key)
    {
        val[++tot] = key, heap[tot] = randint(), sz[tot] = 1;

        return tot;
    }

    int x, y, z;

    inline void Insert(int key)
    {
        Split(root, key, x, y);
        root = Merge(x, Merge(Create(key), y));

        return;
    }

    inline void Delete(int key)
    {
        Split(root, key, x, z);
        Split(x, key - 1, x, y);
        y = Merge(l[y], r[y]);
        root = Merge(x, Merge(y, z));

        return;
    }

    inline int FindRank(int key)
    {
        Split(root, key - 1, x, y);
        int ans = sz[x] + 1;
        root = Merge(x, y);

        return ans;
    }

    inline void Build(int l, int r)
    {
        for (int i = l; i <= r; i++)
            Insert(a[i]);

        return;
    }

    inline int FindKey(int x, int rak)
    {
        if (rak <= sz[l[x]])
            return FindKey(l[x], rak);
        if (rak == sz[l[x]] + 1)
            return val[x];
        return FindKey(r[x], rak - sz[l[x]] - 1);
    }

    inline int Prev(int key)
    {
        Split(root, key - 1, x, y);

        int ans;
        if (sz[x])
            ans = FindKey(x, sz[x]);
        else
            ans = -INF;

        root = Merge(x, y);
        return ans;
    }

    inline int Succ(int key)
    {
        Split(root, key, x, y);

        int ans;
        if (sz[y])
            ans = FindKey(y, 1);
        else
            ans = INF;

        root = Merge(x, y);
        return ans;
    }

} FT[MAXN << 2];

class SegmentTree
{
public:
    #define mid ((x + y) >> 1)
    #define lson (pst << 1)
    #define rson (pst << 1 | 1)
    int root[MAXN << 2];

    inline void Build(int x, int y, int pst)
    {
        FT[pst].Build(x, y);
        if (x != y)
            Build(x, mid, lson), Build(mid + 1, y, rson);
        return;
    }

    inline int QueryRank(int x, int y, int pst, int l, int r, int key)
    {
        if (y < l || x > r)
            return 0;
        if (l <= x && y <= r)
            return FT[pst].FindRank(key) - 1;

        return QueryRank(x, mid, lson, l, r, key) + QueryRank(mid + 1, y, rson, l, r, key);
    }

    inline int QueryKey(int l, int r, int rak)
    {
        int x = 0, y = 1e8, ans = -1;
        while (x <= y)
        {
            if (QueryRank(1, n, 1, l, r, mid) + 1 <= rak)
                ans = mid, x = mid + 1;
            else
                y = mid - 1;
        }

        return ans;
    }

    inline void Update(int x, int y, int pst, int p, int k)
    {
        FT[pst].Delete(a[p]);
        FT[pst].Insert(k);

        if (x != y)
        {
            if (p <= mid)
                Update(x, mid, lson, p, k);
            else
                Update(mid + 1, y, rson, p, k);
        }

        return;
    }

    inline int Prev(int x, int y, int pst, int l, int r, int k)
    {
        if (y < l || x > r)
            return -INF;
        if (l <= x && y <= r)
            return FT[pst].Prev(k);
        return max(Prev(x, mid, lson, l, r, k), Prev(mid + 1, y, rson, l, r, k));
    }

    inline int Succ(int x, int y, int pst, int l, int r, int k)
    {
        if (y < l || x > r)
            return INF;
        if (l <= x && y <= r)
            return FT[pst].Succ(k);
        return min(Succ(x, mid, lson, l, r, k), Succ(mid + 1, y, rson, l, r, k));
    }

} LZZAKIOI;
// 13: LCT
enum {
    S = 11005
};

class LinkCutTree {
	private:
        struct Nodes {
            int idx;
            bool isreversed;
            int f, lc, rc, pf;
        } nodes[S];
        int ncnt, location[S];

        inline void pushdown(int pos) {
            if (nodes[pos].isreversed) {
                nodes[pos].isreversed = false;
                nodes[nodes[pos].lc].isreversed
                = !nodes[nodes[pos].lc].isreversed;
                nodes[nodes[pos].rc].isreversed
                = !nodes[nodes[pos].rc].isreversed;
                swap(nodes[pos].lc, nodes[pos].rc);
            } return;
        }

        inline void leftrotate(int x) {
            int y = nodes[x].f;
            nodes[x].pf = nodes[y].pf;
            nodes[y].pf = 0;
            nodes[y].rc = nodes[x].lc;
			if (nodes[x].lc)
			nodes[nodes[x].lc].f = y;
            nodes[x].f = nodes[y].f;
            if (nodes[y].f) {
                if (nodes[nodes[y].f].lc == y)
                    nodes[nodes[y].f].lc = x;
                else nodes[nodes[y].f].rc = x;
            } nodes[y].f = x; nodes[x].lc = y;
        }

        inline void rightrotate(int x) {
            int y = nodes[x].f;
            nodes[x].pf = nodes[y].pf;
            nodes[y].pf = 0;
            nodes[y].lc = nodes[x].rc;
            if (nodes[x].rc)
                nodes[nodes[x].rc].f = y;
            nodes[x].f = nodes[y].f;
            if (nodes[y].f) {
                if (nodes[nodes[y].f].lc == y)
                    nodes[nodes[y].f].lc = x;
                else nodes[nodes[y].f].rc = x;
            } nodes[y].f = x; nodes[x].rc = y;
        }

        #define lrt(x) leftrotate(x)
        #define rrt(x) rightrotate(x)

        inline void splay(int x) {
            while (nodes[x].f) {
                int fa = nodes[x].f;
                pushdown(fa);
                if (nodes[fa].lc)
                    pushdown(nodes[fa].lc);
                if (nodes[fa].rc)
                    pushdown(nodes[fa].rc);
                if (nodes[fa].lc == x)
                    rrt(x); else lrt(x);
            } return;
        }

        inline void access(int pos) {
            splay(pos); pushdown(pos);
            int q = nodes[pos].rc;
            nodes[pos].rc = nodes[q].f = 0;
            nodes[q].pf = pos;
            for (q = nodes[pos].pf; q; q = nodes[pos].pf)
            {
                splay(q); pushdown(q);
                int o = nodes[q].rc;
                nodes[o].f = 0;
                nodes[o].pf = q;
                nodes[q].rc = pos;
                nodes[pos].f = q;
                nodes[pos].pf = 0;
                pos = q;
            } splay(pos);
        }

        inline int findrt(int pos) {
            access(pos); splay(pos);
            pushdown(pos);
            while (nodes[pos].lc)
                pos = nodes[pos].lc;
            splay(pos); return pos;
        }

        inline void evert(int pos) {
        	access(pos); splay(pos);
        	nodes[pos].isreversed
        	= !nodes[pos].isreversed;
        	pushdown(pos); return;
		}

		inline void cut(int p, int q) {
			evert(p); access(q);
			splay(q); pushdown(q);
			nodes[nodes[q].lc].f = 0;
			nodes[q].lc = 0; return;
		}

		inline void link(int p, int q) {
			evert(p); splay(p);
			pushdown(p); access(q);
			splay(q); pushdown(q);
			nodes[p].lc = q;
			nodes[q].f = p; return;
		}

	public:
		inline void setup(void) {
			ncnt = 0; Ms(location, 0);
		}

		inline void build(int idx) {
			int pos = ++ncnt;
			location[idx] = pos;
			nodes[pos].f = nodes[pos].lc =
			nodes[pos].rc = nodes[pos].pf = 0;
			nodes[pos].idx = idx;
		}

		inline int getrt(int idx) {
			return nodes[findrt(idx)].idx;
		}

		inline void insert(int u, int v) {
			link(location[u], location[v]);
		}

		inline void destroy(int u, int v) {
			cut(location[u], location[v]);
		}
};

LinkCutTree lct;