CodeForces 696A(Lorenzo Von Matterhorn ) & CodeForces 696B(Puzzles )

A,给一棵完全二叉树,第一个操作,给两个点,两点路径上的所有边权值都增加w,第二个操作,给两个点,求两点路径上的所有边权值和。

我看一眼题就觉得是树链剖分,而我又不会树链剖分,扔掉。

后来查了题解,首先数据范围是1e18不可能是树剖,其次完全二叉树啊!不是普通的树啊!!sb。。。

//我做过此题,没做出来,被学弟教会。。虽然我做的题少,但我一向觉得至少自己做过的题都是记得的。。。但是。。。。

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
typedef long long ll;
map<ll, ll> mp;
int main()
{
    ll q, op, u, v, w, ans;
    for (cin >> q; q--; ) {
        cin >> op >> u >> v;
        if (op == 1) {
            cin >> w;
            while (u != v) {
                if (u < v) swap(u, v);
                mp[u] = mp[u] + w;
                u /= 2;
            }
        } else {
            ans = 0;
            while (u != v) {
                if (u < v) swap(u, v);
                ans += mp[u];
                u /= 2;
            }
            cout << ans << endl;
        }
    }
    return 0;
}

B。树形dp,每一个兄弟结点在该节点的前面的概率肯定是0.5啊。我还想了好久。然后就水。

#include <vector>
#include <cstdio>
std::vector<int> tr[100005];
int sz[100005], n, tmp;
double ans[100005];
int dfs(int u) { for (int v: tr[u]) sz[u] += dfs(v); return ++sz[u]; }
void dfs(int u, double d) { ans[u] = d+1; for (int v: tr[u]) dfs(v, (sz[u]-1-sz[v])/2.0+ans[u]); }
int main()
{
    scanf("%d", &n);
    for (int i = 2; i <= n; ++i) scanf("%d", &tmp), tr[tmp].push_back(i);
    dfs(1); dfs(1, 0);
    for (int i = 1; i <= n; ++i) printf("%f%c", ans[i], i==n?‘\n‘:‘ ‘);
    return 0;
}
时间: 2024-08-26 20:30:27

CodeForces 696A(Lorenzo Von Matterhorn ) & CodeForces 696B(Puzzles )的相关文章

【CodeForces 697C】Lorenzo Von Matterhorn(LCA)

Least Common Ancestors 节点范围是1~1e18,至多1000次询问. 只要不断让深的节点退一层(>>1)就能到达LCA. 用点来存边权,用map储存节点和父亲连边的权值. #include<cstdio> #include<map> #define ll long long using namespace std; map<ll,ll>m; ll u,v,w; void add(){ while(u!=v){ if(u<v){ m

Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn LCA(最近公共祖先)

C. 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. Ther

M - Lorenzo Von Matterhorn (二叉树 )

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 ca

CodeForces 696A Lorenzo Von Matterhorn (LCA + map)

方法:求出最近公共祖先,使用map给他们计数,注意深度的求法. 代码如下: #include<iostream> #include<cstdio> #include<map> #include<cstring> using namespace std; #define LL long long map<LL,LL> sum; int Get_Deep(LL x) { for(int i = 0; i < 63; i++) { if((1LL

【STL】Codeforces 696A Lorenzo Von Matterhorn

题目链接: http://codeforces.com/problemset/problem/696/A 题目大意: 一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边权都为0 N(N<=1000)个操作,操作两种,1是从u到v的路径上的所有边权+w,2是求u到v的边权和.(1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109) 题目思路: [STL] 用map写很快,第一次用很生疏.现学只看了一点点. 因为是满二叉树所以直接暴力求LCA和求解,

codeforces 696A Lorenzo Von Matterhorn 水题

这题一眼看就是水题,map随便计 然后我之所以发这个题解,是因为我用了log2()这个函数判断在哪一层 我只能说我真是太傻逼了,这个函数以前听人说有精度问题,还慢,为了图快用的,没想到被坑惨了,以后尽量不用 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #

Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (类似LCA)

题目链接:http://codeforces.com/problemset/problem/697/D 给你一个有规则的二叉树,大概有1e18个点. 有两种操作:1操作是将u到v上的路径加上w,2操作是求u到v上的路径和. 我们可以看出任意一个点到1节点的边个数不会超过64(差不多就是log2(1e18)),所以可以找最近相同祖节点的方式写. 用一条边的一个唯一的端点作为边的编号(比如1到2,那2就为这条边的编号),由于数很大,所以用map来存. 进行1操作的时候就是暴力加w至u到LCA(u,v

Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pashmak has fallen in love with an attractive girl called Parmida s

Codeforces Round #262 (Div. 2)460A. Vasya and Socks(简单数学题)

题目链接:http://codeforces.com/contest/460/problem/A A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put o