8.10 第七场 Fall with Trees

8.10 第七场 Fall with Trees

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2600 Accepted Submission(s): 618

Problem Description

Fall wants to draw a perfect binary tree.

We first stipulate that all nodes in the tree with the same depth also have the same y-coordinate in the plane. Define nodes with the same depth to be nodes at the same level, then the perfect binary tree has four properties.

- It is a full binary tree.
- The difference between the y-coordinates of two nodes at each adjacent level is a constant.
- The difference between the x-coordinates of two adjacent nodes at the same level is constant.
- The x-coordinate of each node is the average of the x-coordinates of its sons.

Fall has drawn the root node and its left and right sons of this binary tree. Now Fall intends to draw a total of k levels and cut the binary tree down and paste it on the wall afterwards, so he wants to know what is the area of the convex hull of all nodes of this perfect binary tree.

Hint

Here’s the picture of the perfect binary tree for the first example, whose area is SABC+SBCGD=14

请添加图片描述

Input

The input consists of multiple test cases.

The first line contains an integer T (T≤2×105) – the number of test cases.

For each test case:

In the first line, there is an integer k (2≤k≤104).

In the second line, there are six integers xroot,yroot,xlson,ylson,xrson,yrson∈[−104,104] ,which represent the coordinates of the root node and its sons.

It is guaranteed that all the coordinates meet the conditions of the question, which means:
- xlson+xrson=2×xroot
- ylson=yrson
- yroot>ylson,xlson<xrson

Output

For each test case, output a real number representing the answer, with three decimal places.

Sample Input

3
3
0 0 -2 -2 2 -2
4
0 0 -4 -2 4 -2
10000
0 0 -10000 -10000 10000 -10000

Sample Output

14.000
54.000
3999000000000.000

大概题意:

求题目描述的树所围成的面积

思路:

推导出复杂度O1的求和公式后才能AC

代码:

//
// Created by Black on 2021/8/10.
//

#include<iostream>
#include<cmath>
#include <cstdio>

using namespace std;

int main() {
//    std::ios::sync_with_stdio(false);
//    std::cin.tie(0);
    int t;
    double k;
    double xRoot, yRoot, xl, xr, yl, yr;
    scanf("%d", &t);
    while (t--) {
//        cin >> k;
        scanf("%lf", &k);
        k--;
        scanf("%lf%lf%lf%lf%lf%lf", &xRoot, &yRoot, &xl, &yl, &xr, &yr);
//        cin >> xRoot >> yRoot >> xl >> yl >> xr >> yr;
        double h = (yRoot - yr);
        double sum = (xr - xRoot) * (k - (1 - pow(0.5, k))) * 2;
        k--;
        sum += (xr - xRoot) * (k - (1 - pow(0.5, k))) * 2;
        sum *= h;
//        for (int i = 1; i < k; ++i) {
//            double xxr = 0, xxl = 0;
//            xxr = xr + (xr - xRoot) / 2;
//            xxl = xl - (xr - xRoot) / 2;
//            sum += 1.0 * h * (xr - xl + xxr - xxl) / 2;
//            xRoot = xr;
//            xl = xxl;
//            xr = xxr;
//        }
        printf("%.3f\n", sum);
    }
    return 0;
}
/**
*
 * 4 4+2 4+2+1
 * 4 4+2
 * 相加乘h
 *
 * 2 2+1
 * 2
 * 相加乘h
 *
*/
posted @   嘿,抬头!  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示