J - Assign the task(dfs序+线段树)J

本文探讨了一家公司内部任务分配及其对员工工作流程的影响。通过构建员工组织结构树,并利用线段树优化查询效率,实现对特定员工当前任务的快速获取与更新。此过程涉及公司任务分配、员工下属关系及员工工作状态跟踪,旨在提高工作效率和资源利用。

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66989#overview

J - Assign the task
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

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 subordinates are your subordinates as well. If you are nobody's boss, then you have no subordinates,the employee who has no immediate boss is the leader of whole company.So it means the N employees form a tree. 

The company usually assigns some tasks to some employees to finish.When a task is assigned to someone,He/She will assigned it to all his/her subordinates.In other words,the person and all his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she will stop the current task(if he/she has) and start the new one. 

Write a program that will help in figuring out some employee’s current task after the company assign some tasks to some employee.
 

Input

The first line contains a single positive integer T( T <= 10 ), indicates the number of test cases. 

For each test case: 

The first line contains an integer N (N ≤ 50,000) , which is the number of the employees. 

The following N - 1 lines each contain two integers u and v, which means the employee v is the immediate boss of employee u(1<=u,v<=N). 

The next line contains an integer M (M ≤ 50,000). 

The following M lines each contain a message which is either 

"C x" which means an inquiry for the current task of employee x 

or 

"T x y"which means the company assign task y to employee x. 

(1<=x<=N,0<=y<=10^9)
 

Output

For each test case, print the test case number (beginning with 1) in the first line and then for every inquiry, output the correspond answer per line.
 

Sample Input

     
1 5 4 3 3 2 1 3 5 2 5 C 3 T 2 1 C 3 T 3 2 C 3
 

Sample Output

     
Case #1: -1 1 2
代码是世界上最好的语言~~,AC之路,我选择坚持~~

/**dfs序+线段树:将树映射成区间,最大的根映射最大的区间,从根节点开始,假设其为2
Start【2】=1;映射成线段树第一个区间,根据dfs序依次映射线段树区间2~n(设有n个点)
修改区间,假设修改根2,然后只需修改Start【2】,End【2】;End存的为该子树的先序遍历的最后一个映射到线段树上的区间
总的说一句话就是将树映射到线段树上,改变原有的树和他子树,就是改变线段树上从区间Start到End的值
**/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
using namespace std;
const int SIZE=5e4+10;
const int maxn=500000;
int head[SIZE],tot;
int Start[SIZE],End[SIZE],sum[SIZE<<2],add[SIZE<<2];
bool vis[SIZE];
struct edge{
    int v,next;
}a[SIZE];
void addedge(int u,int v){
    a[tot].v=u;
    a[tot].next=head[v];
    head[v]=tot++;
}
void dfs(int rt){
    Start[rt]=++tot;
    for(int i=head[rt];i!=-1;i=a[i].next){
        dfs(a[i].v);
    }
    End[rt]=tot;
}
void build(int l,int r,int rt){
    sum[rt]=-1;
    add[rt]=0;
    if(l==r)return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
void pushdown(int rt){
    if(!add[rt])return;
    add[rt<<1|1]=add[rt<<1]=add[rt];
    sum[rt<<1|1]=sum[rt<<1]=add[rt];
    add[rt]=0;
}
void update(int L,int R,int c,int l,int r,int rt){
    if(L<=l&&r<=R){
        sum[rt]=c;
        add[rt]=c;
        return;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m)update(L,R,c,lson);
    if(R>m)update(L,R,c,rson);
}
int query(int s,int l,int r,int rt){
    if(l==r)return sum[rt];
    pushdown(rt);
    int m=(l+r)>>1;
    if(s<=m)return query(s,lson);
    else return query(s,rson);
}
int main()
{
    int m,n,T,u,v,x,y,rt;
    char s[10];
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        scanf("%d",&n);
        tot=0;
        memset(head,-1,sizeof(head));
        memset(vis,false,sizeof(vis));
        for(int i=1;i<n;i++){
            scanf("%d%d",&u,&v);
            addedge(u,v);
            vis[u]=true;
        }
        tot=0;
        for(int i=1;i<=n;i++)
            if(!vis[i]){
                dfs(i);
                break;
            }
        build(1,n,1);
        scanf("%d",&m);
        printf("Case #%d:\n",cas);
        while(m--){
            scanf("%s",s);
            if(s[0]=='C'){
                scanf("%d",&rt);
                printf("%d\n",query(Start[rt],1,n,1));
            }
            else {
                scanf("%d%d",&x,&y);
                update(Start[x],End[x],y,1,n,1);
            }
        }
    }
    return 0;
}


你当前有一个名为 `ev_auto_assign_sorting_batch` 的事件,目前设置为 **每 30 秒运行一次**。现在你想将它修改为 **每 10 秒运行一次**。 --- ## ✅ 使用 `ALTER EVENT` 修改为每 10 秒执行一次 ```sql ALTER EVENT ev_auto_assign_sorting_batch ON SCHEDULE EVERY 10 SECOND DO BEGIN CALL AutoAssignSortingBatch(); END; ``` > ⚠️ 注意:如果你的存储过程名不是 `AutoAssignSortingBatch`,请替换为实际名称。 --- ### 🔍 解释: - `ALTER EVENT ev_auto_assign_sorting_batch`:修改指定事件。 - `ON SCHEDULE EVERY 10 SECOND`:把原间隔从 30 秒改为 10 秒。 - `DO BEGIN ... END`:事件要执行的内容,这里保持调用原存储过程不变。 --- ## ✅ 验证是否修改成功 ```sql SHOW CREATE EVENT ev_auto_assign_sorting_batch\G ``` 输出中你应该看到: ```text Create Event: CREATE DEFINER=`root`@`localhost` EVENT `ev_auto_assign_sorting_batch` ON SCHEDULE EVERY 10 SECOND ... ``` 确认 `EVERY 10 SECOND` 已生效。 --- ## 📌 补充说明 | 项目 | 说明 | |------|------| | 最小调度粒度 | MySQL 支持最小到 `SECOND`,所以 10 秒是完全合法的 | | 是否允许小于 1 秒? | ❌ 不支持,如 `0.5 SECOND` 是语法错误 | | 如果任务耗时超过 10 秒怎么办? | 后续任务会排队或跳过(取决于设置),建议在高频率下加入防重逻辑 | --- ## ✅ 可选:添加防止并发执行的保护机制 为了避免“上一个未结束,下一个又开始”导致数据冲突,可以在存储过程中加锁控制: ```sql -- 在事件调用前尝试获取命名锁 ALTER EVENT ev_auto_assign_sorting_batch ON SCHEDULE EVERY 10 SECOND DO BEGIN IF GET_LOCK('auto_assign_lock', 0) = 1 THEN CALL AutoAssignSortingBatch(); DO RELEASE_LOCK('auto_assign_lock'); END IF; END; ``` > 🔐 `GET_LOCK('auto_assign_lock', 0)`:尝试立即获取锁,失败则跳过本次执行。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值