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. C语言下的Led灯

    1. 设计思想 1.1 设置处理器模式 设置sp啥的汇编要先进入SVC模式,超级管理员特权模式,这样就可以访问所有寄存器了,需要用到cpsr寄存器 0到4位要设置svc模式10011 = 0x13, ...

  2. vue的常用指令

    https://www.bootcdn.cn/ 前端资源库 <!-- 常用内置指令 v:text : 更新元素的 textContent v-html : 更新元素的 innerHTML v-i ...

  3. System类的常用方法(currentTimeMillis与arraycopy)

    System类的常用方法 currentTimeMillis与arraycopy import java.util.Arrays; /* java.lang.System类中提供了大量的静态方法,可以 ...

  4. 记一次 android 线上 oom 问题

    背景 公司的主打产品是一款跨平台的 App,我的部门负责为它提供底层的 sdk 用于数据传输,我负责的是 Adnroid 端的 sdk 开发. sdk 并不直接加载在 App 主进程,而是隔离在一个单 ...

  5. 理解ASP.NET Core - 过滤器(Filters)

    注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或点击此处查看全文目录 Filter概览 如果你是从ASP.NET一路走过来的,那么你一定对过滤器(Filter)不陌 ...

  6. uniapp如何在当前页面获取上个页面的变量、方法

    getCurrentPages() getCurrentPages() 函数用于获取当前页面栈的实例,以数组形式按栈的顺序给出,第一个元素为首页,最后一个元素为当前页面. 注意: getCurrent ...

  7. CF1175G

    叉姐牛逼. \(f_{k,i} = \min_{0\leq j <i}{f_{k - 1,j} + RMQ(j + 1,i) * (i - j)}\) 我们考虑在序列上分治一波. 按照\(m\) ...

  8. Codeforces 1067D - Computer Game(矩阵快速幂+斜率优化)

    Codeforces 题面传送门 & 洛谷题面传送门 好题. 首先显然我们如果在某一次游戏中升级,那么在接下来的游戏中我们一定会一直打 \(b_jp_j\) 最大的游戏 \(j\),因为这样得 ...

  9. Codeforces 464E The Classic Problem(主席树+最短路+哈希,神仙题)

    题目链接 题意:给出一张 \(n\) 个点 \(m\) 条边的无向图,第 \(i\) 条边连接 \(u_i,v_i\),边权为 \(2^{w_i}\),求 \(s\) 到 \(t\) 的最短路. \( ...

  10. 【3】蛋白鉴定软件之Mascot

    目录 1.简介 2.配置 2.1在线版本 2.2 服务器版本 3.运行 3.1 在线版本 3.2 服务器版本 4.结果 1.简介 Mascot是非常经典的蛋白鉴定软件,被Frost & Sul ...