Sir_Kay

Kaysman Official Website


  • 首页

  • 分类

  • 标签

  • 归档

  • 搜索

Codeforces 1119D Frets On Fire 题解

发表于 2019-09-14 更新于 2019-10-27 分类于 算法 , Codeforces
本文字数: 7.7k 阅读时长 ≈ 7 分钟

Codeforces 1119D Frets On Fire 题解

题意

给定$n$和$s_1,s_2,\dots,s_n$,由此得到一个$n\times(10^{18}+1)$的矩阵$f$,其中$f_{i,j}=s_i+j (1\le i\le n,0\le j\le 10^{18})$。

在样例$1$中,给定$n=6,s=[3,1,4,1,5,9]$,得到

再给定$q$个查询,每个查询有$L,R$,问第$L$列到第$R$列(以下表达为$[L,R]$)有多少个不同的数。

题解

因为问的是列,所以行的顺序不重要,可以对$s$进行从小到大排序。

对样例$1$排完序之后矩阵变为

还有:查询$[L,R]$与查询$[0,R-L]$是等价的。

定义$r=R-L,w=r+1$。

考虑当每一行对于整个矩阵即$r=10^{18}$时的贡献。

对于样例$1$:

  • 第$1$行贡献了$0$个数$\{\varnothing\}$;
  • 第$2$行贡献了$2$个数$\{1,2\}$;
  • 第$3$行贡献了$1$个数$\{3\}$;
  • 第$4$行贡献了$1$个数$\{4\}$;
  • 第$5$行贡献了$4$个数$\{5,6,7,8\}$;
  • 第$6$行贡献了$w$个数$\{9,10,\dots,9+10^{18}\}$。

很容易发现,第$i (1\le i<n)$行的贡献为$s_{i+1}-s_i$,第$n$行的贡献为$w$。

对于任意区间$[0,r]$,第$i (1\le i<n)$行的贡献即为$\min\{s_{i+1}-s_i,w\}$,第$n$行的贡献为$w$。

那么答案即为$\sum_{i=1}^{n-1}{\min\{s_{i+1}-s_i,w\}}+w$。

问题:时间复杂度为$O(n\cdot q)$。

解答:设数组$t$,其中$t_i=s_{i+1}-s_i (1\le i<n)$,将$t$从从小到大排序。设$p$为满足$t_p\le w$的最小值(用二分,时间复杂度为$O(\log n)$),那么答案为$t_1+t_2+\dots+t_p+w\times(n-p)$($t_1+t_2+\dots+t_p$用前缀和求出)。时间复杂度为$O(\log n\cdot q)$。

程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000")

// #include <bits/stdc++.h>
#include <map>
#include <set>
#include <list>
#include <array>
#include <cfenv>
#include <cmath>
#include <ctime>
#include <deque>
#include <mutex>
#include <queue>
#include <ratio>
#include <regex>
#include <stack>
#include <tuple>
#include <atomic>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <chrono>
#include <cstdio>
#include <cwchar>
#include <future>
#include <limits>
#include <locale>
#include <memory>
#include <random>
#include <string>
#include <thread>
#include <vector>
#include <cassert>
#include <climits>
#include <clocale>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <cwctype>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <ccomplex>
#include <cstdbool>
#include <iostream>
#include <typeinfo>
#include <valarray>
#include <algorithm>
#include <cinttypes>
#include <cstdalign>
#include <stdexcept>
#include <typeindex>
#include <functional>
#include <forward_list>
#include <system_error>
#include <unordered_map>
#include <unordered_set>
#include <scoped_allocator>
#include <condition_variable>
// #include <conio.h>
// #include <windows.h>
using namespace std;

typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef double ld;
typedef long double LD;
typedef pair<int,int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) || (WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((double)(n))
#define LD(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed<<setprecision(n)

const int INF=1061109567;
const int NINF=-1044266559;
const LL LINF=4557430888798830399;
const ld eps=1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971)

/*
#define MB_LEN_MAX 5
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 0xffffU
#define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647
#define UINT_MAX 0xffffffffU
#define LONG_MIN (-2147483647L - 1)
#define LONG_MAX 2147483647L
#define ULONG_MAX 0xffffffffUL
#define LLONG_MAX 9223372036854775807ll
#define LLONG_MIN (-9223372036854775807ll - 1)
#define ULLONG_MAX 0xffffffffffffffffull
*/

#define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(),(a).end()
#define pall(a) (a).rbegin(),(a).rend()
#define Log(x,y) log(x)/log(y)
#define SZ(a) ((int)(a).size())
#define rep(i,n) for(int i=0;i<((int)(n));i++)
#define rep1(i,n) for(int i=1;i<=((int)(n));i++)
#define repa(i,a,n) for(int i=((int)(a));i<((int)(n));i++)
#define repa1(i,a,n) for(int i=((int)(a));i<=((int)(n));i++)
#define repd(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define repd1(i,n) for(int i=((int)(n));i>=1;i--)
#define repda(i,n,a) for(int i=((int)(n));i>((int)(a));i--)
#define repda1(i,n,a) for(int i=((int)(n));i>=((int)(a));i--)
#define FOR(i,a,n,step) for(int i=((int)(a));i<((int)(n));i+=((int)(step)))
#define repv(itr,v) for(__typeof((v).begin()) itr=(v).begin();itr!=(v).end();itr++)
#define repV(i,v) for(auto i:v)
#define repE(i,v) for(auto &i:v)
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x) MS(x,0)
#define MINF(x) MS(x,63)
#define MCP(x,y) memcpy(x,y,sizeof(y))
#define sqr(x) ((x)*(x))
#define UN(v) sort(All(v)),v.erase(unique(All(v)),v.end())
#define filein(x) freopen(x,"r",stdin)
#define fileout(x) freopen(x,"w",stdout)
#define fileio(x)\
freopen(x".in","r",stdin);\
freopen(x".out","w",stdout)
#define filein2(filename,name) ifstream name(filename,ios::in)
#define fileout2(filename,name) ofstream name(filename,ios::out)
#define file(filename,name) fstream name(filename,ios::in|ios::out)
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define PC(x) putchar(x)
#define GC(x) x=getchar()
#define Endl PC('\n')
#define SF scanf
#define PF printf

inline int Read()
{
int X=0,w=0;char ch=0;while(!isdigit(ch)){w|=ch=='-';ch=getchar();}while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
inline void Write(int x){if(x<0)putchar('-'),x=-x;if(x>9)Write(x/10);putchar(x%10+'0');}

inline LL powmod(LL a,LL b){LL RES=1;a%=MOD;assert(b>=0);for(;b;b>>=1){if(b&1)RES=RES*a%MOD;a=a*a%MOD;}return RES%MOD;}
inline LL gcdll(LL a,LL b){return b?gcdll(b,a%b):a;}
const int dx[]={0,1,0,-1,1,-1,-1,1};
const int dy[]={1,0,-1,0,-1,-1,1,1};
/************************************************************Begin************************************************************/
const int maxn=100010;

int n,q;
LL s[maxn],pre[maxn],L,R;

int main()
{
SF("%d",&n);
rep1(i,n) SF("%lld",&s[i]);

sort(s+1,s+n+1);
rep1(i,n-1) s[i]=s[i+1]-s[i];

sort(s+1,s+n);
rep1(i,n-1) pre[i]=pre[i-1]+s[i];

SF("%d",&q);
while(q--)
{
SF("%lld%lld",&L,&R);

LL w=R-L+1;int id=lower_bound(s+1,s+n,w)-(s+1);
PF("%lld ",pre[id]+(n-id)*w);
}

return 0;
}
/*************************************************************End**************************************************************/
__EOF__
二分 排序 前缀和
Codeforces 309B Context Advertising 题解
Codeforces 609F Frogs and mosquitoes 题解
  • 文章目录
  • 站点概览
Sir_Kay

Sir_Kay

Kaysman #1 Sir_Kay
33 日志
6 分类
34 标签
RSS
Main site Wikipedia GitHub GitLab
Creative Commons
  1. 1. Codeforces 1119D Frets On Fire 题解
    1. 1.1. 题意
    2. 1.2. 题解
    3. 1.3. 程序
0%
© 2019 – 2020 Sir_Kay