CodeForces - 1355C Count Triangles(数学)

题目链接:点击查看

题目大意:给出 A B C D ,规定 A <= x <= B <= y <= C <= z <= D ,问 ( x , y , z ) 三元组为三角形的情况有多少种

题目分析:首先需要知道,当 x + y > z 时,三元组 ( x , y , z ) 满足条件,但是三个数的范围都是 1e5 级别的,不能直接暴力枚举

但是不难想到可以 O( n ) 枚举 x ,然后尝试 O( 1 ) 去计算 ( y , z ) 有多少对适应当前的 x

想是比较难想的,所以不妨再 O( n ) 枚举一下 y ,然后观察一下是否存在规律吧

拿样例一为例,即 A B C D 分别等于 1 2 3 4

  1. 当 x == 1 时
    1. 当 y == 2 时,答案为 0
      1. z == 3 不满足条件
      2. z == 4 不满足条件
    2. 当 y == 3 时,答案为 1
      1. z == 3 满足条件
      2. z == 4 不满足条件
  2. 当 x == 2 时
    1. 当 y == 2 时,答案为 1
      1. z == 3 满足条件
      2. z == 4 不满足条件
    2. 当 y == 3 时,答案为 2
      1. z == 3 满足条件
      2. z == 4 满足条件

这里只给出示范,如果没有看出规律来,可以自己适当扩大 x , y , z 的范围,不难发现当 x 确定之后,z 与 y 呈线性关系,那么就可以用等差数列求和公式 O( 1 ) 算出了

如果直接使用等差数列来计算这个题目的话,还需要处理复杂的边界条件,所以干脆直接前缀和打个表,既方便又省事

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

typedef long long LL;

typedef unsigned long long ull;

const int inf=0x3f3f3f3f;

const int N=1e6+100;

LL sum[N];

LL a,b,c,d;

void init()
{
	for(int i=c;i<=d;i++)
		sum[i]=sum[i-1]+(i-c+1);
	for(int i=d+1;i

 

你可能感兴趣的:(CodeForces上分,数学)