CodeForces - 292E 线段树

题意:给a,b两个数组, 然后有m个操作,t 如果t=2 ,接着输入一个k ,然后输出b【k】;如果t =1,接着输入3个数,x,y,k,表示b【y】到b【y+k-1】等于

a【x】到a【x+k-1】。

线段树设置两个变量,flag1 表示这段区间被x占用,flag2表示这段区间从y位置开始,

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define FF freopen("Input.txt","r",stdin)
const int N=1e5+2;
int a[N],b[N],id,id1;
struct Node
{
    int l,r;
    int flag1,flag2;
    int mid()
    {
        return (l+r)>>1;
    }
}Tree[N*4];
void Build(int rt,int l,int r)
{
    Tree[rt].l=l;
    Tree[rt].r=r;
    Tree[rt].flag1=0;
    if(l==r) return ;
    int mid=Tree[rt].mid();
    Build(rt<<1,l,mid);
    Build(rt<<1|1,mid+1,r);
}
void Pushdown(int rt)
{
    Tree[rt<<1].flag1=Tree[rt<<1|1].flag1=Tree[rt].flag1;
    Tree[rt<<1].flag2=Tree[rt<<1|1].flag2=Tree[rt].flag2;
    Tree[rt].flag1=0;
    Tree[rt].flag2=0;
}
void Update(int rt,int l,int r,int val1,int val2)
{
    if(Tree[rt].l==l&&Tree[rt].r==r)
    {
        Tree[rt].flag1=val1;
        Tree[rt].flag2=val2;
        return ;
    }
    if(Tree[rt].flag1!=0) Pushdown(rt);
    int mid=Tree[rt].mid();
    if(l>mid) Update(rt<<1|1,l,r,val1,val2);
    else if(r<=mid) Update(rt<<1,l,r,val1,val2);
    else
    {
        Update(rt<<1,l,mid,val1,val2);
        Update(rt<<1|1,mid+1,r,val1,val2);
    }
}
void Query(int rt,int k)
{
    if(Tree[rt].flag1!=0) { id=Tree[rt].flag1;id1=Tree[rt].flag2;return ;}
    if(Tree[rt].l==Tree[rt].r&&Tree[rt].flag1==0)
    {  id=0; return ;}
    int mid=Tree[rt].mid();
    if(k>mid)  Query(rt<<1|1,k);
    else  Query(rt<<1,k);
}
int main()
{
    //FF;
    int n,m,i;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&b[i]);
        Build(1,1,n);
        int x,y,k,w;
        while(m--)
        {
            scanf("%d",&w);
            if(w==1)
            {
                scanf("%d%d%d",&x,&y,&k);
                Update(1,y,y+k-1,x,y);
            }
            else
            {
                scanf("%d",&k);
                Query(1,k);
                if(id==0)
                    printf("%d\n",b[k]);
                else
                    printf("%d\n",a[id+k-id1]);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值