首页 > 其他分享 >Codeforces Round 950 (Div. 3)G. Yasya and the Mysterious Tree(字典树处理区间异或值)

Codeforces Round 950 (Div. 3)G. Yasya and the Mysterious Tree(字典树处理区间异或值)

时间:2024-06-07 17:23:10浏览次数:20  
标签:std cnt int 950 Tree Codeforces -- trie add

Problem - G - Codeforces

存个字典树板子。

  1 #include <bits/stdc++.h>
  2 
  3 using i64 = long long;
  4 
  5 constexpr int N = 1E7;
  6 
  7 int trie[N][2];
  8 int cnt[N][2];
  9 
 10 int tot = 0;
 11 int newNode() {
 12     int x = ++tot;
 13     trie[x][0] = trie[x][1] = 0;
 14     cnt[x][0] = cnt[x][1] = 0;
 15     return x;
 16 }
 17 
 18 void add(int x, int d, int t = 1) {
 19     int p = 1;
 20     cnt[p][d] += t;
 21     for (int i = 29; i >= 0; i--) {
 22         int u = x >> i & 1;
 23         if (!trie[p][u]) {
 24             trie[p][u] = newNode();
 25         }
 26         p = trie[p][u];
 27         cnt[p][d] += t;
 28     }
 29 }
 30 
 31 int query(int x, int d) {
 32     int p = 1;
 33     if (!cnt[p][d]) {
 34         return 0;
 35     }
 36     int ans = 0;
 37     for (int i = 29; i >= 0; i--) {
 38         int u = x >> i & 1;
 39         if (cnt[trie[p][u ^ 1]][d]) {
 40             ans |= 1 << i;
 41             p = trie[p][u ^ 1];
 42         } else {
 43             p = trie[p][u];
 44         }
 45     }
 46     return ans;
 47 }
 48 
 49 void solve() {
 50     int n, m;
 51     std::cin >> n >> m;
 52     
 53     std::vector<std::vector<std::pair<int, int>>> adj(n);
 54     for (int i = 1; i < n; i++) {
 55         int u, v, w;
 56         std::cin >> u >> v >> w;
 57         u--, v--;
 58         adj[u].emplace_back(v, w);
 59         adj[v].emplace_back(u, w);
 60     }
 61     
 62     std::vector<int> a(n), d(n);
 63     auto dfs = [&](auto &&self, int x, int p) -> void {
 64         for (auto [y, w] : adj[x]) {
 65             if (y == p) {
 66                 continue;
 67             }
 68             d[y] = d[x] ^ 1;
 69             a[y] = a[x] ^ w;
 70             self(self, y, x);
 71         }
 72     };
 73     dfs(dfs, 0, -1);
 74     
 75     tot = 0;
 76     newNode();
 77     for (int i = 0; i < n; i++) {
 78         add(a[i], d[i]);
 79     }
 80     
 81     int w = 0;
 82     while (m--) {
 83         char o;
 84         std::cin >> o;
 85         
 86         if (o == '^') {
 87             int y;
 88             std::cin >> y;
 89             w ^= y;
 90         } else {
 91             int v, x;
 92             std::cin >> v >> x;
 93             v--;
 94             add(a[v], d[v], -1);
 95             int ans = std::max(query(a[v] ^ x, d[v]), query(a[v] ^ x ^ w, d[v] ^ 1));
 96             add(a[v], d[v]);
 97             std::cout << ans << " ";
 98         }
 99     }
100     std::cout << "\n";
101 }
102 
103 int main() {
104     std::ios::sync_with_stdio(false);
105     std::cin.tie(nullptr);
106     std::cout.tie(nullptr);
107     
108     int t;
109     std::cin >> t;
110     
111     while (t--) {
112         solve();
113     }
114     
115     return 0;
116 }

 

   

标签:std,cnt,int,950,Tree,Codeforces,--,trie,add
From: https://www.cnblogs.com/ccsu-kid/p/18237566

相关文章

  • Codeforces Round 949 (Div. 2)D. Turtle and Multiplication(欧拉路径、线性筛、思维
    Problem-D-Codeforces  按照官方正解做即可,顺带存个jiangly板子。1#include<bits/stdc++.h>23usingi64=longlong;4std::vector<int>minp,primes;56voidsieve(intn){7minp.assign(n+1,0);8primes.clear();910......
  • Codeforces Round 950 (Div. 3) A B C D E
    A.ProblemGeneratortimelimitpertest:1secondmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputVladisplanningtoholdm......
  • Codeforces Round 951 (Div. 2)
    A.GuesstheMaximum题意:给定一个数组,求一个k值,k满足对于任意的这个数组的区间的最大值max,k<max。求满足条件的最大k。思路:只考虑长度为2的区间即可。参与到比较中的数值一定是两个数中的大数,从所有大数中选出最小的一个即可。总结:赛时很快就A掉了,但是思考的不够细节,思维太......
  • codeforces round 961题解(A、B、C)
    A.GuesstheMaximum因为\(i<j\),所以所有的\([i,j]\)区间中都至少包含两个相邻元素,所以只要求出所有相邻元素中较大值的最小值即可。intn;inta[N];voidsolve(){cin>>n;intmin_v=1e9+1;for(inti=1;i<=n;i++){cin>>a[i];......
  • CodeForces Round #951(Div. 2) 补题记录(A~D)
    A容易发现对于任意一个长度为\(n\),下标从\(1\)开始的序列\(a\),若\(1\lel\ler<n\),则必然有\(\max\limits_{i=l}^ra_i\le\max\limits_{i=l}^{r+1}a_i\)。若\(1<l\ler\len\),则必然有\(\max\limits_{i=l}^ra_i\le\max\limits_{i=l-1}^ra_i\)。很显然Bob希望......
  • codeforces 1442 D Codeforces Round 681 (Div. 1, based on VK Cup 2019-2020 - Fina
    链接大意就是给你n组物品,这n组物品里面每组有\(t_i\)个,且他们是按照价值不降的顺序排列的。现在允许取k个物品,每个物品必须取在数组的开头处,每个物品在被取用后就会消失。问你最大能够拿到多少价值的物品。其中\(n,k\leq1500,\sumt_i\leq1e6,a_i\leq1e8\)很背包吧。可......
  • 每日AIGC最新进展(20):基于树的长视频理解VideoTree、IBM研究院提出AI生成图片生成检测
    DiffusionModels专栏文章汇总:入门与实战VideoTree:AdaptiveTree-basedVideoRepresentationforLLMReasoningonLongVideos本文介绍了一种名为VideoTree的新框架,旨在提高长视频理解任务中的推理能力。VideoTree通过自适应和分层的方法,动态提取与查询相关的视频帧,......
  • G. Yasya and the Mysterious Tree
    G.YasyaandtheMysteriousTreeYasyawaswalkingintheforestandaccidentallyfoundatreewith$n$vertices.Atreeisaconnectedundirectedgraphwithnocycles.Nexttothetree,thegirlfoundanancientmanuscriptwith$m$querieswrittenonit.......
  • GCD-sequence(Round 950)
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • Matrix-Tree 定理
    引入此算法可以解决图上生成树计数问题。值得注意的是,矩阵树定理不能用于存在自环的图。定义设\(G\)是一个图。记邻接矩阵\(A(G)_{i,j}=\#e(i,j),\#e(i,j)\)若\(G\)是无向图记\(D(G)\)表示其度数矩阵,\(D(G)\)满足\(D(G)_{i,i}\)表示第\(i\)点的度数,\(D(G)_{......