ADACOINS - Ada and Coins


Ada the Ladybug has several coins in her wallet. She is wondering, how many different values [sum of some subset of coins] is she able to make (in given range).

Input

The first line contains two integer numbers 1 ≤ N ≤ 104, 1 ≤ Q ≤ 5*105, the number of coins and number of queries.

The next line contains N integer numbers 1 ≤ Ai ≤ 105, the values of coins in Ada's wallet.

Afterward, Q lines follow, each containing two integers 1 ≤ B ≤ E ≤ 105, beginning and end of range, for which Ada wants to know the answer

Output

For each query, output the number of distinct values Ada can generate in given range.

Example Input

10 10
1 2 2 3 5 30 31 90 100 100
1 1
1 5
3 6
2 9
30 100
1 10000
30 32
5 12
9 29
190 220

Example Output

1
5
4
8
40
231
3
8
5
25

Output Subsets

[  1,    1]: 1 
[  1,    5]: 1 2 3 4 5 
[  3,    6]: 3 4 5 6 
[  2,    9]: 2 3 4 5 6 7 8 9 
[ 30,  100]: 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 61 62 63 64 65 66 67 68 69 70 71 72 73 74 90 91 92 93 94 95 96 97 98 99 100 
[  1,10000]: 1 2 3 4 5 6 7 8 9 10 11 12 13 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 61 62 63 64 65 66 67 68 69 70 71 72 73 74 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 290 291 292 293 294 295 296 297 298 299 300 301 302 303 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 351 352 353 354 355 356 357 358 359 360 361 362 363 364 
[ 30,   32]: 30 31 32 
[  5,   12]: 5 6 7 8 9 10 11 12 
[  9,   29]: 9 10 11 12 13 
[190,  220]: 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 220

hide comments
ajaytec227: 2019-11-11 08:03:01

Even with fast I/O cin cout fails
Use printf scanf

ansh_08lehri: 2019-08-17 16:50:26

very nice problem @Morass, thnx @Nour Alhadi for ur suggestion,learnt a lot

rohitkk074: 2019-04-21 12:45:14

@Morass is there any place where i can learn about bit manipulation ? Thank You :)

tihsrah1177: 2019-04-17 17:11:06

@kshubham02 what kind of offline algorithm...i request some help

chandru_r_1: 2019-04-03 13:42:52

for the query [30, 100] the output has to be 71 as numbers like 45, 46 and all can be cosntructed using the given coins.......... can someone correct me if im wrong

dhruv2212000: 2019-03-20 07:22:03

Use scanf() and printf()

hkyiitd: 2019-03-15 19:34:28

The solution using cin , cout gives TLE , whereas using printf and scan it gets accepted.

chasr: 2018-12-12 13:13:24

It can pass using cin, cout as well using fast io

lamia2658: 2018-11-26 18:13:36

@satyampnc use scanf

Rohit Agarwal: 2018-10-21 17:18:59

Took me 2 hours to understand why the bitset will work but it was worth it. Really cool approach! I'm gonna code it now! :D


Added by:Morass
Date:2017-02-11
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All