codeforces CF620E New Year Tree DFS序 线段树 状压

本文详细解析了Codeforces比赛中的E题“NewYearTree”,介绍了如何利用DFS序和线段树解决颜色修改和查询问题,通过状态压缩实现区间更新和统计不同颜色数量。

$ \Rightarrow $ 戳我进CF原题

E. New Year Tree

time limit per test: 3 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

The New Year holidays are over, but Resha doesn't want to throw away the New Year tree.
He invited his best friends Kerim and Gural to help him to redecorate the New Year tree.

The New Year tree is an undirected tree with $ n $ vertices and root in the vertex $ 1 $.

You should process the queries of the two types:

1.Change the colours of all vertices in the subtree of the vertex $ v $ to the colour $ c $.
2.Find the number of different colours in the subtree of the vertex $ v $.

Input

The first line contains two integers $ n, m (1 ≤ n, m ≤ 4·10^5) $
— the number of vertices in the tree and the number of the queries.

The second line contains $ n $ integers $ c_i (1 ≤ ci ≤ 60) $ — the colour of the $ i $ -th vertex.

Each of the next $ n - 1 $ lines contains two integers $ x_j, y_j (1 ≤ x_j, y_j ≤ n) $ — the vertices of the $ j $ -th edge.
It is guaranteed that you are given correct undirected tree.

The last m lines contains the description of the queries.
Each description starts with the integer $ t_k (1 ≤ t_k ≤ 2) $ — the type of the $ k $ -th query.
For the queries of the first type then follows two integers $ v_k, c_k (1 ≤ v_k ≤ n, 1 ≤ c_k ≤ 60) $
— the number of the vertex whose subtree will be recoloured with the colour $ c_k $ .
For the queries of the second type then follows integer $ v_k (1 ≤ v_k ≤ n) $
— the number of the vertex for which subtree you should find the number of different colours.

Output

For each query of the second type print the integer $ a $
— the number of different colours in the subtree of the vertex given in the query.

Each of the numbers should be printed on a separate line in order of query appearing in the input.

Examples

input1
 7 10
 1 1 1 1 1 1 1
 1 2
 1 3
 1 4
 3 5
 3 6
 3 7
 1 3 2
 2 1
 1 4 3
 2 1
 1 2 5
 2 1
 1 6 4
 2 1
 2 2
 2 3
output1
 2
 3
 4
 5
 1
 2

input2
 23 30
 1 2 2 6 5 3 2 1 1 1 2 4 5 3 4 4 3 3 3 3 3 4 6
 1 2
 1 3
 1 4
 2 5
 2 6
 3 7
 3 8
 4 9
 4 10
 4 11
 6 12
 6 13
 7 14
 7 15
 7 16
 8 17
 8 18
 10 19
 10 20
 10 21
 11 22
 11 23
 2 1
 2 5
 2 6
 2 7
 2 8
 2 9
 2 10
 2 11
 2 4
 1 12 1
 1 13 1
 1 14 1
 1 15 1
 1 16 1
 1 17 1
 1 18 1
 1 19 1
 1 20 1
 1 21 1
 1 22 1
 1 23 1
 2 1
 2 5
 2 6
 2 7
 2 8
 2 9
 2 10
 2 11
 2 4
output2
 6
 1
 3
 3
 2
 1
 2
 3
 5
 5
 1
 2
 2
 1
 1
 1
 2
 3

题目大意

  • 给你一棵树,每个结点刚开始的时候都有一个颜色,

  • 现有操作

  1. $ u \quad col $ :给这个结点及其子树染上 $ col $ 这种颜色
  2. $ u $ :查询以 $ u $ 为根节点的子树的所有颜色种类
  • 颜色数 $ \le 60 \quad n \le 10^5 $

思路

  • 我们看到这道题的颜色最多只有 $ 60 $ 种,所以理所应当的想到突破口就是颜色,
    $ 60 $ 的范围可以考虑状压,其实我们在乎的只是有或者没有该颜色,所以我们可以用或运算来合并两个块

  • 只有修改子树或者查询子树的操作,所以直接 $ dfs $ 序就可以

  • 对 $ dfs $ 序维护一个线段树,支持区间修改区间或,统计区间或之后得到的数在二进制下 $ 1 $ 的个数,即为答案

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long  
#define N 400005
inline int read() {
    register char ch;
    while(!isdigit(ch=getchar()));
    register int x=ch^'0';
    while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
    return x;
}
vector<int>e[N];
int n,m,c[N],id[N],in[N],out[N],tim,sum[N<<2],lzy[N<<2];
void dfs(int u,int fa){
    id[in[u]=++tim]=u;
    for(int i=0;i<e[u].size();++i){
        int v=e[u][i];
        if(v!=fa) dfs(v,u);
    }
    out[u]=tim;
}
void build(int o,int l,int r){
    if(l==r){
        sum[o]=c[id[l]];
        return;
    }
    int mid=l+r>>1;
    build(o<<1,l,mid); build(o<<1|1,mid+1,r);
    sum[o]=sum[o<<1]|sum[o<<1|1]; 
}
inline void pushdown(int o){
    sum[o<<1]=sum[o<<1|1]=lzy[o<<1]=lzy[o<<1|1]=lzy[o];
    lzy[o]=0;
}
void updata(int o,int l,int r,int L,int R,int val){
    if(L<=l&&r<=R){
        sum[o]=val;
        lzy[o]=val;
        return;
    }
    if(lzy[o]) pushdown(o);
    int mid=l+r>>1;
    if(L>mid) updata(o<<1|1,mid+1,r,L,R,val);
    else if(R<=mid) updata(o<<1,l,mid,L,R,val);
    else {
        updata(o<<1,l,mid,L,R,val);
        updata(o<<1|1,mid+1,r,L,R,val);
    }
    sum[o]=sum[o<<1]|sum[o<<1|1];
}
int query(int o,int l,int r,int L,int R){
    if(L<=l&&r<=R) return sum[o];
    if(lzy[o]) pushdown(o);
    int mid=l+r>>1;
    if(L>mid) return query(o<<1|1,mid+1,r,L,R);
    else if(R<=mid) return query(o<<1,l,mid,L,R);
    else return query(o<<1,l,mid,L,R)|query(o<<1|1,mid+1,r,L,R);
    sum[o]=sum[o<<1]|sum[o<<1|1];
}
inline int calc(int x){
    int res=0; 
    while(x){ ++res; x-=x&(-x); }
    return res;
}
signed main(){
    n=read(); m=read();
    for(int i=1;i<=n;++i) c[i]=1ll<<(read()-1);
    for(int i=1;i<n;++i){
        int u=read(),v=read();
        e[u].push_back(v);
        e[v].push_back(u);
    }
    dfs(1,0);
    build(1,1,n);
    while(m--){
        int opt=read(),u=read();
        if(opt==1) updata(1,1,n,in[u],out[u],1ll<<(read()-1));
        else printf("%lld\n",calc(query(1,1,n,in[u],out[u])));
    }
    return 0;
}

转载于:https://www.cnblogs.com/PotremZ/p/CF620E.html

内容概要:本文详细记录了对一个Android ARM64静态ELF文件中字符串加密机制的逆向分析过程。该ELF文件的所有字符串均被加密,无法通过常规strings命令或IDA直接识别。作者通过分析发现,加密字符串存储在.rodata段,其解密所需信息(包括密文地址、长度和16位密钥)保存在.data.rel.ro段的40字节描述符中。核心解密函数sub_10F408采用自反的双pass流密码算法,结合固定密钥KEY_TERM(由.data段24字节数据计算得出),实现字节级非线性、位置与长度相关的加密。文章还复现了完整的Python解密脚本,并揭示了该保护机制的本质为代码混淆而非强加密,最终成功批量解密全部956条字符串,暴露程真实行为,如shell命令模板、设备标识篡改、网络重置等操作。此外,文中还提及未启用的自定义壳框架及其反dump设计。; 适合人群:具备逆向工程基础的安全研究人员、二进制分析人员及对ELF保护技术感兴趣的开发者。; 使用场景及目标:①学习ELF二进制中字符串加密的典型实现方式与逆向突破口;②掌握从结构识别、函数追踪到算法还原的完整逆向流程;③理解“绑定二进制”的完整性校验设计及其局限性;④实践编写IDAPython脚本自动化提取与解密敏感数据。; 阅读建议:此资源以实战案例驱动,不仅展示技术细节,更强调逆向思维与验证方法,建议读者结合IDA调试环境,逐步跟随文中步骤进行动态分析与算法验证,深入理解每一步的推理依据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值