HDU - 3974 Assign the task (线段树区间修改+构建模型)

https://cn.vjudge.net/problem/HDU-3974

题意

有一棵树,给一个结点分配任务时,其子树的所有结点都能接受到此任务。有两个操作,C x表示查询x结点此时任务编号,T x y表示给x结点分配编号为y的任务。

分析

题目读起来就很有区间修改的味道,将一个区间变为一个值。问题在于怎么把这棵树对应到区间上。

对于一个结点,其控制的范围是它的子树,对应区间范围可以看作是以dfs序表示的区间。好像有点绕。。就是给每个结点再对应一个dfs序,然后在dfs时把这个点控制的子树看作是一段连续区间。然后就跑线段树啦。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = 5e4 + 10;
const int maxm = 200000 + 10;
const int mod = 998244353;

struct Edge{
    int to,nxt;
    Edge(){}
    Edge(int x,int y):to(x),nxt(y){}
}e[maxn];
int head[maxn],tot;
int cnt,start[maxn],ed[maxn];
void init(){
    tot=cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v){
    e[tot]=Edge(v,head[u]);head[u]=tot++;
}
void dfs(int u){
    ++cnt;
    start[u]=cnt;
    for(int i=head[u];~i;i=e[i].nxt){
        dfs(e[i].to);
    }
    ed[u]=cnt;
}
struct ND{
    int l,r;
    int val,lazy;
}tree[maxn<<2];
int n,m;
void pushup(int rt){
}
void pushdown(int rt){
    if(tree[rt].lazy){
        tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy;
        tree[rt<<1].val=tree[rt<<1|1].val=tree[rt].val;
        tree[rt].lazy=0;
    }
}
void build(int rt,int l,int r){
    tree[rt].l=l,tree[rt].r=r;
    tree[rt].lazy=0;
    tree[rt].val=-1;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
}
void update(int rt,int L,int R,int val){
    if(L<=tree[rt].l&&tree[rt].r<=R){
        tree[rt].val=val;
        tree[rt].lazy=1;
        return;
    }
    pushdown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(mid>=L) update(rt<<1,L,R,val);
    if(mid<R) update(rt<<1|1,L,R,val);
}

int query(int rt,int x){
    if(tree[rt].l==x&&tree[rt].r==x){
        return tree[rt].val;
    }
    pushdown(rt);
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(mid>=x) return query(rt<<1,x);
    else return query(rt<<1|1,x);
}
bool used[maxn];
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    int t,cas=1;
    char op[5];
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        printf("Case #%d:\n",cas++);
        init();
        int u,v;
        memset(used,false,sizeof(used));
        for(int i=1;i<n;i++){
            scanf("%d%d",&u,&v);
            used[u]=true;
            addedge(v,u);
        }
        for(int i=1;i<=n;i++)
            if(!used[i])
                dfs(i);
        build(1,1,cnt);
        scanf("%d",&m);
        while(m--){
            scanf("%s",op);
            if(op[0]==‘C‘){
                scanf("%d",&u);
                printf("%d\n",query(1,start[u]));
            }else{
                scanf("%d%d",&u,&v);
                update(1,start[u],ed[u],v);
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/fht-litost/p/9573636.html

时间: 2024-10-06 08:50:01

HDU - 3974 Assign the task (线段树区间修改+构建模型)的相关文章

HDU 3974 Assign the task(线段树)

描述There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all his subordin

hdu 3974 Assign the task 线段树 DFS序

给你一棵树,每次修改一个子树的所有值,然后单点查询. 按照DFS序把节点排列(即在DFS中出现的先后次序),同一个子树在序列中连续. 1 #include <cstdio> 2 using namespace std; 3 typedef long long ll; 4 int n,q,T,Tc,cnt,sum; 5 int col[210000],lzy[210000],sta[51000],fin[51000]; 6 int nxt[51000],to[51000],head[51000]

hdu 4902 Nice boat(线段树区间修改,输出最终序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 Problem Description There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his peopl

HDU 4902 Nice boat(线段树 区间更新)

Nice boat 大意:给你一个区间,每次可以进行两种操作,1:把区间中的数全都变成x  2:把区间中大于x的数变成gcd(a[i], x),最后输出序列. 思路:线段树成段更行,用num数组的叶子存储数据,节点当作lazy来使用. 1 #include <stdio.h> 2 const int maxn = 100005; 3 4 int num[maxn<<2]; 5 6 int gcd(int a, int b){ 7 return b?gcd(b, a%b):a; 8

HDU 1698 Just a Hook (线段树,区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17214    Accepted Submission(s): 8600 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f

HDU - 1698 Just a Hook (线段树区间修改)

Description In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. Now Pudge wants to do some operations on the hook.

HDU 1698 Just a Hook(线段树区间替换)

题目地址:HDU 1698 区间替换裸题.同样利用lazy延迟标记数组,这里只是当lazy下放的时候把下面的lazy也全部改成lazy就好了. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #in

hdu 6444 Neko&#39;s loop 线段树区间更新

题目连接:Neko's loop 题意:给一个长度为n的环,下标从0~n-1,环上每个点有个值表示到这个点会得到的快乐值.,然后每次可以花费1能量往后跳k步.你可以选择任意点开始跳,可以任意点结束,最多跳m次问得到至少s的快乐值最初要拥有多少. 题解:先把循环节挑出来,,然后在循环节上找最大字段和.循环节长度为cnt,然后就是枚举起点用线段树维护前缀和,然后取最大值. #include<bits/stdc++.h> #define ls l,m,rt<<1 #define rs m

杭电 1698 Just a Hook(线段树区间修改)

http://acm.hdu.edu.cn/showproblem.php?pid=1698 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17322    Accepted Submission(s): 8640 Problem Description In the game of DotA, Pudge's