zoukankan      html  css  js  c++  java
  • [CF697C]Lorenzo Von Matterhorn(LCA,思路好题)

    题目链接:http://codeforces.com/contest/697/problem/C

    题意:在一棵树中更新和查询一个节点到另一个节点的距离。

    用map来存一个节点和它到它父亲节点的路径值,利用堆序性可以推算当前节点的父亲节点序号(当前节点序号/2),求两个点的距离可以直接两个点上升到他们的lca,这时候把走过的路径长度记下就行了。思路好题。

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rll(a) scanf("%I64d", &a)
     44 #define Rs(a) scanf("%s", a)
     45 #define Cin(a) cin >> a
     46 #define FRead() freopen("in", "r", stdin)
     47 #define FWrite() freopen("out", "w", stdout)
     48 #define Rep(i, len) for(int i = 0; i < (len); i++)
     49 #define For(i, a, len) for(int i = (a); i < (len); i++)
     50 #define Cls(a) memset((a), 0, sizeof(a))
     51 #define Clr(a, x) memset((a), (x), sizeof(a))
     52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
     53 #define lrt rt << 1
     54 #define rrt rt << 1 | 1
     55 #define pi 3.14159265359
     56 #define RT return
     57 #define lowbit(x) x & (-x)
     58 #define onecnt(x) __builtin_popcount(x)
     59 typedef long long LL;
     60 typedef long double LD;
     61 typedef unsigned long long ULL;
     62 typedef pair<int, int> pii;
     63 typedef pair<string, int> psi;
     64 typedef pair<LL, LL> pll;
     65 typedef map<string, int> msi;
     66 typedef vector<int> vi;
     67 typedef vector<LL> vl;
     68 typedef vector<vl> vvl;
     69 typedef vector<bool> vb;
     70 
     71 int q;
     72 map<LL, LL> f;
     73 
     74 LL lca(LL u, LL v, LL w) {
     75     LL ret = 0;
     76     while(u != v) {
     77         if(u < v) swap(u, v);
     78         f[u] = f[u] + w;
     79         ret += f[u];
     80         u >>= 1;
     81     }
     82     return ret;
     83 }
     84 
     85 int main() {
     86     // FRead();
     87     Rint(q);
     88     int cmd;
     89     W(q) {
     90         LL u, v, w;
     91         Rint(cmd);
     92         if(cmd == 1) {
     93             cin >> u >> v >> w;
     94             lca(u, v, w);
     95         }
     96         else {
     97             cin >> u >> v;
     98             cout << lca(u, v, 0) << endl;
     99         }
    100     }
    101     RT 0;
    102 }
  • 相关阅读:
    PyMySQL TypeError: not enough arguments for format string
    使用python3抓取pinpoint应用信息入库
    JS 异步之 async await
    JS Null 空 判断
    Vue问题汇总
    pymysql DAO简单封装
    py可视化执行过程
    jenkins回滚之groovy动态获取版本号
    容器时间 容器乱码问题
    SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
  • 原文地址:https://www.cnblogs.com/kirai/p/5789388.html
Copyright © 2011-2022 走看看