AtCoder题解—— AtCoder Beginner Contest 181 —— B - Trapezoid Sum
题目相关
题目链接
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;
}
时间复杂度
AtCoder题解—— AtCoder Beginner Contest 181 —— B - Trapezoid Sum相关推荐
- AtCoder题解 —— AtCoder Beginner Contest 182 —— D - Wandering
题目相关 题目链接 AtCoder Beginner Contest 182 D 题,https://atcoder.jp/contests/abc182/tasks/abc182_d. Proble ...
- AtCoder题解 —— AtCoder Beginner Contest 187 —— B - Gentle Pairs —— 暴力
题目相关 题目链接 AtCoder Beginner Contest 187 B 题,https://atcoder.jp/contests/abc187/tasks/abc187_b. Proble ...
- AtCoder题解——AtCoder Grand Contest 048——A - atcoder < S
题目相关 题目链接 AtCoder Grand Contest 048 A 题,https://atcoder.jp/contests/agc048/tasks/agc048_a. Problem S ...
- AtCoder题解 —— AtCoder Grand Contest 050 —— B - Three Coins —— 动态规划
题目相关 题目链接 AtCoder Grand Contest 050 B 题,https://atcoder.jp/contests/agc050/tasks/agc050_b. Problem S ...
- AtCoder题解——AtCoder Regular Contest 107——B - Quadruple
题目相关 题目链接 AtCoder Regular Contest 107 B 题,https://atcoder.jp/contests/arc107/tasks/arc107_b. Problem ...
- AtCoder Beginner Contest 272「A」「B」「C」「D bfs」「E 思维」
AtCoder Beginner Contest 272 A - Integer Sum 题目描述: 输入n个数字,求数字和 #include <bits/stdc++.h> using ...
- Atcoder题解与视频集
开启Atcoder之路 开启Atcoder之路_sortmin的博客-CSDN博客_atcoder怎么用 atcoder心得 atcoder心得_404REN的博客-CSDN博客_atcoder怎么用 ...
- AtCoder题解——Beginner Contest 170——F - Pond Skater
题目相关 题目链接 AtCoder Beginner Contest 170 F题,https://atcoder.jp/contests/abc170/tasks/abc170_f. Problem ...
- AtCoder题解——Beginner Contest 167——C - Skill Up
题目相关 题目链接 AtCoder Beginner Contest 167 C题,https://atcoder.jp/contests/abc167/tasks/abc167_c. Problem ...
最新文章
- MVC 区域内默认控制器不能访问(Multiple types were found that match the controller named ‘Index')...
- Object #HTMLCanvasElement 的drawImage没有作用的原因
- 即使是一个技术人员,也需要知道沟通的重要性
- python add argument list_python模块介绍- argparse:命令行选项及参数解析
- Windows校验文件哈希hash的两种常用方式
- 国产美瞳频获融资背后:“小”美瞳的“大”生意经?
- 单链表的应用(电话本)
- python学习---常见的内置字符串(二)
- 微信支付分使用用户数超2.4亿 每日使用笔数达千万级
- 问题 D: 去行首行尾的空白符
- 隔行变色java代码_纯js实现隔行变色效果
- 【中秋快乐】求问meta-learning和few-shot learning的关系是什么?
- 乱码问题_win7 64位系统出现字体乱码的问题
- 计算机创建管理员用户名和密码,创建管理员密码是什么意思(图文)
- JavaScript 代码的执行流程
- Halcon区域region系列(1)相关的算子
- 天大计算机研究生毕业工资,本科生VS研究生工资差距这么大?又一高校薪资排行曝光,看完扎心了……...
- Unity 中的基础光照
- php账单明细功能怎么实现,php 处理微信账单
- 2020年最烂密码出炉!一秒钟就破解!