High-level ancients

分析:

无论父节点增加了多少,子节点的增量总比父节点多1。

这种差分的关系是保存不变的,我们可以一遍dfs根据结点深度得到在根结点的每个点的系数。

估且把一开始的结点深度称做c0吧,对于子树的修改就只是结点的系数就只是c0+d,d是修正值。

dfs得到树的dfs序列,子树的结点连续,就变成区间更新了。

区间更新的时候,在线段树上保存好初始的系数,修改的时候把系数的lazy标记和普通的lazy标记分开。

这道题学到的新东西:在线段树上不仅可以总体+d,还可以总体增加某一系列特定的系数

这个系数甚至是可变的,感觉这里可以挖掘一下。

/*********************************************************
*            ------------------                          *
*   author AbyssFish                                     *
**********************************************************/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<numeric>
#include<climits>
using namespace std;

typedef long long ll;
const int MAX_N = 5e4+5;
//MAX_P = 1e5 , K <= 1e3
int fa[MAX_N];
int son[MAX_N], bro[MAX_N];

int N;
int dep[MAX_N];

int L[MAX_N], R[MAX_N];
int c[MAX_N]; //dfs_order
int dfs_clk;

void dfs(int u = 1,int d = 1)
{
    c[dfs_clk] = dep[u] = d;
    L[u] = dfs_clk++;
    for(int v = son[u]; v; v = bro[v]){
        dfs(v,d+1);
    }
    R[u] = dfs_clk;
}

#define para int o = 1,int l = 0,int r = N
#define lo (o<<1)
#define ro (o<<1|1)
#define Tvar int md = (l+r)>>1;
#define lsn lo,l,md
#define rsn ro,md,r
#define insd ql <= l && r <= qr

const int ST_SIZE = 1<<17;
ll t_c[ST_SIZE];//O 5e4*5e4/2
ll sum[ST_SIZE];//O 1e5*(1e3*5e4+5e4*5e4/2)
int dwn_c[ST_SIZE];//O 1e5
ll dwn[ST_SIZE];//O (5e4+1e3)*1e5

void build(para)
{
    sum[o] = 0;
    if(r-l == 1){
        t_c[o] = c[l];
    }
    else {
        dwn_c[o] = dwn[o] = 0;
        Tvar
        build(lsn);
        build(rsn);
        t_c[o] = t_c[lo]+t_c[ro];
    }
}

inline void sink_d(int o,ll d,int len)
{
    sum[o] += len*d;
    dwn[o] += d;
}

inline void sink_c(int o,int k)
{
    sum[o] += t_c[o]*k;
    dwn_c[o] += k;
}

inline void push_down(int o,int l,int r)
{
    if(dwn_c[o]){
        sink_c(lo,dwn_c[o]);
        sink_c(ro,dwn_c[o]);
        dwn_c[o] = 0;
    }
    if(dwn[o]){
        Tvar
        sink_d(lo,dwn[o],md-l);
        sink_d(ro,dwn[o],r-md);
        dwn[o] = 0;
    }
}

#define upara d,ql,qr
void update(int d,int ql,int qr,para)
{
    if(insd){
        sink_d(o,d,r-l);//O 5e4+1e3
        sink_c(o,1);
    }
    else {
        Tvar
        push_down(o,l,r);
        if(ql < md) update(upara,lsn);
        if(qr > md) update(upara,rsn);
        sum[o] = sum[lo] + sum[ro];
    }
}

ll query(int ql,int qr,para)
{
    if(insd) return sum[o];
    else {
        Tvar
        push_down(o,l,r);
        ll re = 0;
        if(ql < md) re += query(ql,qr,lsn);
        if(qr > md) re += query(ql,qr,rsn);
        return re;
    }
}

void solve()
{
    int i, P;
    scanf("%d%d",&N,&P);
    memset(son+1,0,sizeof(int)*N);
    for(i = 2; i <= N; i++){
        scanf("%d",fa+i);
        bro[i] = son[fa[i]];
        son[fa[i]] = i;
    }

    dfs_clk = 0;
    dfs();
    build();

    char op[2];
    int u;
    while(P--){
        scanf("%s%d",op,&u);
        if(*op == ‘A‘){
            scanf("%d",&i);
            update(i-dep[u],L[u],R[u]);
        }
        else {
            printf("%lld\n",query(L[u],R[u]));
        }
    }
}

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    int T, kas = 0;
    scanf("%d",&T);
    while(++kas <= T){
        printf("Case #%d:\n",kas);
        solve();
    }
    return 0;
}
时间: 2024-10-22 22:56:44

High-level ancients的相关文章

Demacia of the Ancients

Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a popular multiplayer online battle arena game called Demacia of the Ancients. There are lots of professional teams playing this game. A team will be approved as Level K if there are exact K

修改Android Studio默认的API Level(SDK版本)

原文:修改Android Studio默认的API Level(SDK版本) Android Studio(2.1.2)新建工程的时候只会让你选择最低支持的SDK版本,默认的目标编译SDK版本会以系统当前SDK中最新SDK platform作为目标的API Level.但是很多时候我们并不需要最新的SDK版本,如何修改呢? 方法是:修改工程目录中的Gradle Scripts->build.gradle(Module:app)中的相关行,具体见下图: 当然也可以图形化操作,右键工程目录选择"

maven -- 问题解决(三)Java compiler level does not match the version of the installed Java project facet

问题: Java compiler level does not match the version of the installed Java project facet 解决方法如下: properties->Java Compiler,修改JDK版本,然后Apply

Train Herblore Level with 9% off 500M runescape gold shop on RSorder 8.19-8.24

When an employer conducts a background rs3 gold check on their own they will usually only get a hold of information on the location held, dates associated with employment and in case the candidate is entitled to rehire.The very Runescape Grand Switch

Java [Leetcode 107]Binary Tree Level Order Traversal II

题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order

[LeetCode]Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 re

Binary Tree Zigzag Level Order Traversal

原题: 题目解析:这个问题的实质是要我们按成访问二叉树的结点,并返回每层访问的结果,这里要求走Z字,其实就是一行正向一行反向. /* the kernel idea is visit a binary search tree in level and the additional work we have to label the end of one level. */ vector<vector<int> > zigzagLevelOrder(TreeNode *root) {

Type Java compiler level does not match the version of the installed Java project facet.项目内容没错但是项目上报错,不影响运行

1.Window->Show View->Problems 2.在项目上右键properties->project Facets->修改右侧的version  保持一致 3.window->preferences->Java->Compiler->设置右侧的Compiler compliance level 4.window->preferences->java->Installed JREs->设置或者选择右侧的Installed

Java_异常_02_java.lang.NoClassDefFoundError: org/apache/log4j/Level

总结:解析Json时,除了要导入json-lib-2.2-jdk15.jar外,还要导入: commons-beanutils.jar, commons-httpclient.jar, commons-lang.jar, ezmorph.jar, morph-1.0.1.jar 1.异常1信息: Failed to instantiate SLF4J LoggerFactory Reported exception: java.lang.NoClassDefFoundError: org/apa

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example