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 题解的更多相关文章

  1. 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=( ...

  2. Margarite and the best present

    Little girl Margarita is a big fan of competitive programming. She especially loves problems about a ...

  3. Codeforces Round #524 (Div.2)题解

    题解 CF1080A [Petya and Origami] 这道题其实要我们求的就是 \[\lceil 2*n/k \rceil + \lceil 5*n/k \rceil + \lceil 8*n ...

  4. Codeforces Round #524 (Div. 2)(前三题题解)

    这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...

  5. 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 ...

  6. Codeforces Round #524 (Div. 2)

    A. Petya and Origamitime limit per test1 secondmemory limit per test256 megabytesinputstandard input ...

  7. CodeForces-Round524 A~D

    A. Petya and Origami time limit per test  1 second   memory limit per test  256 megabytes input stan ...

  8. Codeforces Round #524 (Div. 2) Solution

    A. Petya and Origami Water. #include <bits/stdc++.h> using namespace std; #define ll long long ...

  9. 题解 P2910 【[USACO08OPEN]寻宝之路Clear And Present Danger】

    说起来这还是本蒟蒻学完Floyd之后做的第一道题. emm...这是一道裸题,题目大致是说有一堆岛,岛之间有海盗,因此每一条边都有一个危险指数(权重),然后给出一段必须经过的路线,求从一号小岛走到N号 ...

随机推荐

  1. Sentry 监控 - Snuba 数据中台架构(编写和测试 Snuba 查询)

    系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...

  2. tomcat的log日志乱码解决方案

    Intellij idea Tomcat输出log中文乱码 配置tomcat在VM options添加-Dfile.encoding=UTF8 -Dsun.jnu.encoding=UTF8 重启后控 ...

  3. JAVA特点及安装卸载

    C语言特点 1972 贴近硬件,运行速度快,效率高 操作系统,数据库,网络系统,编译器 指针和内存管理 C++语言特点 1982 面向对象 兼容C 图形领域,游戏等 Java语言特点 简单性 面向对象 ...

  4. springboot 配置多数据源实例代码(分包方式)

    目录 1.数据库 2.pom与yml 2.1.pom中的依赖部分 2.2.yml数据库配置部分 3.数据源配置类 DataSourceConfig 3.1.DataSourceConfig1.java ...

  5. 【NOI导刊200908模拟试题02 题4】【二分+Dijkstra】 收费站

    Description 在某个遥远的国家里,有n个城市.编号外1,2,3,-,n. 这个国家的政府修建了m条双向的通路.每条公路连接着两个城市.沿着某条公路,开车从一个城市到另一个城市,需要花费一定的 ...

  6. Anaconda3-更换为清华源后依旧报错CondaHTTPError: HTTP 000 CONNECTION FAILED

    前言 今天发现换完清华源以后依旧报错 CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsi.. ...

  7. time 查看命令执行时间

    在命令执行完成之后就会打印出CPU的使用情况: real    0m5.064s      <== 实际使用时间(real time) user    0m0.020s     <== 用 ...

  8. Linux—yum的python版本错误——初级解决方案

    为了安装rrdtool,发现不是少这个就是少那个,最后发现yum也不能用. 从网上找的解决yum问题. 转自:http://doarthon.blog.51cto.com/3175384/728809 ...

  9. hadoop基础题

    转自:http://blog.csdn.net/pelick/article/details/8299482 //Hadoop基础 Doug Cutting所创立的项目的名称都受到其家人的启发,以下项 ...

  10. 学习java 7.13

    学习内容: 一个汉字存储:如果是GBK编码,占用2个字节:如果是UTF-8编码,占用3个字节 汉字在存储的时候,无论选择哪种编码存储,第一个字节都是负数 字符流=字节流+编码表 采用何种规则编码,就要 ...