CF1080B Margarite and the best present 题解
Content
有 \(t\) 次询问,每次询问给定两个整数 \(l,r\),求 \(\sum\limits_{i=l}^r (-1)^i\times i\)。
数据范围:\(1\leqslant t\leqslant 10^3,1\leqslant l\leqslant r\leqslant 10^9\)。
Solution
建议先去做 CF486A,然后我们可以得知 \(\sum\limits_{i=1}^n (-1)^i\times i=\begin{cases}\dfrac{i}{2}&2\mid i\\\left\lfloor\dfrac{i}{2}\right\rfloor-i&2\nmid i\end{cases}\)。因此,我们利用前缀和的思想,设 \(f_x=\sum\limits_{i=1}^x (-1)^i\times i\),那么每次询问的答案就是 \(f_r-f_{l-1}\),直接套用上面的公式计算即可。
Code
int t, l, r;
long long calc(int i) {
return i / 2 - (i % 2 ? i : 0);
}
int main() {
scanf("%d", &t);
while(t--) {
scanf("%d%d", &l, &r);
printf("%lld\n", calc(r) - calc(l - 1));
}
return 0;
}
CF1080B Margarite and the best present 题解的更多相关文章
- Codeforces Round #524 (Div. 2) B. Margarite and the best present
B. Margarite and the best present 题目链接:https://codeforces.com/contest/1080/problem/B 题意: 给出一个数列:an=( ...
- Margarite and the best present
Little girl Margarita is a big fan of competitive programming. She especially loves problems about a ...
- Codeforces Round #524 (Div.2)题解
题解 CF1080A [Petya and Origami] 这道题其实要我们求的就是 \[\lceil 2*n/k \rceil + \lceil 5*n/k \rceil + \lceil 8*n ...
- Codeforces Round #524 (Div. 2)(前三题题解)
这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...
- Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)部分(A~E)题解
(A) Even Subset Sum Problem 题解:因为n非常非常小,直接暴力枚举所有区间即可. #include<bits/stdc++.h> using namespace ...
- Codeforces Round #524 (Div. 2)
A. Petya and Origamitime limit per test1 secondmemory limit per test256 megabytesinputstandard input ...
- CodeForces-Round524 A~D
A. Petya and Origami time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces Round #524 (Div. 2) Solution
A. Petya and Origami Water. #include <bits/stdc++.h> using namespace std; #define ll long long ...
- 题解 P2910 【[USACO08OPEN]寻宝之路Clear And Present Danger】
说起来这还是本蒟蒻学完Floyd之后做的第一道题. emm...这是一道裸题,题目大致是说有一堆岛,岛之间有海盗,因此每一条边都有一个危险指数(权重),然后给出一段必须经过的路线,求从一号小岛走到N号 ...
随机推荐
- Ubuntu压缩和解压缩
1.常用的压缩格式 tar tar.bz2 tar.gz 2.gzip压缩 gzip xxx //压缩 gzip -d xxx.gz //解压缩 gzip对文件夹的压缩 gzip -r xxx //文 ...
- idea解决Command line is too long. Shorten command line for ServiceStarter or also for Application报错
找到 .idea\workspace.xml: 找到<component name="PropertiesComponent">,在里面添加<property n ...
- 部署vue项目到Linux服务器
案例一 vue-cli构建vue3项目,将项目上传到Linux服务器,服务器安装node,并启动vue项目 首先本地有一个vue项目,启动后可正常访问 本地打包后,也可直接访问 若打包后的index. ...
- 柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
1.关于柯基数据 南京柯基数据科技有限公司成立于2015年,提供一站式全生命周期知识图谱构建和运维.智能应用服务,致力于"链接海量数据,从大数据中挖掘智慧".帮助企业运用知识 ...
- 【NOI 2002 银河英雄传说】【带权并查集】
题面 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集*在巴米利恩星域爆发战争.泰山压顶集 ...
- python第三天 列表和元组
枚举 for in enumerate 循环输出字符串的内容并且输出它的索引信息: #判断索引为5的字符串是不是"您" is in Python提供了⼤量的内置数据结构,包含了列表 ...
- Go 性能提升tips--边界检查
1. 什么是边界检查? 边界检查,英文名 Bounds Check Elimination,简称为 BCE.它是 Go 语言中防止数组.切片越界而导致内存不安全的检查手段.如果检查下标已经越界了,就会 ...
- 多选项、多个选择项【c#】
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AddDataInfoCe ...
- Linux 内存泄漏 valgrind
Valgrind 是个开源的工具,功能很多.例如检查内存泄漏工具---memcheck. Valgrind 安装: 去官网下载: http://valgrind.org/downloads/curre ...
- ceph对象存储场景
安装ceph-radosgw [root@ceph-node1 ~]# cd /etc/ceph # 这里要注意ceph的源,要和之前安装的ceph集群同一个版本 [root@ceph-node1 c ...