D. Frets On Fire 【二分,前缀和】 (Codeforces Global Round 2)
题目传送门:http://codeforces.com/contest/1119/problem/D
D. Frets On Fire
1.5 seconds
256 megabytes
standard input
standard output
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.
In return, the fleas made a bigger ukulele for her: it has nn strings, and each string has (1018+1)(1018+1) frets numerated from 00 to 10181018. The fleas use the array s1,s2,…,sns1,s2,…,sn to describe the ukulele's tuning, that is, the pitch of the jj-th fret on the ii-th string is the integer si+jsi+j.
Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.
Each question is in the form of: "How many different pitches are there, if we consider frets between ll and rr (inclusive) on all strings?"
Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!
Formally, you are given a matrix with nn rows and (1018+1)(1018+1) columns, where the cell in the ii-th row and jj-th column (0≤j≤10180≤j≤1018) contains the integer si+jsi+j. You are to answer qq queries, in the kk-th query you have to answer the number of distinct integers in the matrix from the lklk-th to the rkrk-th columns, inclusive.
The first line contains an integer nn (1≤n≤1000001≤n≤100000) — the number of strings.
The second line contains nn integers s1,s2,…,sns1,s2,…,sn (0≤si≤10180≤si≤1018) — the tuning of the ukulele.
The third line contains an integer qq (1≤q≤1000001≤q≤100000) — the number of questions.
The kk-th among the following qq lines contains two integers lklk,rkrk (0≤lk≤rk≤10180≤lk≤rk≤1018) — a question from the fleas.
Output one number for each question, separated by spaces — the number of different pitches.
6
3 1 4 1 5 9
3
7 7
0 2
8 17
5 10 18
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
2 1500000000000000000
For the first example, the pitches on the 66 strings are as follows.
There are 55 different pitches on fret 77 — 8,10,11,12,168,10,11,12,16.
There are 1010 different pitches on frets 0,1,20,1,2 — 1,2,3,4,5,6,7,9,10,111,2,3,4,5,6,7,9,10,11.
题意概括:
有一个 N*(10^18) 的矩阵,题目给出 N 和矩阵 N 行的首元素,后面的元素都是前一个元素+1;
Q次查询,询问 [ L, R ] 列 有多少个不同的元素(所有行)。
解题思路:
很容易想到,每一行的贡献就是产生小于其他行首元素的值。
因为各行的元素都是加一递增,所以查询 [ L, R ] 和查询 [ 0, R-L+1 ] 是等效的!!!
那么我们就可以先对 首元素 排一个序,这样相邻两行的 差 就是前一行所能做的贡献了。
例如排序后是:
1
2
2
5
那么第一行所能做的贡献就是 2-1 = 1;(小于其他行首元素的也就只有 1 了)
第二行的贡献 就是 2-2 = 0, 其实它跟第三行完全一样...
第三行的贡献 5-2 = 3 (2, 3, 4);
后面类似.
但仅仅是这样,每次查询还是要遍历所有行,复杂度 O(N*Q),怂了...
频繁的区间查询,看到网上有大佬莽了线段树...
而我只能学习处理一下前缀和了
首先对每行所能做的贡献排一个序
sum[ i ] 为前 i 行所能做出的贡献,前面已经说过查询区间可以等效转移。
那么 R = R-L+1; 在行贡献里 二分查找 不大于 R 的位置 t,
那么 t 前面的都不比 R 大,也就是说每一行的贡献都不会超标,直接累加,而前面已经处理出前缀和,O(1)解决前面的部分。
后面的都比 R 大, 虽然他们贡献很多,但是超标了,每行我只要 R 个(因为只有 R 列),那么这样的有多少行呢 有( N-t )行
AC code:
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#define mem(i, j) memset(i, j, sizeof(i))
#define inc(i, j, k) for(LL i = j; i <= k; i++)
#define rep(i, j, k) for(LL i = j; i < k; i++)
#define gcd(i, j) __gcd(i, j)
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 2e5+;
LL num[MAXN];
LL sum[MAXN];
LL N, Q;
LL L, R;
vector<LL>ans;
int main()
{
scanf("%I64d", &N);
inc(i, , N){ scanf("%I64d", &num[i]);}
sort(num+, num++N); rep(i, , N) num[i] = num[i+]-num[i];
sort(num+, num+N);
rep(i, , N) sum[i] = sum[i-]+num[i];
LL tp;
scanf("%I64d", &Q);
while(Q--){
scanf("%I64d %I64d", &L, &R);
R = R-L+; L = ;
tp = upper_bound(num+, num+N, R)-num-;
ans.push_back(sum[tp]+(N-tp)*R);
}
for(int i = ; i < ans.size(); i++){
printf("%I64d ", ans[i]);
} return ;
}
D. Frets On Fire 【二分,前缀和】 (Codeforces Global Round 2)的更多相关文章
- Codeforces Global Round 2 题解
Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...
- Codeforces Global Round 3
Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)
Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- 【手抖康复训练1 】Codeforces Global Round 6
[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
- Codeforces Global Round 2 D. Frets On Fire (动态开点线段树,沙雕写法)
题目链接:D. Frets On Fire 思路:明明可以离散化+二分写,思路硬是歪到了线段树上,自闭了,真实弟弟,怪不得其他人过得那么快 只和查询的区间长度有关系,排完序如果相邻的两个点的差值小于等 ...
随机推荐
- Debian - 安装随记
为什么要突然换个操作系统? 之前使用的是Lubuntu,可见硬件非常糟糕. 更糟糕的是Lubuntu被玩坏了,很多程序不能正常运行. 于是打算换Debian + XFCE. 随手记录一下遇到的一些坑, ...
- nvm安装最新稳定版node
安装当前最新的稳定版. nvm install stable
- C#生成Excel
需要引用MyXls.SL2.dll的类库: 下载地址:http://sourceforge.net/projects/myxls/ 命名空间using org.in2bits.MyXls: //创建表 ...
- groovy实现循环、交换变量、多赋值、?.运算符
/** * Created by Jxy on 2019/1/3 10:01 * 1.实现循环的方式 * 2.安全导航操作符---?. * 3.一次性赋值给多个变量 */ 0.upto(2){ pri ...
- spss C# 二次开发 学习笔记(五)——Spss系统集成模式
Spss官方不支持Server2008R2等Server系列,但做Spss的二次开发,调用Spss的Web系统,一般部署在Server系列上,例如Server2008R2. 起初,在Server上安装 ...
- Java:反射与代理
Java世界的繁荣反射这一特性有很大功劳,可以获取全面的类型信息. /** * */ package ref; import java.lang.reflect.Field; import java. ...
- csharp: datatable get Column datatype or Column Name
/// <summary> ///列表名 /// </summary> /// <param name="table"></param&g ...
- 理解position:relative
前言:position有5个属性:static.absolute.relative.fixed和inherit.本篇博客主要介绍relative属性,因为似乎很多人对这个属性的理解很模糊,而且不清楚r ...
- 使用 docker-machine 管理 Azure 容器虚拟机
安装 docker-machine 请参见该链接(https://docs.docker.com/machine/install-machine "https://docs.docker.c ...
- win10系统80端口被System (PID=4)占用的解决
今天想用wamp搭建虚拟目录.发现80端口被占用,操作挺麻烦的,所以想要更改. 具体流程如下: 1.“win+R”输入“cmd”,然后输入“netstat -ano | findstr "8 ...