题目相关

题目链接

AtCoder Beginner Contest 181 B 题,https://atcoder.jp/contests/abc181/tasks/abc181_b。

Problem Statement

We have a blackboard with nothing written on it. Takahashi will do N operations to write integers on it.

In the i-th operation, he will write each integer from Ai through Bi once, for a total of Bi−Ai+1 integers.

Find the sum of the integers written on the blackboard after the N operations.

Input

Input is given from Standard Input in the following format:

N
A1 B1
.
.
.
AN BN

Output

Print the sum of the integers written on the blackboard after the N operations.

Samples1

Sample Input 1

2
1 3
3 5

Sample Output 1

18

Explaination

In the 1-st operation, he will write 1, 2, and 3 on the blackboard.

In the 2-nd operation, he will write 3, 4, and 5 on the blackboard.

The sum of the integers written is 1+2+3+3+4+5=18.

Samples2

Sample Input 2

3
11 13
17 47
359 44683

Sample Output 2

998244353

Samples3

Sample Input 3

1
1 1000000

Sample Output 3

500000500000

Constraints

  • All values in input are integers.
  • 1≤N≤10^5
  • 1≤Ai≤Bi≤10^6

题解报告

题目翻译

高桥在白板写出 N 行,每行包括两个整数。第 i 行操作,将写出数据 Ai 和 Bi,这样一共有 Bi-Ai+1 个数字。请计算出这 N 次操作包含的所有数据总和。

题目分析

回到了 AtCoder Beginner Contest 后,感觉明显好多了。看来自己的水平也就是这样。

我们可以知道每次高桥写出一个公差为 1,数量为 Bi-Ai+1 个数字。我们可以根据等差数量求和公式: 来计算。

数据规模估计

从数据限制可知,最终的总和最大值为:。说明需要使用 long long 来表示。

AC 参考代码

//https://atcoder.jp/contests/abc181/tasks/abc181_b
//B - Trapezoid Sum
//d=1的等差数列求和
#include <iostream>
using namespace std;
int main() {int n;cin>>n;long long ans=0;while (n--) {long long a,b;cin>>a>>b;long long tot=b-a+1;ans+=(a*tot+tot*(tot-1)/2);}cout<<ans<<"\n";return 0;
}

时间复杂度

O(N)。

AtCoder题解—— AtCoder Beginner Contest 181 —— B - Trapezoid Sum相关推荐

  1. AtCoder题解 —— AtCoder Beginner Contest 182 —— D - Wandering

    题目相关 题目链接 AtCoder Beginner Contest 182 D 题,https://atcoder.jp/contests/abc182/tasks/abc182_d. Proble ...

  2. AtCoder题解 —— AtCoder Beginner Contest 187 —— B - Gentle Pairs —— 暴力

    题目相关 题目链接 AtCoder Beginner Contest 187 B 题,https://atcoder.jp/contests/abc187/tasks/abc187_b. Proble ...

  3. AtCoder题解——AtCoder Grand Contest 048——A - atcoder < S

    题目相关 题目链接 AtCoder Grand Contest 048 A 题,https://atcoder.jp/contests/agc048/tasks/agc048_a. Problem S ...

  4. AtCoder题解 —— AtCoder Grand Contest 050 —— B - Three Coins —— 动态规划

    题目相关 题目链接 AtCoder Grand Contest 050 B 题,https://atcoder.jp/contests/agc050/tasks/agc050_b. Problem S ...

  5. AtCoder题解——AtCoder Regular Contest 107——B - Quadruple

    题目相关 题目链接 AtCoder Regular Contest 107 B 题,https://atcoder.jp/contests/arc107/tasks/arc107_b. Problem ...

  6. AtCoder Beginner Contest 272「A」「B」「C」「D bfs」「E 思维」

    AtCoder Beginner Contest 272 A - Integer Sum 题目描述: 输入n个数字,求数字和 #include <bits/stdc++.h> using ...

  7. Atcoder题解与视频集

    开启Atcoder之路 开启Atcoder之路_sortmin的博客-CSDN博客_atcoder怎么用 atcoder心得 atcoder心得_404REN的博客-CSDN博客_atcoder怎么用 ...

  8. AtCoder题解——Beginner Contest 170——F - Pond Skater

    题目相关 题目链接 AtCoder Beginner Contest 170 F题,https://atcoder.jp/contests/abc170/tasks/abc170_f. Problem ...

  9. AtCoder题解——Beginner Contest 167——C - Skill Up

    题目相关 题目链接 AtCoder Beginner Contest 167 C题,https://atcoder.jp/contests/abc167/tasks/abc167_c. Problem ...

最新文章

  1. MVC 区域内默认控制器不能访问(Multiple types were found that match the controller named ‘Index')...
  2. Object #HTMLCanvasElement 的drawImage没有作用的原因
  3. 即使是一个技术人员,也需要知道沟通的重要性
  4. python add argument list_python模块介绍- argparse:命令行选项及参数解析
  5. Windows校验文件哈希hash的两种常用方式
  6. 国产美瞳频获融资背后:“小”美瞳的“大”生意经?
  7. 单链表的应用(电话本)
  8. python学习---常见的内置字符串(二)
  9. 微信支付分使用用户数超2.4亿 每日使用笔数达千万级
  10. 问题 D: 去行首行尾的空白符
  11. 隔行变色java代码_纯js实现隔行变色效果
  12. 【中秋快乐】求问meta-learning和few-shot learning的关系是什么?
  13. 乱码问题_win7 64位系统出现字体乱码的问题
  14. 计算机创建管理员用户名和密码,创建管理员密码是什么意思(图文)
  15. JavaScript 代码的执行流程
  16. Halcon区域region系列(1)相关的算子
  17. 天大计算机研究生毕业工资,本科生VS研究生工资差距这么大?又一高校薪资排行曝光,看完扎心了……...
  18. Unity 中的基础光照
  19. php账单明细功能怎么实现,php 处理微信账单
  20. 2020年最烂密码出炉!一秒钟就破解!

热门文章

  1. [ue5]关卡设计与地编_材质
  2. 怎么在手机上取消双重认证_手机怎么连到电视上?
  3. 结构体NSPoint、NSRect、与NSSize或CG开头的详解
  4. iOS实现微信网页进度条,WKWebview进度条的实现
  5. Pyenv-virtualenv使用
  6. 中国碳酸亚铁市场深度研究分析报告
  7. 视频教程-JSP从入门到精通2016+在线视频教学平台项目实训-其他
  8. html 实现格子效果,CSS中网格效果的实现方案
  9. windows优质软件汇总表
  10. 苹果cms如何设置采集间隔时间