Guest User

SALMAN

a guest
May 27th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. import static java.lang.Integer.min;
  6.  
  7. class SALMAN {
  8.  
  9.     private static int tree[][] = new int[200010][2];
  10.     private static List<Integer> adj[] = new List[100001];
  11.     private static int lazy[] = new int[200010];
  12.     private static int salary[] = new int[100001];
  13.     private static int id[] = new int[100001];
  14.     private static int s[] = new int[100001];
  15.     private static int e[] = new int[100001];
  16.     private static int counter = 1;
  17.     private static int MINIMUM = 0;
  18.     private static int SUM = 1;
  19.     private static int UNDEFINED[] = {Integer.MAX_VALUE, 0};
  20.  
  21.     public static void main(String[] args) {
  22.         FastReader reader = FastReader.SYSTEM_READER;
  23.         int t, n, q, c, v;
  24.         t = reader.nextInt();
  25.         while (t-- > 0) {
  26.             counter = 1;
  27.             reader.nextString();
  28.             n = reader.nextInt();
  29.             q = reader.nextInt();
  30.             initGraph(n);
  31.             for (int i = 1; i <= n; i++) {
  32.                 int p = reader.nextInt();
  33.                 int s = reader.nextInt();
  34.                 salary[i] = s;
  35.                 adj[p].add(i);
  36.             }
  37.             dfs(1);
  38.             buildTree(1, 1, n);
  39.             for (int i = 0; i < q; i++) {
  40.                 c = reader.nextInt();
  41.                 v = reader.nextInt();
  42.                 if (c == 1) {
  43.                     System.out.println(query(1, 1, n , s[v], e[v])[SUM]);
  44.                 } else {
  45.                     int x = min(1000, query(1, 1, n, s[v], e[v])[MINIMUM]);
  46.                     update(1, 0, n - 1, s[v], e[v], x);
  47.                 }
  48.             }
  49.         }
  50.     }
  51.  
  52.     private static void buildTree(int nodeId, int start, int end) {
  53.         if (start == end) {
  54.             tree[nodeId][MINIMUM] = salary[start];
  55.             tree[nodeId][SUM] = salary[id[start]];
  56.             return;
  57.         }
  58.         int left = nodeId << 1;
  59.         int right = (nodeId << 1) + 1;
  60.         int mid = (start + end) / 2;
  61.         buildTree(left, start, mid);
  62.         buildTree(right, mid + 1, end);
  63.         int leftResult[] = tree[left];
  64.         int rightResult[] = tree[right];
  65.         tree[nodeId][MINIMUM] = min(leftResult[MINIMUM], rightResult[MINIMUM]);
  66.         tree[nodeId][SUM] = leftResult[SUM] + rightResult[SUM];
  67.     }
  68.  
  69.     private static void update(int id, int start, int end, int l, int r, int x) {
  70.         if (end < l || start > r) {
  71.             return;
  72.         }
  73.         if (start >= l && end <= r) {
  74.             updateNode(id, start, end, x);
  75.             return;
  76.         }
  77.         shift(id, start, end);
  78.         int left = id << 1;
  79.         int right = (id << 1) + 1;
  80.         int mid = (start + end) / 2;
  81.         update(left, start, mid, l, r, x);
  82.         update(right, mid + 1, end, l, r, x);
  83.         int leftResult[] = tree[left];
  84.         int rightResult[] = tree[right];
  85.         tree[id][MINIMUM] = min(leftResult[MINIMUM], rightResult[MINIMUM]);
  86.         tree[id][SUM] = leftResult[SUM] + rightResult[SUM];
  87.     }
  88.  
  89.     private static int[] query(int id, int start, int end, int l, int r) {
  90.         if (end < l || start > r) {
  91.             return UNDEFINED;
  92.         }
  93.         if (start >= l && end <= r) {
  94.             return tree[id];
  95.         }
  96.         shift(id, start, end);
  97.         int left = id << 1;
  98.         int right = (id << 1) + 1;
  99.         int mid = (start + end) / 2;
  100.         int leftResult[] = query(left, start, mid, l, r);
  101.         int rightResult[] = query(right, mid + 1, end, l, r);
  102.  
  103.         return new int[]{min(leftResult[MINIMUM], rightResult[MINIMUM]), leftResult[SUM] + rightResult[SUM]};
  104.     }
  105.  
  106.     private static void updateNode(int id, int start, int end, int x) {
  107.         lazy[id] += x;
  108.         tree[id][SUM] += (end - start + 1) * x;
  109.         tree[id][MINIMUM] += x;
  110.     }
  111.     private static void shift(int id, int start, int end) {
  112.         int mid = (start + end) / 2;
  113.         updateNode(id << 1, start, mid, lazy[id]);
  114.         updateNode((id << 1) + 1, mid + 1, end, lazy[id]);
  115.         lazy[id] = 0;
  116.     }
  117.  
  118.     private static void dfs(int src) {
  119.         s[src] = counter;
  120.         id[counter] = src;
  121.         int size = adj[src].size();
  122.         for (int i = 0; i < size; i++) {
  123.             int v = adj[src].get(i);
  124.             counter++;
  125.             dfs(v);
  126.         }
  127.         e[src] = counter;
  128.     }
  129.  
  130.     private static void initGraph(int n) {
  131.         for (int i = 0; i <= n; i++) {
  132.             adj[i] = new ArrayList<>();
  133.         }
  134.     }
  135.  
  136.     public static final class FastReader {
  137.         static final FastReader SYSTEM_READER = new FastReader(System.in);
  138.         private final InputStream in;
  139.         private final byte[] buffer = new byte[1 << 16];
  140.         private int pos, count;
  141.  
  142.         public FastReader(InputStream in) {
  143.             this.in = in;
  144.             pos = count = 0;
  145.         }
  146.  
  147.         public int nextInt() {
  148.             int c;
  149.             while ((c = read()) < '0') ;
  150.             int result = c - '0';
  151.             while ((c = read() - '0') >= 0)
  152.                 result = 10 * result + c;
  153.             return result;
  154.         }
  155.  
  156.         public long nextLong() {
  157.             int c;
  158.             while ((c = read()) < '0') ;
  159.             long result = c - '0';
  160.             while ((c = read() - '0') >= 0)
  161.                 result = 10 * result + c;
  162.             return result;
  163.         }
  164.  
  165.         public String nextString() {
  166.             StringBuilder s = new StringBuilder();
  167.             int c;
  168.             while ((c = read()) >= 33)
  169.                 s.append((char) c);
  170.             return s.toString();
  171.         }
  172.  
  173.         private void fillBuffer() {
  174.             try {
  175.                 count = in.read(buffer, pos = 0, buffer.length);
  176.             } catch (Exception e) {
  177.             }
  178.         }
  179.  
  180.         public int read() {
  181.             if (pos == count)
  182.                 fillBuffer();
  183.             return buffer[pos++];
  184.         }
  185.     }
  186. }
Add Comment
Please, Sign In to add comment