Kaidul

Untitled

Dec 31st, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.25 KB | None | 0 0
  1. //  Created by Kaidul Islam on 18/4/16.
  2. //  Copyright © 2016 Kaidul Islam. All rights reserved.
  3. //
  4. //
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. #define rep(i, n) for(__typeof(n) i = 0; i < (n); i++)
  10. #define rrep(i, n) for(__typeof(n) i = (n) - 1; i >= 0; --i)
  11. #define rep1(i, n) for(__typeof(n) i = 1; i <= (n); i++)
  12. #define FOR(i, a, b) for(__typeof(b) i = (a); i <= (b); i++)
  13. #define forstl(i, s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
  14. #define SZ(v) ((int)v.size())
  15. #define INF (1 << 30)
  16. #define PI acos(-1.0)
  17. #define bitcnt __builtin_popcount
  18. #define pb push_back
  19. #define ppb pop_back
  20. #define all(x) x.begin(), x.end()
  21. #define mem(x, y) memset(x, y, sizeof x)
  22. #define eps 1e-9
  23. #define pii pair<int, int>
  24. #define couple make_pair
  25. #define X first
  26. #define Y second
  27. #define vi vector<int>
  28. #define vpii vector< pii >
  29. #define si set<int>
  30. #define SDi(x) sf("%d", &x)
  31. #define SD2(x, y) sf("%d %d", &x, &y)
  32. #define SD3(x, y, z) sf("%d %d %d", &x, &y, &z)
  33. #define SDs(x) sf("%s", x)
  34. #define pf printf
  35. #define print(x) pf("%d ", x)
  36. #define println(x) pf("%d\n", x)
  37. #define output(x, y); pf("Case %d: %d", ++x, y)
  38. #define newLine pf("\n")
  39. #define sf scanf
  40. #define READ(f) freopen(f, "r", stdin)
  41. #define WRITE(f) freopen(f, "w", stdout)
  42. #if ( _WIN32 or __WIN32__ )
  43. #define LLD "%I64d"
  44. #else
  45. #define LLD "%lld"
  46. #endif
  47. #define SDl(x) sf(LLD, &x)
  48. #define MAX5 100005
  49. #define MAX7 10000007
  50. #define MAX9 1000000009
  51. #define MOD9 1000000007
  52. typedef long long i64;
  53. typedef unsigned long long ui64;
  54. const i64 INF64 = (i64) 1E18;
  55.  
  56. template<typename T> string toStr(T n) { ostringstream oss; oss << n; oss.flush(); return oss.str(); }
  57. template<typename T> T toInt(string s) { T n = 0; istringstream sin(s); sin >> n; return n; }
  58.  
  59. class TimeTracker {
  60.     clock_t start, end;
  61. public:
  62.     TimeTracker() {
  63.         start = clock();
  64.     }
  65.     ~TimeTracker() {
  66.         end = clock();
  67.         fprintf(stderr, "%.3lf s\n", (double)(end - start) / CLOCKS_PER_SEC);
  68.     }
  69. };
  70.  
  71. struct Point {
  72.     int x, y;
  73.     Point(): x(0), y(0) {}
  74.     Point(int a, int b): x(a), y(b) {}
  75.     bool operator < (const Point& other) const {
  76.         return x < other.x;
  77.     }
  78. };
  79. // BitMask
  80. int Set(int N, int pos) {
  81.     return N |= (1 << pos);
  82. }
  83. int Reset(int N, int pos) {
  84.     return N &= ~(1 << pos);
  85. }
  86. int Check(int N, int pos) {
  87.     return (N & (1 << pos));
  88. }
  89. int toggle(int N, int pos) {
  90.     return N ^= (1 << pos);
  91. }
  92.  
  93. // direction array
  94. //int dx[] = {0, -1, 0, 1};
  95. //int dy[] = {-1, 0, 1, 0};
  96. //int Dx[] = {0, -1, -1, -1, 0, 1, 1, 1};
  97. //int Dy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
  98. //int _row, _col;
  99. //inline bool isValid(int i, int j) {
  100. //    return i >= 0 and j >= 0 and i < _row and j < _col;
  101. //}
  102.  
  103. #define MOD (MAX9 + 7)
  104.  
  105. inline void IncMod(int &x, int y) {
  106.     x += y;
  107.     if (x >= MOD) {
  108.         x -= MOD;
  109.     }
  110. }
  111.  
  112. #define keyType int
  113. class Compare {
  114. public:
  115.     bool operator() (keyType& lhs, keyType& rhs) const {
  116.         return lhs > rhs;
  117.     }
  118. } compare;
  119. // e.g. sort(all(arr), compare)
  120.  
  121.  
  122. #define error(args...) { vector<string> _v; split(#args, ',', _v); err(_v.begin(), args); }
  123.  
  124. void split(const string& s, char c, vector<string>& result) {
  125.     stringstream ss(s);
  126.     string x;
  127.     while (getline(ss, x, c))
  128.         result.push_back(x);
  129. }
  130.  
  131. void err(vector<string>::iterator it) {}
  132. template<typename T, typename... Args>
  133. void err(vector<string>::iterator it, T a, Args... args) {
  134.     cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << '\n';
  135.     err(++it, args...);
  136. }
  137.  
  138. /** Implementation **/
  139.  
  140. #define Max MAX5
  141. int depth[Max];
  142. int parent[Max];
  143. vector <int> adj[Max];
  144. i64 lazy[MAX];
  145. int nodes[MAX];
  146. i64 sum[MAX];
  147. int value[MAX];
  148. int N, Q;
  149.  
  150. void dfs(int from, int u, int d) {
  151.     parent[u] = from;
  152.     depth[u] = d;
  153.     int childs = 0;
  154.     i64 total = 0;
  155.     for(int i = 0; i < (int)adj[u].size(); i++) {
  156.         int v = adj[u][i];
  157.         if(v == from) continue;
  158.         dfs(u, v, d + 1);
  159.         childs += nodes[v];
  160.         total += sum[v];
  161.     }
  162.     nodes[u] += childs;
  163.     nodes[u]++;
  164.     sum[u] += value[u];
  165.     sum[u] += total;
  166. }
  167.  
  168. int lcaQuery(int u, int v) {
  169.     if(depth[u] > depth[v]) {
  170.         swap(u, v);
  171.     }
  172.     while(depth[v] > depth[u]) {
  173.         v = parent[v];
  174.     }
  175.     // assert(depth[v] == depth[u]);
  176.     while(u != v) {
  177.         u = parent[u], v = parent[v];
  178.     }
  179.     // assert(u == v);
  180.  
  181.     return u;
  182. }
  183.  
  184. bool isDisjoint(int u, int v) {
  185.     int lca = lcaQuery(u, v);
  186.     return (lca != u and lca != v);
  187. }
  188.  
  189. int main(int argc, const char * argv[]) {
  190. #ifndef ONLINE_JUDGE
  191.     assert(READ("input.txt"));
  192. #endif
  193.     SD2(N, Q);
  194.     int val;
  195.     rep(i, N) {
  196.         SDi(val);
  197.         value[i] = val;
  198.     }
  199.     int u, v;
  200.     rep(i, N - 1) {
  201.         SD2(u, v);
  202.         u--, v--;
  203.         adj[u].pb(v);
  204.         adj[v].pb(u);
  205.     }
  206.     dfs(0, 0, 0);
  207.     int qtype;
  208.     int x;
  209.     rep(i, Q) {
  210.         SDi(qtype);
  211.         if(qtype == 1) {
  212.  
  213.         }
  214.         else if(qtype == 2) {
  215.             SD2(u, x);
  216.             lazy[u] += x;
  217.         }
  218.         else if(qtype == 3) {
  219.             SD2(u, v);
  220.             if(isDisjoint(u, v)) {
  221.                 puts("-1\n");
  222.                 continue;
  223.             }
  224.  
  225.         }
  226.     }
  227.     return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment