zoukankan      html  css  js  c++  java
  • Codeforces 1355C


    题面

    Time limit per test: 1 second

    Memory limit per test: 256 megabytes

    Description

    Like any unknown mathematician, Yuri has favourite numbers: A, B, C, and D, where A≤B≤C≤D. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides x, y, and z exist, such that A≤x≤B≤y≤C≤z≤D holds?

    Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property.

    The triangle is called non-degenerate if and only if its vertices are not collinear.

    Input

    The first line contains four integers: A, B, C and D (1≤A≤B≤C≤D≤5⋅10^5^) — Yuri's favourite numbers.

    Output

    Print the number of non-degenerate triangles with integer sides x, y, and z such that the inequality A≤x≤B≤y≤C≤z≤D holds.

    Examples

    input 1

    1 2 3 4
    

    output 1

    4
    

    input 2

    1 2 2 5
    

    output 2

    3
    

    input 3

    500000 500000 500000 500000
    

    output 3

    1
    

    Note

    In the first example Yuri can make up triangles with sides (1,3,3), (2,2,3), (2,3,3) and (2,3,4).

    In the second example Yuri can make up triangles with sides (1,2,2), (2,2,2) and (2,2,3).

    In the third example Yuri can make up only one equilateral triangle with sides equal to 5⋅10^5^.




    题意

    给定四个数 A B C D ,满足 1≤A≤B≤C≤D≤5⋅10^5^

    问有多少组 x y z 使得

    x ∈ [ A , B ]

    y ∈ [ B , C ]

    z ∈ [ C , D ]

    且 x y z 严格构成一个三角形(三顶点不共线)




    解题思路

    别人的代码好短qwq

    没看懂,自己想


    明显对于5e5的数据范围,O(n^2^)的方法行不通

    所以尽量通过公式入手并优化至 O(n)

    对于三角形的性质,可得两边之和一定大于第三边

    又因为已经限制 x≤y≤z

    所以只要保证 x+y>z 即可


    对 x 进行枚举,然后将 x A B C D 当作常量看

    因为 z ∈ [ C , D ] ,所以对于 z 的最小值 C ,一定要让 x+y>C 能够成立才有可行解

    得到 y>C-x 即 y>=C-x+1

    所以可以设置 y 的最小值 ym 为 max( B , C-x+1 )

    然后假设我们枚举 y 从 ym 到 C

    对于每一组 (x,y) ,由 x+y>z 得到此时 z 的最大值为 x+y-1

    所以 z 的可行解共有 (x+y-1)-C+1 = x+y-C 种

    又因为 z 的最大值为 D

    一旦 x+y-1 大于D,则 z 的可行解种数将会变成一个常数 D-C+1


    可以发现,在 x+y≤D 时,可行解种类数为 y+(x-C) ,是一个公差为 1 的等差数列(k=1的一次函数)

    在 x+y>D 时,可行解种类数为 D-C+1 ,是一个常数

    所以就可以把对于 y 的枚举直接进行分类讨论来线性处理

    等差数列使用 “首项加尾项乘以项数除以2” ,常数直接乘上出现次数即可


    一、z 的可行解均为常数

    此时一定满足条件 x+ym>D ,表示 y 取最小值时,z 的最大取值也符合题意

    项数为 C-ym+1 ,常数为 D-C+1

    故答案为 (C-ym+1)*(D-C+1)

    二、z 的可行解均呈一次函数形式

    此时一定满足条件 x+C>D,即满足x+C<=D,表示 y 取最大值时,z 的最大取值也不符合题意

    可以把一次函数的截距当作常数直接计算

    ans = y+(x-C) ,截距为 x-C

    因项数为 C-ym+1 ,所以对于截距,答案为 (C-ym+1)*(x-C)

    然后看一次函数部分(等差数列),发现首项为 ym,尾项为 C

    所以答案为 (ym+C)*(C-ym+1)/2

    故答案为 (C-ym+1)*(x-C)+(C-ym+1)*(ym+C)/2

    三、z 的可行解先呈一次函数形式再为一常数

    此时一定满足条件 x+C>D && x+ym<=D,表示 y 取最大值时 z 的最大取值符合题意,但是 y 取最小值时,z 的最大取值不符合题意

    所以分成两部分看即可

    由 x+y>D 得到函数的两种图像分界点为 y==D-x

    在 [ ym , D-x ] 上呈一次函数,在 [ D-x+1 , C ] 上呈常数

    对于常数部分,常数为 D-C+1 ,项数为 C-(D-x+1)+1 = C-D+x

    故答案为 (C-D+x)*(D-C+1)

    对于一次函数部分,项数为 D-x-ym+1

    同样分离常数部分与函数部分

    常数部分值为 x-C ,答案为 (D-x-ym+1)*(x-C)

    函数部分首项为 ym ,尾项为 D-x ,答案为 (D-x-ym+1)*(ym+D-x)/2

    故答案为 (D-x-ym+1)*(x-C)+(D-x-ym+1)*(ym+D-x)/2

    综上,答案为 (C-D+x)*(D-C+1)+(D-x-ym+1)*(x-C)+(D-x-ym+1)*(ym+D-x)/2




    完整程序

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    void solve()
    {
        ll A,B,C,D,ans=0,ym;
        cin>>A>>B>>C>>D;
        for(ll x=A;x<=B;x++)
        {
            ym=max(B,C-x+1);
            if(x+C>D)
            {
                if(x+ym>D)
                    ans+=(C-ym+1)*(D-C+1);
                else
                    ans+=(C-D+x)*(D-C+1)+(D-x-ym+1)*(x-C)+(D-x-ym+1)*(ym+D-x)/2;
            }
            else
                ans+=(C-ym+1)*(x-C)+(C-ym+1)*(ym+C)/2;
        }
        cout<<ans<<'
    ';
    }
    int main()
    {
        ios::sync_with_stdio(0);
        cin.tie(0);cout.tie(0);
        solve();
        return 0;
    }
    

  • 相关阅读:
    Response
    servelt乱码问题(tomcat服务端编码为ISO-8859-1)
    Servlet中的常用类以及常用方法
    EKF优化:协方差coff计算公式、意义、Code优化
    使用std::cout不能输出显示
    SLAM: Inverse Depth Parametrization for Monocular SALM
    SLAM: 单目视觉SLAM的方案分类《机器人手册》
    SLAM: SLAM的发展历程(WIKI)
    SLAM: VSLAM扫盲之旅
    安卓系统使用摄像头API
  • 原文地址:https://www.cnblogs.com/stelayuri/p/12903245.html
Copyright © 2011-2022 走看看