Heavy Light 2 White Falcon


Problem Statement :


White Falcon was amazed by what she can do with heavy-light decomposition on trees. As a resut, she wants to improve her expertise on heavy-light decomposition. Her teacher gave her an another assignment which requires path updates. As always, White Falcon needs your help with the assignment.

You are given a tree with N  nodes and each node's value Vi is initially 0.

Let's denote the path from node u to node v  like this: p1, p2, p3 . . . pk, where p1 = u  and pk = v, and pi and   pi+1 are connected.

The problem asks you to operate the following two types of queries on the tree:

"1 u v x" Add  x to  valp1, 2x to  valp2, 3x to valp3 , ...,  kx to valpk.
"2 u v" print the sum of the nodes' values on the path between u and v  at modulo 10^9 + 7 .

Input Format

First line cosists of two integers N and Q seperated by a space.
Following N - 1  lines contains two integers which denote the undirectional edges of the tree.
Following   Q lines contains one of the query types described above.

Note: Nodes are numbered by using 0-based indexing.

Constraints

1  <=  N , Q  <=  50000
0  <=  x  <= 10^9 + 7

Output Format

For every query of second type print a single integer.



Solution :



title-img


                            Solution in C :

In   C++  :







#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cassert>

using namespace std;

const long long mod = 1000000007ll;
const long long inv2 = 500000004ll;
const int N = 50010;

long long calc(long long _n, long long _a, long long _d) {
    long long ans = (_a + _a + (_n-1) * _d) % mod;
    ans = (ans * _n) % mod;
    ans = (ans * inv2) % mod;
    return ans;
}

class node {
  public:
    int l, r;
    long long sum, a, d;
    node *left, *right;
    
    void propagate(void) {
        if(a == 0 and d == 0) return;
        int mid = (l + r) / 2;
        left->update(l, mid, a, d);
        long long nw_a = (a + (mid - l + 1) * d) % mod; 
        right->update(mid+1, r, nw_a, d);
        sum = (left->sum + right->sum) % mod;
        a = d = 0ll;
    }
    
    void update(int x, int y, long long _a, long long _d) {
        if(y < l or r < x) return;
        if(x <= l and r <= y) {
            sum = (sum + calc(r - l + 1, _a, _d)) % mod;
            a = (a + _a) % mod;
            d = (d + _d) % mod;
            return;
        }
        propagate();
        int mid = (l + r) / 2;
        if(y <= mid) {
            left->update(x, y, _a, _d);
        }else if(mid < x) {
            right->update(x, y, _a, _d);
        }else {
            left->update(x, mid, _a, _d);
            long long nw_a = (_a + (mid - x + 1) * _d) % mod;
            right->update(mid+1, y, nw_a, _d);
        }
        sum = (left->sum + right->sum) % mod;
    }
    
    long long query(int x, int y) {
        if(y < l or r < x) return 0ll;
        if(x <= l and r <= y) return sum;
        propagate();
        return (left->query(x, y) + right->query(x, y)) % mod;
    }
    
    node(int _l, int _r) : l(_l), r(_r), sum(0ll), a(0ll), d(0ll) {}
};

node* init(int l, int r) {
    node *p = new node(l, r);
    if(l < r) {
        int mid = (l + r) / 2;
        p->left = init(l, mid);
        p->right = init(mid+1, r);
    }
    return p;
}

vector<int> adj[N];
int n, q;

vector<int> Path[N];
node* head[N];
int G[N], H[N], P[N], pos[N], sz[N];

void dfs_init(int u, int p, int h) {
    H[u] = h;
    P[u] = p;
    sz[u] = 1;
    for(int v : adj[u]) {
        if(v == p) continue;
        dfs_init(v, u, h+1);
        sz[u] += sz[v];
    }
}
void dfs_HLD(int u) {
    Path[u].push_back(u);
    for(int i = 0;i < Path[u].size();i++) {
        int v = Path[u][i];
        G[v] = u;
        pos[v] = i;
        for(int vv : adj[v]) {
            if(vv == P[v]) continue;
            if(2*sz[vv] >= sz[v]) {
                Path[u].push_back(vv);
            }else {
                dfs_HLD(vv);
            }
        }
    } 
    head[G[u]] = init(0, Path[u].size() - 1);
}
int lca(int u, int v) {
    while(G[u] != G[v]) {
        if(H[G[u]] < H[G[v]]) swap(u, v);
        u = P[G[u]];
    }
    return pos[u] < pos[v] ? u : v;
}
void update(int u, int v, long long a, long long d) {
    int l = lca(u, v);
    while(G[u] != G[l]) {
        a = (a + (pos[u] + 1) * d) % mod;      
        head[G[u]]->update(0, pos[u], (a-d+mod) % mod, mod-d);
        u = P[G[u]];
    }
    if(pos[l] + 1 <= pos[u]) {
        a = (a + (pos[u] - pos[l]) * d) % mod;
        head[G[u]]->update(pos[l]+1, pos[u], (a-d+mod) % mod, mod-d);
    }
    long long nw_a = (a + (H[v] - H[l]) * d) % mod, nw_d = mod - d;
    while(G[v] != G[l]) {
        nw_a = (nw_a + (pos[v] + 1) * nw_d) % mod;
        head[G[v]]->update(0, pos[v], (nw_a + d) % mod, d);
        v = P[G[v]];
    }
    head[G[v]]->update(pos[l], pos[v], a, d);
    nw_a = (nw_a + (pos[v] - pos[l]) * nw_d) % mod;
    assert(a == nw_a);
}
long long query(int u, int v) {
    long long ans = 0ll;
    while(G[u] != G[v]) {
        if(H[G[u]] < H[G[v]]) {
            swap(u, v);
        }
        ans = (ans + head[G[u]]->query(0, pos[u])) % mod;
        u = P[G[u]];
    }
    if(pos[u] > pos[v]) swap(u, v);
    ans = (ans + head[G[u]]->query(pos[u], pos[v])) % mod;
    ans = (ans + mod) % mod;
    return ans;
}
int main() {

    ios::sync_with_stdio(false);

    cin >> n >> q;
    for(int i = 0;i < n-1;i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    
    dfs_init(0, 0, 0);
    dfs_HLD(0);
    
    for(int i = 0;i < q;i++) {
        int type;
        cin >> type;
        if(type == 1) {
            int u, v;
            long long x;
            cin >> u >> v >> x;
            update(u, v, x, x);
        }else {
            int u, v;
            cin >> u >> v;
            cout << query(u, v) << "\n";
        }
    }
    
    return 0;
}









In   Java  :







import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;

public class Solution {
    static List<Integer>[] conn;
    static long[] v;
    static long[] ov;
    static int[] d;
    static int[] p;
    static int count;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int q = sc.nextInt();
        int r = 0;
        long time = System.currentTimeMillis();
        count = n;
        conn = new List[n + 1];
        v = new long[n + 1];
        ov = new long[n + 1];
        d = new int[n + 1];
        p = new int[n + 1];

        for (int i = 0; i < n - 1; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();

            List<Integer> xconn = conn[x];
            if (xconn == null) {
                xconn = new ArrayList<Integer>();
                conn[x] = xconn;
            }

            List<Integer> yconn = conn[y];
            if (yconn == null) {
                yconn = new ArrayList<Integer>();
                conn[y] = yconn;
            }

            xconn.add(y);
            yconn.add(x);
        }

        d[r] = 1;
        treefy3(r);

        for (int i = 0; i < q; i++) {
            String uq = sc.next();

            if ("1".equals(uq)) {
                int u = sc.nextInt();
                int v = sc.nextInt();
                int x = sc.nextInt();

                update2(u, v, x);
            } else {
                int u = sc.nextInt();
                int v = sc.nextInt();

                long s = sum(u, v);
                System.out.println(s % 1000000007);
            }
        }

//        System.out.println(System.currentTimeMillis() - time);

    }

    private static void update(int un, int vn, long x) {
        // List<Integer> vl = new ArrayList<>(count);

        int[] va = new int[count];
        int idx = 0;

        int ud = d[un];
        int vd = d[vn];

        int rn = un;
        boolean isUnUsed = true;
        if (ud <= vd) {
            rn = vn;
            isUnUsed = false;
        }

        int k = 1;
        int abs = Math.abs(vd - ud);
        for (int i = 0; i < abs; i++) {
            if (isUnUsed) {
                // v[rn] += k++ * x;
                v[rn] = (v[rn] + k++ * x) % 1000000007;
            } else {
                // vl.add(rn);
                va[idx++] = rn;

                ov[rn] = v[rn];
                v[rn] = (v[rn] + (ud + d[rn] - 1) * x) % 1000000007;
            }
            rn = p[rn];
        }

        if (d[un] <= d[vn]) {
            vn = rn;
        } else {
            un = rn;
        }

        while (un != vn) {
            // v[un] += k++ * x;
            v[un] = (v[un] + k++ * x) % 1000000007;
            // vl.add(vn);
            va[idx++] = vn;
            ov[vn] = v[vn];
            v[vn] = (v[vn] + (ud + d[vn] - 1) * x) % 1000000007;
            un = p[un];
            vn = p[vn];
        }

        // v[un] += k++ * x;
        v[un] = (v[un] + k++ * x) % 1000000007;

        int vs = idx;

        if (d[un] != 1) {
            for (int i = vs - 1; i >= 0; i--) {
                int n = va[i];
                // n.value += k++ * x;
                v[n] = (ov[n] + k++ * x) % 1000000007;
            }
        }
    }

    private static void update2(int un, int vn, long x) {
        int ud = d[un];
        int vd = d[vn];

        int uns = un;
        int vns = vn;

        int k = 1;
        if (ud > vd) {
            int abs = ud - vd;
            for (int i = 0; i < abs; i++) {
                v[uns] = (v[uns] + k++ * x) % 1000000007;
                uns = p[uns];
            }
        }else{
            int abs = vd - ud;
            for (int i = 0; i < abs; i++) {
                vns = p[vns];
            }
        }

        while (uns != vns) {
            // v[un] += k++ * x;
            v[uns] = (v[uns] + k++ * x) % 1000000007;
            // vl.add(vn);
            uns = p[uns];
            vns = p[vns];
        }

        // v[un] += k++ * x;
        v[uns] = (v[uns] + k * x) % 1000000007;

        int tt = k + (d[vn] - d[vns]);

        while(vns != vn) {
            
            // n.value += k++ * x;
            v[vn] = (v[vn] + tt-- * x) % 1000000007;
            vn = p[vn];
        }
    }

    private static long sum(int un, int vn) {
long sum = 0;

int rn = un;
if (d[un] <= d[vn]) {
rn = vn;
}

int abs = Math.abs(d[vn] - d[un]);
for (int i = 0; i < abs; i++) {
sum += v[rn];
rn = p[rn];
}

if (d[un] <= d[vn]) {
vn = rn;
} else {
un = rn;
}

while (un != vn) {
sum += v[un] + v[vn];
un = p[un];
vn = p[vn];
}
sum += v[un];

return sum;
}

static int c = 1;

private static void treefy2(int rn) {
Queue<Integer> q = new ArrayDeque<>();
q.add(rn);

while (!q.isEmpty()) {
int n = q.poll();

int s = conn[n].size();
int dd = d[n] + 1;
for (int i = 0; i < s; i++) {
int cn = conn[n].get(i);

if (d[cn] == 0) {
p[cn] = n;
d[cn] = dd;
q.add(cn);
}
}
}
}

private static void treefy3(int rn) {
int[] iq = new int[count];
int idx = 0;
iq[idx] = rn;

while (idx >= 0) {
int n = iq[idx];
idx--;
int s = conn[n].size();
int dd = d[n] + 1;
for (int i = 0; i < s; i++) {
int cn = conn[n].get(i);

if (d[cn] == 0) {
p[cn] = n;
d[cn] = dd;
idx++;
iq[idx] = cn;
}
}
}
}
}









In  Python3 :






from operator import attrgetter

MOD = 10**9 + 7

def solve(edges, queries):
    nodes, leaves = make_tree(edges)
    hld(leaves)

    results = []
    for query in queries:
        if query[0] == 1:
            update(nodes[query[1]], nodes[query[2]], query[3])
        elif query[0] == 2:
            results.append(sum_range(nodes[query[1]], nodes[query[2]]))

    return results

def make_tree(edges):
    nodes = [
        Node(i)
        for i in range(len(edges) + 1)
    ]

    # the tree is a graph for now
    # as we don't know the direction of the edges
    for edge in edges:
        nodes[edge[0]].children.append(nodes[edge[1]])
        nodes[edge[1]].children.append(nodes[edge[0]])

    # pick the root of the tree
    root = nodes[0]
    root.depth = 0

    # for each node, remove its parent of its children
    stack = []
    leaves = []
    for child in root.children:
        stack.append((child, root, 1))
    for node, parent, depth in stack:
        node.children.remove(parent)
        node.parent = parent
        node.depth = depth

        if len(node.children) == 0:
            leaves.append(node)
            continue

        for child in node.children:
            stack.append((child, node, depth + 1))

    return nodes, leaves


def hld(leaves):
    leaves = sorted(leaves, key=attrgetter('depth'), reverse=True)

    for leaf in leaves:
        leaf.chain = Chain()
        leaf.chain_i = 0

        curr_node = leaf
        while curr_node.parent is not None:
            curr_chain = curr_node.chain
            if curr_node.parent.chain is not None:
                curr_chain.init_fenwick_tree()
                curr_chain.parent = curr_node.parent.chain
                curr_chain.parent_i = curr_node.parent.chain_i
                break

            curr_node.parent.chain = curr_chain
            curr_node.parent.chain_i = curr_chain.size
            curr_node.chain.size += 1
            curr_node = curr_node.parent

        if curr_node.parent is None:
            curr_chain.init_fenwick_tree()


def update(node1, node2, x):
    path_len = 0
    chain1 = node1.chain
    chain_i1 = node1.chain_i
    depth1 = node1.depth
    chains1 = []
    chain2 = node2.chain
    chain_i2 = node2.chain_i
    depth2 = node2.depth
    chains2 = []

    while chain1 is not chain2:
        step1 = chain1.size - chain_i1
        step2 = chain2.size - chain_i2

        if depth1 - step1 > depth2 - step2:
            path_len += step1
            chains1.append((chain1, chain_i1))
            depth1 -= step1
            chain_i1 = chain1.parent_i
            chain1 = chain1.parent
        else:
            path_len += step2
            chains2.append((chain2, chain_i2))
            depth2 -= step2
            chain_i2 = chain2.parent_i
            chain2 = chain2.parent

    path_len += abs(chain_i1 - chain_i2) + 1

    curr_val1 = 0
    for (chain, chain_i) in chains1:
        chain.ftree.add(chain_i, chain.size-1, curr_val1, x)
        curr_val1 += (chain.size - chain_i) * x

    curr_val2 = (path_len + 1) * x
    for (chain, chain_i) in chains2:
        chain.ftree.add(chain_i, chain.size-1, curr_val2, -x)
        curr_val2 -= (chain.size - chain_i) * x

    if chain_i1 <= chain_i2:
        chain1.ftree.add(chain_i1, chain_i2, curr_val1, x)
    else:
        chain1.ftree.add(chain_i2, chain_i1, curr_val2, -x)


def sum_range(node1, node2):
    sum_ = 0
    chain1 = node1.chain
    chain_i1 = node1.chain_i
    depth1 = node1.depth
    chain2 = node2.chain
    chain_i2 = node2.chain_i
    depth2 = node2.depth
    while chain1 is not chain2:
        step1 = chain1.size - chain_i1
        step2 = chain2.size - chain_i2
        if depth1 - step1 > depth2 - step2:
            sum_ += chain1.ftree.range_sum(chain_i1, chain1.size - 1)

            depth1 -= step1
            chain_i1 = chain1.parent_i
            chain1 = chain1.parent
        else:
            sum_ += chain2.ftree.range_sum(chain_i2, chain2.size - 1)

            depth2 -= step2
            chain_i2 = chain2.parent_i
            chain2 = chain2.parent

    if chain_i1 > chain_i2:
        chain_i1, chain_i2 = chain_i2, chain_i1

    sum_ += chain1.ftree.range_sum(chain_i1, chain_i2)

    return int(sum_ % MOD)

class Node():
    __slots__ = ['i', 'val', 'parent', 'children', 'depth', 'chain', 'chain_i']

    def __init__(self, i):
        self.i = i
        self.val = 0
        self.parent = None
        self.depth = None
        self.children = []
        self.chain = None
        self.chain_i = -1


class Chain():
    __slots__ = ['size', 'ftree', 'parent', 'parent_i']

    def __init__(self):
        self.size = 1
        self.ftree = None
        self.parent = None
        self.parent_i = -1

    def init_fenwick_tree(self):
        self.ftree = RURQFenwickTree(self.size)

def g(i):
    return i & (i + 1)

def h(i):
    return i | (i + 1)

class RURQFenwickTree():
    def __init__(self, size):
        self.tree1 = RUPQFenwickTree(size)
        self.tree2 = RUPQFenwickTree(size)
        self.tree3 = RUPQFenwickTree(size)

    def add(self, l, r, k, x):
        k2 = k * 2
        self.tree1.add(l, x)
        self.tree1.add(r+1, -x)
        self.tree2.add(l, (3 - 2*l) * x + k2)
        self.tree2.add(r+1, -((3 - 2*l) * x + k2))
        self.tree3.add(l, (l**2 - 3*l + 2) * x + k2 * (1 - l))
        self.tree3.add(r+1, (r**2 + 3*r - 2*r*l) * x + k2 * r)

    def prefix_sum(self, i):
        sum_ = i**2 * self.tree1.point_query(i)
        sum_ += i * self.tree2.point_query(i)
        sum_ += self.tree3.point_query(i)

        return ((sum_ % (2 * MOD)) / 2) % MOD

    def range_sum(self, l, r):
        return self.prefix_sum(r) - self.prefix_sum(l - 1)

class RUPQFenwickTree():
    def __init__(self, size):
        self.size = size
        self.tree = [0] * size

    def add(self, i, x):
        j = i
        while j < self.size:
            self.tree[j] += x
            j = h(j)

    def point_query(self, i):
        res = 0
        j = i
        while j >= 0:
            res += self.tree[j]
            j = g(j) - 1

        return res

if __name__ == '__main__':
    nq = input().split()

    n = int(nq[0])

    q = int(nq[1])

    tree = []

    for _ in range(n-1):
        tree.append(list(map(int, input().rstrip().split())))

    queries = []

    for _ in range(q):
        queries.append(list(map(int, input().rstrip().split())))

    results = solve(tree, queries)

    print('\n'.join(map(str, results)))
                        








View More Similar Problems

Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →