codeforces 384e

本文介绍了一种使用树状数组和深度优先搜索(DFS)序解决特定树形结构问题的方法。通过将树上的点转化为DFS序,利用两个树状数组分别处理不同类型的更新,巧妙地解决了树上节点权值的更新与查询问题。

题目大意:

给定n个点,m个询问的无向树(1为根)

下面n个数表示每个点的权值

下面n-1行给出树

操作1:x点权值+v, x的第 i & 1 的儿子-v, 第 !(i&1) 的儿子+v

操作2:询问x点权值

思路:把树上的点用dfs序转换一下就可以用树状数组做了,不过因为更新时有两种更新同时进行所以需要用两个树状数组分别更新就行,很巧妙的树状数组运用

#include <iostream>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int maxn= 200000+10;
const int mod=1e9+7;
const double en=2.718281828459;
using namespace std;
#define inf (ll)1<<60
struct st{
int val,l,r,h;
}q[maxn];
vector<int>g[maxn];
int n,m,cnt,fg[maxn],tree[3][maxn];
void dfs(int u,int h){
    cnt++;
    q[u].l=cnt;
    q[u].h=h;
    fg[u]=1;
    for(int i=0;i<g[u].size();i++){
        int v=g[u][i];
        if(fg[v]==0)
            dfs(v,(h+1)%2);
    }
    q[u].r=cnt;
}
void add(int k,int num,int h){//对k位置的数加上num
    while(k<=n){
        tree[h][k]+=num;
        k+=k&(-k);
    }
}
int read(int k,int h){//查询一到k位置a[i]的和
    int sum=0;
    while(k){
        sum+=tree[h][k];
        k-=k&(-k);
    }
    return sum;
}
int main() {
// freopen("in.txt","r",stdin);
    cin>>n>>m;
    int i;
    for(i=1;i<=n;i++)
        scanf("%d",&q[i].val);
    int a,b,c;
    for(i=1;i<=n-1;i++){
        scanf("%d%d",&a,&b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1,0);
    for(i=1;i<=m;i++){
        scanf("%d%d",&a,&b);
        if(a==1){
            scanf("%d",&c);
            add(q[b].l,c,q[b].h);
            add(q[b].r+1,-c,q[b].h);
            add(q[b].l,-c,(q[b].h+1)%2);
            add(q[b].r+1,c,(q[b].h+1)%2);
        }
        else printf("%d\n",q[b].val+read(q[b].l,q[b].h));

    }

    return 0;
}

 

转载于:https://www.cnblogs.com/shimu/p/5750720.html

多角色体系 支持管理员、商家、消费者三种角色,权限分级管控: 管理员:负责平台整体配置、用户审核、数据监控等全局操作。 商家:管理店铺信息、发布商品、处理订单、回复评价等。 消费者:浏览商品、加入购物车、下单支付、评价商品等。 实现用户注册(手机号 / 邮箱验证)、登录(支持密码 / 验证码 / 第三方登录)、个人信息管理(头像、收货地址、密码修改)。 权限精细化控制 商家仅能管理自家店铺及商品,消费者仅能查看和购买商品,管理员拥有全平台数据访问权限。 二、商品管理功能 商品信息维护 商家可发布商品:填写名称、分类(如服饰、电子产品)、子类别(如手机、笔记本)、规格(尺寸、颜色、型号)、价格、库存、详情描述(图文)、物流信息(运费、发货地)等。 支持商品上下架、库存调整、信息编辑,系统自动记录商品状态变更日志。 商品分类与搜索 按多级分类展示商品(如 “数码产品→手机→智能手机”),支持自定义分类体系。 提供智能搜索功能:按关键词(名称、品牌)搜索,支持模糊匹配和搜索联想;结合用户浏览历史对搜索结果排序(优先展示高相关度商品)。 商品推荐 基于用户浏览、收藏、购买记录,推荐相似商品(如 “浏览过该商品的用户还买了…”)。 首页展示热门商品(销量 TOP10)、新品上架、限时折扣等推荐列表。 三、订单与交易管理 购物车与下单 消费者可将商品加入购物车,支持修改数量、选择规格、移除商品,系统自动计算总价(含运费、折扣)。 下单流程:确认收货地址→选择支付方式(在线支付、货到付款)→提交订单→系统生成唯一订单号。 订单处理流程 订单状态跟踪:待支付→已支付→商家发货→物流运输→消费者收货→订单完成,各状态变更实时通知用户。 商家端功能:查看新订单提醒、确认发货(填写物流单号)、处理退款申请(需审核理由)。 消费者端功能:查看订单详情、追踪物流、申请退款 / 退货、确认收货。
### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值