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

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →

Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →