HDU 6274

查询次数少的问题

HDU 6274 Master of sequence

题目描述

求满足 $S(t) = \sum_{i=1}^{n} {\lfloor \frac {t-b_i} {a_i} \rfloor} \geq k$ 最小的 $t$ ,其中 $a_i \leq 1000$ ,查询次数不超过 $1000$

题解

由于查询次数很少,我们需要特殊处理一下 $a_i$ ,利于在查询的时候得到结果。

每次将 $b_i \ \text{mod} \ a_i$ 预处理出来,我们需要在 $O(n \log n)$ 处理大于 $t \ \text{mod} \ a_i$ 的个数。

二分 $t$ ,我们利用上面的结果来计算答案,复杂度 $O(1000 \times 1000 \times \log {1000} \times \log t)$

AC Code

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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//#define NOSTDCPP
//#define Cpp11
//#define Linux_System
#ifndef NOSTDCPP
#include <bits/stdc++.h>
#else
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstring>
#include <cstdio>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <list>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#endif
# ifdef Linux_System
# define getchar getchar_unlocked
# define putchar putchar_unlocked
# endif
# define RESET(_) memset(_, 0, sizeof(_))
# define RESET_(_, val) memset(_, val, sizeof(_))
# define fi first
# define se second
# define pb push_back
# define midf(x, y) ((x + y) >> 1)
# define DXA(_) ((_ << 1))
# define DXB(_) ((_ << 1) | 1)
# define next __Chtholly__
# define x1 __Mercury__
# define y1 __bbtl04__
# define index __ikooo__
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector <int> vi;
typedef set <int> si;
typedef pair <int, int> pii;
typedef long double ld;
const int MOD = 1e9 + 7;
const int maxn = 1097;
const int maxm = 100009;
const ll inf = 1e18;
const double pi = acos(-1.0);
const double eps = 1e-6;
ll myrand(ll mod){return ((ll)rand() << 32 ^ (ll)rand() << 16 ^ rand()) % mod;}
template <class T>
inline bool scan_d(T & ret)
{
char c;
int sgn;
if(c = getchar(), c == EOF)return false;
while(c != '-' && (c < '0' || c > '9'))c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while(c = getchar(), c >= '0' && c <= '9')
ret = ret * 10 + (c - '0');
ret *= sgn;
return true;
}
#ifdef Cpp11
template <class T, class ... Args>
inline bool scan_d(T & ret, Args & ... args)
{
scan_d(ret);
scan_d(args...);
}
#define cin.tie(0); cin.tie(nullptr);
#define cout.tie(0); cout.tie(nullptr);
#endif
inline bool scan_ch(char &ch)
{
if(ch = getchar(), ch == EOF)return false;
while(ch == ' ' || ch == '\n')ch = getchar();
return true;
}
template <class T>
inline void out_number(T x)
{
if(x < 0)
{
putchar('-');
out_number(- x);
return ;
}
if(x > 9)out_number(x / 10);
putchar(x % 10 + '0');
}
int n, m;
struct BIT
{
int sum[maxn];
int lowbit(int x){return x & -x;}
void update(int x, int v)
{
++ x;
for(int i = x; i < maxn; i += lowbit(i))
sum[i] += v;
}
int query(int x)
{
++ x;
int ans = 0;
for(int i = x; i; i -= lowbit(i))
ans += sum[i];
return ans;
}
}tree[maxn];
int A[maxm], t[maxm], B[maxm];
bool ok(ll mid, ll ans)
{
ll ss = 0;
for(int i = 1; i <= 1000; ++ i)
{
ss += (mid / i) * t[i] - (t[i] - tree[i].query(mid % i));
if(ss >= ans + 1000 - i) return true;
}
return ss >= ans;
}
int main()
{
int T, type, x, y;
ll s, ans, l, r, mid, rans;
scanf("%d", &T);
while(T --)
{
s = 0;
RESET(tree);
RESET(t);
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++ i)
{
scanf("%d", A + i);
++ t[A[i]];
}
for(int i = 1; i <= n; ++ i)
{
scanf("%d", B + i);
s += B[i] / A[i];
tree[A[i]].update(B[i] % A[i], 1);
}
while(m --)
{
scanf("%d %d", &type, &x);
switch(type)
{
case 1:
scanf("%d", &y);
-- t[A[x]];
s = s - B[x] / A[x];
tree[A[x]].update(B[x] % A[x], -1);
A[x] = y;
++ t[A[x]];
s = s + B[x] / A[x];
tree[A[x]].update(B[x] % A[x], 1);
break;
case 2:
scanf("%d", &y);
s = s - B[x] / A[x];
tree[A[x]].update(B[x] % A[x], -1);
B[x] = y;
s = s + B[x] / A[x];
tree[A[x]].update(B[x] % A[x], 1);
break;
case 3:
ans = s + x;
l = 0, r = 2e12;
while(l <= r)
{
mid = midf(l, r);
if(ok(mid, ans)) rans = mid, r = mid - 1;
else l = mid + 1;
}
printf("%I64d\n", rans);
break;
default:
assert(type == 1 || type == 2 || type == 3);
}
}
}
return 0;
}
/*
2
4 6
2 4 6 8
1 3 5 7
1 2 3
2 3 3
3 15
1 3 8
3 90
3 66
8 5
2 4 8 3 1 3 6 24
2 2 39 28 85 25 98 35
3 67
3 28
3 73
3 724
3 7775
*/
文章目录
  1. 1. HDU 6274 Master of sequence
    1. 1.1. 题目描述
    2. 1.2. 题解
    3. 1.3. AC Code
{{ live2d() }}