线段树合并求卡空间
查看原帖
线段树合并求卡空间
1033990
lw393楼主2025/6/30 16:36

本来是 TLE 的,卡了卡常,成 MLE 了。求卡一卡!!

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, M = 1.68e7 + 5;

class fastIO
{
private:
    char ibuf[50007], *p1 = ibuf, *p2 = ibuf, obuf[50007], *p3 = obuf, sta[50];
    bool file_end = false;
    char get() { return p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 50007, stdin), p1 == p2) ? (file_end = true), char(EOF) : *p1++; }
    void put(const char x) { p3 - obuf < 50007 ? *p3++ = x : (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf, *p3++ = x); }

public:
    explicit operator bool() { return !file_end; }
    size_t flush()
    {
        size_t f = fwrite(obuf, p3 - obuf, 1, stdout);
        p3 = obuf;
        *p3 = 0;
        return f;
    }
    fastIO &operator>>(char &t)
    {
        for (t = get(); !isgraph(t); t = get())
            ;
        return *this;
    }
    template <typename any>
    typename std::enable_if<std::is_same<any, char>::value, any>::type tpval()
    {
        char t;
        for (t = get(); !isgraph(t); t = get())
            ;
        return t;
    }
    fastIO &operator>>(char *t)
    {
        char c;
        for (c = get(); !isgraph(c); c = get())
            ;
        for (; isgraph(c); c = get())
            *t = c, t++;
        *t = 0;
        return *this;
    }
    fastIO &operator>>(std::string &t)
    {
        t.clear();
        char c;
        for (c = get(); !isgraph(c); c = get())
            ;
        for (; isgraph(c); c = get())
            t += c;
        return *this;
    }
    template <typename any>
    typename std::enable_if<std::is_same<any, std::string>::value, any>::type tpval()
    {
        std::string t;
        char c;
        for (c = get(); !isgraph(c); c = get())
            ;
        for (; isgraph(c); c = get())
            t += c;
        return t;
    }
    template <typename any>
    typename std::enable_if<(std::is_signed<any>::value && std::is_integral<any>::value && !std::is_same<any, char>::value) || std::is_same<any, __int128_t>::value, fastIO>::type &operator>>(any &t)
    {
        t = 0;
        bool y = 0;
        char c = get();
        for (; !isdigit(c); c = get())
            if (c == 45)
                y = true;
        for (; isdigit(c); c = get())
            t = t * 10 + c - 48;
        if (y == 1)
            t = -t;
        return *this;
    }
    template <typename any>
    typename std::enable_if<(std::is_signed<any>::value && std::is_integral<any>::value && !std::is_same<any, char>::value) || std::is_same<any, __int128_t>::value, any>::type tpval()
    {
        any t = 0;
        bool y = 0;
        char c = get();
        for (; !isdigit(c); c = get())
            if (c == 45)
                y = true;
        for (; isdigit(c); c = get())
            t = t * 10 + c - 48;
        if (y == 1)
            t = -t;
        return t;
    }
    template <typename any>
    typename std::enable_if<(std::is_unsigned<any>::value && std::is_integral<any>::value && !std::is_same<any, char>::value) || std::is_same<any, __uint128_t>::value, fastIO>::type &operator>>(any &t)
    {
        t = 0;
        char c = get();
        for (; !isdigit(c); c = get())
            ;
        for (; isdigit(c); c = get())
            t = t * 10 + c - 48;
        return *this;
    }
    template <typename any>
    typename std::enable_if<(std::is_unsigned<any>::value && std::is_integral<any>::value && !std::is_same<any, char>::value) || std::is_same<any, __uint128_t>::value, any>::type tpval()
    {
        any t = 0;
        char c = get();
        for (; !isdigit(c); c = get())
            ;
        for (; isdigit(c); c = get())
            t = t * 10 + c - 48;
        return t;
    }
    template <typename any1, typename any2>
    fastIO &operator>>(std::pair<any1, any2> &t) { return *this >> t.first >> t.second; }
    template <typename any1, typename any2>
    std::pair<any1, any2> tpval() { return std::pair<any1, any2>(tpval<any1>(), tpval<any2>()); }
    template <typename any>
    fastIO &read(any &t) { return *this >> t; }
    fastIO &read(char *t)
    {
        char c;
        for (c = get(); !isgraph(c); c = get())
            ;
        for (; isgraph(c); c = get())
            *t = c, t++;
        *t = 0;
        return *this;
    }
    template <typename any, typename... args>
    fastIO &read(any &t1, args &...t2) { return (*this >> t1).read(t2...); }
    fastIO &operator<<(const char t)
    {
        put(t);
        return *this;
    }
    fastIO &operator<<(const char *t)
    {
        for (; *t; t++)
            put(*t);
        return *this;
    }
    fastIO &operator<<(const std::string &t)
    {
        for (const char it : t)
            put(it);
        return *this;
    }
    template <typename any>
    typename std::enable_if<(std::is_signed<any>::value && std::is_integral<any>::value && !std::is_same<any, char>::value) || std::is_same<any, __int128_t>::value, fastIO>::type &operator<<(any t)
    {
        if (!t)
        {
            put(48);
            return *this;
        }
        int len = 0;
        if (t < 0)
            t = -t, put(45);
        while (t)
            sta[len++] = char(t % 10 + 48), t /= 10;
        while (len--)
            put(sta[len]);
        return *this;
    }
    template <typename any>
    typename std::enable_if<(std::is_unsigned<any>::value && std::is_integral<any>::value && !std::is_same<any, char>::value) || std::is_same<any, __uint128_t>::value, fastIO>::type &operator<<(any t)
    {
        if (!t)
        {
            put(48);
            return *this;
        }
        int len = 0;
        while (t)
            sta[len++] = char(t % 10 + 48), t /= 10;
        while (len--)
            put(sta[len]);
        return *this;
    }
    template <typename any1, typename any2>
    fastIO &operator<<(const std::pair<any1, any2> &t) { return *this << t.first << ' ' << t.second; }
    template <typename any>
    fastIO &write(const any &t) { return *this << t; }
    template <typename any, typename... args>
    fastIO &write(const any &t1, const args &...t2) { return (*this << t1).write(t2...); }
    ~fastIO() { fwrite(obuf, p3 - obuf, 1, stdout); }
} fio;

int ls[M], rs[M], xor_sum[M];
int num, n;
vector<pair<int, int> >q[N];

inline void modify(int &k, int l, int r, int x, int v){
	k = ++num; xor_sum[k] = v;
	if(l == r) { return; }
	int mid = l + r >> 1;
	if(x <= mid) modify(ls[k], l, mid, x, v);
	else modify(rs[k], mid + 1, r, x, v);
}

inline int merge(int x, int y){
	if(!x || !y) return x | y;
	ls[x] = merge(ls[x], ls[y]), rs[x] = merge(rs[x], rs[y]);
	xor_sum[x] = xor_sum[x] ^ xor_sum[y];
	return x;
}

inline int query(int k, int l, int r, int ql, int qr){
	if(l >= ql && r <= qr) return xor_sum[k];
	int mid = l + r >> 1, ans = 0;
	if(ql <= mid) ans = query(ls[k], l, mid, ql, qr);
	if(qr > mid) ans ^= query(rs[k], mid + 1, r, ql, qr);
	return ans;
}

struct edge{
	int to, next;
}e[N];
int head[N], cnt;
void add(int u, int v){
	e[cnt] = {v, head[u]};
	head[u] = cnt++;
}

void init(){
	memset(head, -1, sizeof(head));
	cnt = 0;
}
int a[N], dep[N];

inline void dfs(int u, int fa){
	dep[u] = dep[fa] + 1;
	for(register int i = head[u], v; ~i; i = e[i].next){
		v = e[i].to;
		if(v == fa) continue;
		dfs(v, u);
	}
}

int ans[N];

inline int dfs2(int u, int fa){
	int rt;
	modify(rt, 1, n, dep[u], a[u]);
	for(register int i = head[u], v; ~i; i = e[i].next){
		v = e[i].to;
		rt = merge(rt, dfs2(v, u));
	}
	for(auto [x, d] : q[u]){
		ans[x] = query(rt, 1, n, dep[u], min(n, dep[u] + d));
	}
	return rt;
}

inline void solve(){
	int m; fio >> n >> m; init();
	for(register int i = 1; i <= n; i++) fio >> a[i];
	for(register int i = 1; i < n; i++){
		int p; fio >> p;
		add(p, i + 1);
	}
	for(register int i = 1; i <= m; i++){
		int u, h; fio >> u >> h;
		q[u].push_back({i, h});
	}
	dfs(1, 0); dfs2(1, 0);
	for(int i = 1; i <= m; i++) fio << ans[i] / 1000 << '.' << ans[i] % 1000 / 100 << ans[i] % 100 / 10 << ans[i] % 10 << '\n';
}

int main(){
	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
	return 0;
}
2025/6/30 16:36
加载中...