zoukankan      html  css  js  c++  java
  • codeforces 696A A. Lorenzo Von Matterhorn(水题)

    题目链接:

    A. Lorenzo Von Matterhorn

    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections.

    Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:

    1. Government makes a new rule. A rule can be denoted by integers vu and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.

    2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.

    Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).

    Input

    The first line of input contains a single integer q (1 ≤ q ≤ 1 000).

    The next q lines contain the information about the events in chronological order. Each event is described in form v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.

    1 ≤ v, u ≤ 10^18, v ≠ u, 1 ≤ w ≤ 10^9 states for every description line.

    Output

    For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.

    Example
    input
    7
    1 3 4 30
    1 4 1 2
    1 3 6 8
    2 4 3
    1 6 1 40
    2 3 7
    2 2 4
    output
    94
    0
    32
    题意:
     
    i与2*i和2*i+1相连的一个图,两种操作,一种是在u,v的道路上的每一条边权值加w,一种是询问u,v之间道路上的边权值和;
     
    思路:
     
    可以直接暴力,可以找出最近功公共祖先,然后找这两个点与公共祖先之间把边的权值加上;
     
    AC代码:
    #include <bits/stdc++.h>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    
    typedef  long long LL;
    
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e18;
    const int N=3e6+10;
    const int maxn=3e6;
    const double eps=1e-10;
    
    int q;
    map<LL,LL>mp,vis;
    LL getfa(LL x,LL y)
    {
        vis.clear();
        while(x)vis[x]=1,x>>=1;
        while(1)
        {
            if(vis[y])return y;
            y>>=1;
        }
    }
    int main()
    {
            read(q);
            while (q--)
            {
                int f;
                LL u,v,w;
                read(f);
                if(f==1)
                {
                    read(u);read(v);read(w);
                    LL fa=getfa(u,v);
                    while(u!=fa)
                    {
                        mp[u]=mp[u]+w;
                        u>>=1;
                    }
                    while(v!=fa)
                    {
                        mp[v]=mp[v]+w;
                        v>>=1;
                    }
                }
                else 
                {
                    read(u);read(v);
                    LL fa=getfa(u,v),ans=0;
                    while(u!=fa)
                    {
                        ans+=mp[u];
                        u>>=1;
                    }
                    while(v!=fa)
                    {
                        ans+=mp[v];
                        v>>=1;
                    }
                    print(ans);
                }
            }    
            return 0;
    }
  • 相关阅读:
    VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法
    vue-cli:渲染过程理解2(vue init webpack方式创建)
    vue2 mint-ui loadmore(下拉刷新)
    vue-resource: jsonp请求百度搜索的接口
    vue中组件绑定事件时是否加.native
    vue 子组件修改父组件传来的props值,报错
    测试开发工程师的角色
    不要信仰BAT!
    简历不能怎么写?
    测试工程师面试,全国各地有哪些知名互联网公司可以去?
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5674271.html
Copyright © 2011-2022 走看看