首页 > 其他分享 >CF383C Propagating tree 题解

CF383C Propagating tree 题解

时间:2024-02-29 09:56:47浏览次数:29  
标签:typedef include val int 题解 template CF383C Propagating define

题目链接:CF 或者 洛谷

比较朴素的题,注意到这个涉及到子树变化,我们考虑优先处理出 \(dfs\) 序,方便处理。

注意到第一个问题较为繁琐,我们着重解决下第一个问题。在树上问题,我们这种间隔点常常使用 \(deep\) 进行区分。

  1. 根的 \(deep\) 为奇数,那么对自己子树范围内的奇数 \(deep\) 都是 \(+val\),偶数 \(deep\) 都是 \(-val\)。

  2. 根的 \(deep\) 为偶数,那么对自己子树范围内的偶数 \(deep\) 都是 \(+val\),奇数 \(deep\) 都是 \(-val\)。

为此,比较直接的想法,我们直接分两种贡献,分别维护两个 \(BIT\) 进行整棵子树变化,但在询问答案时,对于当前点的 \(deep\) 分讨,以奇数为例,那么第一个 \(BIT\) 的贡献即为 \(+\),第二个 \(BIT\) 即为 \(-\)。如果为偶数深度,显然就反过来了。关于子树加和询问单点,我们使用树状数组维护差分答案即可。

参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

// #define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}

constexpr int N = 2e5 + 10;
int n, q;

struct
{
    int bit[N];

    void add(int x, const int val)
    {
        while (x <= n)bit[x] += val, x += lowBit(x);
    }

    int query(int x) const
    {
        int ans = 0;
        while (x)ans += bit[x], x -= lowBit(x);
        return ans;
    }
} bitOdd, bitEven;

vector<int> child[N];
int s[N], e[N], deep[N], cnt;

inline void dfs(const int curr, const int fa)
{
    deep[curr] = deep[fa] + 1;
    s[curr] = ++cnt;
    for (const auto nxt : child[curr])if (nxt != fa)dfs(nxt, curr);
    e[curr] = cnt;
}

int a[N];

inline void solve()
{
    cin >> n >> q;
    forn(i, 1, n)cin >> a[i];
    forn(i, 1, n-1)
    {
        int u, v;
        cin >> u >> v;
        child[u].push_back(v), child[v].push_back(u);
    }
    dfs(1, 0);
    while (q--)
    {
        int op;
        cin >> op;
        if (op == 1)
        {
            int u, val;
            cin >> u >> val;
            if (deep[u] & 1)bitOdd.add(s[u], val), bitOdd.add(e[u] + 1, -val);
            else bitEven.add(s[u], val), bitEven.add(e[u] + 1, -val);
        }
        else
        {
            int u;
            cin >> u;
            int odd = bitOdd.query(s[u]), even = bitEven.query(s[u]);
            cout << (deep[u] & 1 ? a[u] + odd - even : a[u] + even - odd) << endl;
        }
    }
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

考虑下优化,我们的两个 \(BIT\) 能否合成一个?以奇数深度为例,我们注意到最终对于奇数深度的点,询问的答案为 \(a[i]+odd-even\),那么我们 \(BIT\) 直接奇数深度的点还是维护 \(+\),偶数深度直接维护 \(-\),这样直接查出来的答案就是奇数深度点的答案。偶数深度询问点答案,我们注意到为 \(a[i]-even+odd\),即为加上它的相反数,这样一来我们就优化到只用需要一个 \(BIT\) 就能统计答案了。

优化参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

// #define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}

constexpr int N = 2e5 + 10;
int n, q;


int bit[N];

void add(int x, const int val)
{
    while (x <= n)bit[x] += val, x += lowBit(x);
}

int query(int x)
{
    int ans = 0;
    while (x)ans += bit[x], x -= lowBit(x);
    return ans;
}


vector<int> child[N];
int s[N], e[N], deep[N], cnt;

inline void dfs(const int curr, const int fa)
{
    deep[curr] = deep[fa] + 1;
    s[curr] = ++cnt;
    for (const auto nxt : child[curr])if (nxt != fa)dfs(nxt, curr);
    e[curr] = cnt;
}

int a[N];

inline void solve()
{
    cin >> n >> q;
    forn(i, 1, n)cin >> a[i];
    forn(i, 1, n-1)
    {
        int u, v;
        cin >> u >> v;
        child[u].push_back(v), child[v].push_back(u);
    }
    dfs(1, 0);
    while (q--)
    {
        int op;
        cin >> op;
        if (op == 1)
        {
            int u, val;
            cin >> u >> val;
            if (!(deep[u] & 1))val = -val;
            add(s[u], val), add(e[u] + 1, -val);
        }
        else
        {
            int u;
            cin >> u;
            int val = query(s[u]);
            if (!(deep[u] & 1))val = -val;
            cout << a[u] + val << endl;
        }
    }
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\[时间复杂度为:\ O(q\log{n}) \]

标签:typedef,include,val,int,题解,template,CF383C,Propagating,define
From: https://www.cnblogs.com/Athanasy/p/18042755

相关文章

  • P2487 [SDOI2011] 拦截导弹 题解
    题目链接:拦截导弹约定:本题中提到的\(LDS\)和\(LIS\)不是严格单调,而是非严格单调,即为\(\le或者\ge\)。比较神奇的题,问的东西比较多,我们逐渐拆分:对于第一个问题而言,这个dp方程是很好写的:\[dp[i]=\max{dp[j]}+1(i<j,h[i]\leh[j],v[i]\lev[j])\]其中\(dp[i]\)即......
  • 题解 gym102900J 【Octasection】
    代码:#include<iostream>#include<algorithm>#include<stack>#include<vector>#include<cstdio>usingnamespacestd;typedefstructRectangle_tag{ intx1; intx2; inty1; inty2; Rectangle_tag(intx1_,intx2_,int......
  • 24冬网络流复习题解
    A是谁瞪了半个小时不会*2000呀,是谁呀是谁呀。感觉这题比部分紫题都难。。。首先发现选取字符的顺序并不重要,所以\(t\)可以看成\(26\)个字符要选的个数。设字符\(c\)出现了\(x\)次,那么直接向汇点连流量为\(x\)费用为\(0\)的边。然后考虑\(s_i\)与每个字符的关......
  • 2024年2月28号题解
    P4994终于结束的起点解题思路通过加法同余原理可以知道f[i]%m==0,那么f[i-1]%m=1,所有把f[i+1]%m=1转换成了f[i-1]%m=1的问题那么只需要填好斐波那契数列再判断就可以了,又因为斐波那契可能会超出int的范围那么我们对每一项斐波那契再取模就可以了代码......
  • 旅游景点 Tourist Attractions (壮压 DP)题解
    简化题意题目链接——不卡内存班题目链接——卡内存版给定\(n\)个点和\(m\)条边组成的无向图,按照一定限制要求停留\(2\simk+1\)共\(k\)个点(可以经过但不停留),求最短的从\(1\)出发到\(n\)的路径长。限制情况如下:共有\(q\)个限制,接下来\(q\)行,每行两个数\(x......
  • CF1408H Rainbow Triples 题解
    Description给定长度为\(n\)的序列\(p\)找出尽可能多的三元组\((a_i,b_i,c_i)\)满足:\(1\lea_i<b_i<c_i\len\)\(p_{a_i}=p_{c_i}=0\),\(p_{b_i}\ne0\)\(p_{b_i}\)互不相同。所有的\(a_i,b_i,c_i\)互不相同。输出最多可以选出多少个三元组,多组数据。\(\sumn\le......
  • P10160 [DTCPC 2024] Ultra 题解
    【题目描述】给你一个\(01\)序列,你可以进行如下操作若干次(或零次):将序列中形如\(101\cdots01\)的一个子串(即\(1(01)^k\),\(k\ge1\))替换成等长的\(010\cdots10\)(即\(0(10)^k\))。你要操作使得\(1\)的个数尽可能少,输出最少的\(1\)的个数。【思路】一开始看到这道题......
  • 数据类型拓展与面试题解读
    整数拓展进制:在平时咱们生活中经常见到的例如通用于电脑识别的二进制、咱们生活中常用的十进制、工作中常见的八进制与十六进制。二进制:通常会以0b开头十进制:咱们生活中的数字八进制:通常以0开头十六进制:通常以0x开头​ 浮点数拓展(float、double)试题举例银行......
  • Codeforces Round 929 (Div. 3) 题解(D-G)
    比赛链接:https://codeforces.com/contest/1933官解暂未放出。质量不错的一场D3。CF1933D.TurtleTenacity:ContinualMods题意将数组\(a_{1..n}\ge1\)重排,问是否能使\(((a_1\mathbin{\rmmod}a_2)\mathbin{\rmmod}a_3)\cdots\mathbin{\rmmod}a_n\ne0\)......
  • 题解 P10196【[USACO24FEB] Lazy Cow P】
    总算铂金组场切一题。似乎做麻烦了,而且常数蛮大的,但是没啥思维难度,记录一下。对于每一个需求,我们将其放置在平面直角坐标系的\((m_i,b_i)\)位置。另外,自然有一个\((0,0)\)需求,也同样处理这个点。我们需要支持插入一个点的操作,并维护出答案。先考虑不需要动态插点,只在最后求......