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
Nour Alhadi: 2018-08-25 15:03:59

Way to learn std::bitset :D :D great problem!!

kshubham02: 2018-07-03 15:10:59

Amazing problem!
(Instead of a data structure like Fenwick tree, use offline algorithm)

bittu_coder: 2018-06-22 11:13:38

Wonderful Problem

vaibhav2303: 2018-06-06 17:32:39

Learnt something new today

soham_12345: 2018-06-05 09:54:56

vv nice problem. Thanks morass :D .

saihemanth9019: 2018-05-31 21:42:46

Fast IO to the rescue :)

nsasrr1998: 2018-05-17 07:10:19

Very Good Problem.
Learnt the use of bitset to solve the subset sum problem.

arpit_01: 2018-03-23 15:20:00

how to get 0.33 sec exec time? with bitset i passed using scanf and tle with cin/cout!!

satyampnc: 2018-03-21 21:42:06

tle with BITSET also.. help please

Last edit: 2018-03-21 22:22:39
anctartica: 2017-11-09 21:47:02

Where should I learn concept related to solving this problem if I am not able to make any further progress with my solution. @morass

Last edit: 2017-11-09 21:49:27

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