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号 ...
随机推荐
- 【Azure 应用服务】App Service 无法连接到Azure MySQL服务,报错:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
问题描述 App Service使用jdbc连接MySQL服务,出现大量的 Communications link failure: com.mysql.cj.jdbc.exceptions.Com ...
- 从头带你撸一个Springboot Starter
我们知道 SpringBoot 提供了很多的 Starter 用于引用各种封装好的功能: 名称 功能 spring-boot-starter-web 支持 Web 开发,包括 Tomcat 和 spr ...
- Spring Cloud Gateway过滤器精确控制异常返回(实战,控制http返回码和message字段)
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 前文<Spring Cloud Gat ...
- 【JAVA】编程(6)--- 应用IO流拷贝文件夹(内含多个文件)到指定位置
此程序应用了: File 类,及其常用方法: FileInputStream,FileOutputStream类及其常用方法: 递归思维: package com.bjpowernode.javase ...
- [NOI2021] 量子通信
嗯. NOI2021最白给的一题. PS:很后悔没打同步赛,少了一张同步赛Ag 考虑加黑的256位01串,我们思考一下. 因为\(k\)小于16,所以我们直接分成16块.所以一定可以的绝对有一块是完全 ...
- CSP-S2021 被碾压记
没啥好说的,不会 T3 这种贪心/dk,或者说我的贪心能力太拉跨导致 T3 这种基本的贪心都不会. 只能说,还好 CSP 不算省选.自爆了,根本没心情写游记. 希望大家不要因为我这次的失误而瞧不起我这 ...
- 使用 vue-property-decorator 用法总结
Vue + TypeScript 使用 vue-property-decorator 用法总结 简介 要使vue支持ts写法,我们需要用到vue-property-decorator,这个组件完全依赖 ...
- Python获取随机数
Python当中,可用random模块来获取随机数 import random """ random模块,用于获取随机数 """ print ...
- 避免UE4项目VS中误改源码.h文件导致编译时间长
最近几天两次触发VS中误改UE4源码头文件,导致需要编译大量源码的情况:再好的习惯也有不可靠的时候,还是需要可靠方案解决这个问题:官方提供了预编译版本(即从Launcher中下载的版本),但是对于程序 ...
- Qt最好用评价最高的是哪个版本?
来源: http://www.qtcn.org/bbs/read-htm-tid-89455.html /// Qt4: 4.8.7 4.X 系列终结版本 Qt5 : 5.6 LT ...