Statistics

Problem Statement for "PopChartDominance"

Problem Statement

Recently, the artist Kate Bush made history: thanks to her song being featured in Stranger Things her old song became a #1 hit in the charts, a stunning 44 years since her previous #1 hit. This inspired us to write a problem about pop charts statistics.

Who were the dominant artists in the sixties? How can we tell? One necessarily imprecise but really simple way of measuring dominance within a time interval is to look at the artist's first and last #1 hit within that period. The farther apart they are, the longer the artist was popular in that period.


We will consider a period of N days, numbered from 0 to N-1. You will be given a sequence A[0..N-1]: for each day i, A[i] is the ID of the artist who was on top of the charts that day.

Let set(A) denote the set of distinct values that appear in A.


A half-open interval [lo, hi) consists of all i such that lo <= i < hi.

Given an interval [lo, hi) and an artist x, the dominance of artist x within [lo, hi) is calculated as follows:

  • If there is no t in [lo, hi) such that A[t] = x, the dominance is 0.
  • If there is one or more such t, let tmax and tmin be the largest and smallest among them. Then, the dominance is (tmax-tmin).

(Note that an artist must have a #1 hit on at least two distinct days within the given interval in order to have a positive dominance. A single day still only yields zero dominance.)


You will be given two arrays L and H, each of length Q.

For each valid i, consider the interval [ L[i], H[i] ). For each artist in set(A), calculate their dominance within this interval. Let ans[i] be the sum of those dominances.

Let TOTAL be the sum of (i+1)*ans[i] over all i. Return TOTAL modulo (10^9 + 7).


----------------------------------------------------------


As we need to keep the input size small, the input will be generated pseudorandomly. Please follow the pseudocode below to generate the arrays A, L, H.

(The pseudocode takes the provided prefixes of A, L, H. The rest of A is filled with pseudorandom values from [0,Alimit) and the rest of L and H is filled with pseudorandom queries whose lengths are between minQL and maxQL, inclusive.)


state = seed

A = Aprefix
while length(A) < N:
    state = (state * 1103515245 + 12345) modulo 2^31
    A.append( state modulo Alimit )

L = Lprefix
H = Hprefix
while length(L) < Q:
    state = (state * 1103515245 + 12345) modulo 2^31
    ql = minQL + (state modulo (maxQL-minQL+1))

    state = (state * 1103515245 + 12345) modulo 2^31
    lo = state modulo (N-ql+1)

    L.append(lo)
    H.append(lo+ql)

Definition

Class:
PopChartDominance
Method:
count
Parameters:
int, int, int[], int, int[], int[], int, int, int
Returns:
int
Method signature:
int count(int N, int Q, int[] Aprefix, int Alimit, int[] Lprefix, int[] Hprefix, int minQL, int maxQL, int seed)
(be sure your method is public)

Notes

  • The reference solution does not depend on the assumption that the input is pseudorandom.

Constraints

  • N will be between 1 and 300,000, inclusive.
  • Q will be between 1 and 300,000, inclusive.
  • Aprefix will have between 0 and min(100, N) elements, inclusive.
  • Each element of Aprefix will be between 0 and Alimit-1, inclusive.
  • Alimit will be between 1 and 300,000, inclusive.
  • Lprefix will have between 0 and min(100, Q) elements, inclusive.
  • Hprefix will have the same number of elements as Lprefix.
  • For each valid index i we will have 0 <= Lprefix[i] < Hprefix[i] <= N.
  • minQL and maxQL will satisfy 1 <= minQL <= maxQL <= N.
  • seed will be between 0 and 2^31 - 1, inclusive.

Examples

  1. 7

    2

    {10, 20, 30, 40, 50, 60, 70}

    100

    {0, 3}

    {7, 5}

    1

    7

    47

    Returns: 0

    Seven days, two queries. As A = {10, 20, 30, 40, 50, 60, 70}, there was no artist with more than one day on top of the charts. Thus, for each query interval and each artist their dominance is zero. This means that ans[0] = ans[1] = 0 and TOTAL = 1*ans[0] + 2*ans[1] = 0.

  2. 9

    5

    {10, 20, 10, 30, 10, 20, 10, 40, 30}

    100

    {0, 0, 5, 0, 3}

    {9, 9, 9, 5, 8}

    1

    9

    47

    Returns: 71

    Again, the entire A, L, H are given, nothing is pseudorandom. Each of the first two queries asks about the entire array A. In this array, artist 10 has dominance 6, artist 20 has dominance 4, artist 30 has dominance 5 and artist 40 has dominance 0. The third query asks about the segment {20, 10, 40, 30}. Everyone' dominance is zero. The fourth query asks about {10, 20, 10, 30, 10}. Artist 10 has dominance 4. The fifth query asks about {30, 10, 20, 10, 40}. Again, only artist 10 has a non-zero dominance: 2. Thus, TOTAL is 1*(6+4+5+0) + 2*(6+4+5+0) + 3*(0+0+0+0) + 4*(4+0+0+0) + 5*(2+0+0+0) = 71.

  3. 30

    10

    {4, 7, 5, 18}

    20

    {0, 5}

    {30, 18}

    3

    10

    47

    Returns: 189

    Most of A, L, H are generated pseudorandomly. For reference, their correct contents are given below. A = { 4, 7, 5, 18, 8, 5, 18, 11, 8, 13, 10, 7, 16, 17, 6, 3, 0, 1, 2, 15, 12, 13, 10, 15, 16, 13, 18, 15, 0, 9 } L = { 0, 5, 1, 21, 5, 13, 13, 25, 13, 21 } H = { 30, 18, 6, 24, 14, 20, 18, 28, 22, 28 } Below is the full list of situations when some artist has a positive dominance: in interval [0,30) artist 0 has dominance 12 in interval [0,30) artist 5 has dominance 3 in interval [0,30) artist 7 has dominance 10 in interval [0,30) artist 8 has dominance 4 in interval [0,30) artist 10 has dominance 12 in interval [0,30) artist 13 has dominance 16 in interval [0,30) artist 15 has dominance 8 in interval [0,30) artist 16 has dominance 12 in interval [0,30) artist 18 has dominance 23 in interval [1,6) artist 5 has dominance 3 in interval [21,28) artist 13 has dominance 4 in interval [21,28) artist 15 has dominance 4

  4. 300000

    300000

    {}

    100000

    {}

    {}

    1

    300000

    4747

    Returns: 34267726

  5. 300000

    300000

    {}

    1

    {}

    {}

    2

    2

    424242

    Returns: 149685

    The array A contains 300,000 zeros. Each query has length 2. The answer to each query is 1. Thus, TOTAL = 1+2+...+300000 = 45000150000. Remember to compute TOTAL modulo 10^9 + 7 and watch out for integer overflows.

  6. 1

    300000

    {47}

    48

    {}

    {}

    1

    1

    24

    Returns: 0

  7. 281529

    272048

    {}

    15624

    {}

    {}

    134967

    268669

    1660249968

    Returns: 235752872

  8. 282858

    290170

    {}

    3

    {}

    {}

    8

    502

    39237771

    Returns: 359213997

  9. 296729

    299449

    {}

    1899

    {}

    {}

    219

    24315

    142868263

    Returns: 124659221

  10. 288381

    282260

    {}

    3448

    {}

    {}

    26

    137

    420679459

    Returns: 488660086

  11. 281889

    288951

    {}

    6167

    {}

    {}

    124

    213

    473072173

    Returns: 468903391

  12. 295332

    283653

    {}

    3220

    {}

    {}

    2212

    199646

    2078532398

    Returns: 504308817

  13. 280230

    288439

    {}

    6227

    {}

    {}

    11

    8072

    1087222470

    Returns: 756056247

  14. 289776

    288285

    {}

    30

    {}

    {}

    6

    37

    2046595582

    Returns: 887575107

  15. 295635

    296275

    {}

    14181

    {}

    {}

    497

    1069

    1043761029

    Returns: 911123692

  16. 276925

    281336

    {}

    11206

    {}

    {}

    19

    45788

    586949650

    Returns: 74011395

  17. 293934

    295726

    {}

    3655

    {}

    {}

    1

    483

    1778417307

    Returns: 624788840

  18. 272200

    282099

    {}

    1

    {}

    {}

    21

    129890

    863925910

    Returns: 836284161

  19. 295660

    295325

    {}

    62494

    {}

    {}

    5

    26

    200513682

    Returns: 762210638

  20. 298755

    290383

    {}

    143

    {}

    {}

    11647

    64566

    380485713

    Returns: 901151681

  21. 279919

    271765

    {}

    211724

    {}

    {}

    170709

    202679

    346617440

    Returns: 468280755

  22. 288447

    286097

    {}

    1130

    {}

    {}

    3288

    15034

    2101334812

    Returns: 913211945

  23. 289584

    274576

    {}

    4

    {}

    {}

    9374

    237905

    1866519440

    Returns: 714427721

  24. 296531

    288478

    {}

    74211

    {}

    {}

    257008

    263400

    490178290

    Returns: 430992681

  25. 294738

    278032

    {}

    3404

    {}

    {}

    15

    17124

    1977954749

    Returns: 516315575

  26. 279069

    290907

    {}

    137306

    {}

    {}

    503

    553

    1647993519

    Returns: 837560686

  27. 272243

    293672

    {}

    15

    {}

    {}

    163420

    216057

    879909240

    Returns: 214445777

  28. 293998

    290726

    {}

    4

    {}

    {}

    174460

    212379

    1546714289

    Returns: 635229364

  29. 297888

    273565

    {}

    7

    {}

    {}

    26343

    183387

    242563618

    Returns: 793737859

  30. 282840

    290772

    {}

    386

    {}

    {}

    726

    26198

    1912313258

    Returns: 232667970

  31. 295418

    284432

    {}

    1136

    {}

    {}

    1

    32808

    1964529467

    Returns: 457556461

  32. 287046

    284348

    {}

    406

    {}

    {}

    59

    638

    1445927722

    Returns: 83619954

  33. 286303

    283959

    {}

    195

    {}

    {}

    12094

    198874

    1983527925

    Returns: 121849673

  34. 290217

    288294

    {}

    56

    {}

    {}

    7

    43

    1765314531

    Returns: 682691500

  35. 271974

    277353

    {}

    22148

    {}

    {}

    2341

    72924

    312614420

    Returns: 783055964

  36. 288356

    282750

    {}

    1

    {}

    {}

    195665

    238616

    526066028

    Returns: 125223418

  37. 286752

    283123

    {}

    9

    {}

    {}

    54664

    215982

    750893228

    Returns: 937892788

  38. 299608

    291794

    {}

    63950

    {}

    {}

    49139

    207358

    1406159933

    Returns: 782328148

  39. 284174

    294540

    {}

    104

    {}

    {}

    39107

    69476

    2094311409

    Returns: 82259736

  40. 280913

    291297

    {}

    12

    {}

    {}

    48607

    187253

    127702674

    Returns: 75879472

  41. 282928

    290470

    {}

    5

    {}

    {}

    628

    99310

    268214787

    Returns: 449606846

  42. 279401

    293581

    {}

    63

    {}

    {}

    1

    7979

    1010397418

    Returns: 758808091

  43. 290490

    288165

    {}

    2485

    {}

    {}

    33708

    82353

    454791749

    Returns: 911938666

  44. 277831

    297675

    {}

    37

    {}

    {}

    36

    87575

    398097787

    Returns: 832957340

  45. 274127

    289303

    {}

    6949

    {}

    {}

    13023

    188624

    1489995248

    Returns: 464438555

  46. 295186

    272952

    {}

    1232

    {}

    {}

    81

    1210

    1371506773

    Returns: 56006817

  47. 295190

    293442

    {}

    13961

    {}

    {}

    228611

    230227

    1774309568

    Returns: 403490712

  48. 293847

    286077

    {}

    1

    {}

    {}

    25728

    224672

    434271713

    Returns: 658599314

  49. 284388

    295341

    {}

    9

    {}

    {}

    30940

    229196

    1433250621

    Returns: 889396788

  50. 295813

    294727

    {}

    58

    {}

    {}

    249031

    271557

    513342065

    Returns: 679855978

  51. 277518

    276507

    {}

    1

    {}

    {}

    74453

    85889

    1241566301

    Returns: 731778523

  52. 272378

    271538

    {}

    557

    {}

    {}

    91

    432

    1586454074

    Returns: 865991714

  53. 299558

    282146

    {}

    22

    {}

    {}

    18109

    158131

    1286708360

    Returns: 326089553

  54. 283820

    273630

    {}

    15045

    {}

    {}

    134650

    260980

    194969798

    Returns: 575921991

  55. 293725

    288512

    {}

    47823

    {}

    {}

    30658

    212615

    103084142

    Returns: 970811917

  56. 282881

    276250

    {}

    14202

    {}

    {}

    3436

    18604

    281126873

    Returns: 765071518

  57. 285504

    278742

    {}

    5

    {}

    {}

    236

    45708

    1274375336

    Returns: 105683104

  58. 270194

    273741

    {}

    1

    {}

    {}

    46809

    228765

    1427747666

    Returns: 527335349

  59. 283122

    299861

    {}

    3

    {}

    {}

    180736

    281858

    32134090

    Returns: 287750142

  60. 273581

    270673

    {}

    561

    {}

    {}

    2

    65976

    1646888126

    Returns: 113093476

  61. 299263

    295004

    {}

    22641

    {}

    {}

    458

    111131

    162392144

    Returns: 327525829

  62. 299054

    272138

    {}

    830

    {}

    {}

    13641

    31569

    386253728

    Returns: 109870314

  63. 278297

    297949

    {}

    14311

    {}

    {}

    50

    7188

    127958513

    Returns: 126930620

  64. 270998

    288845

    {}

    80

    {}

    {}

    4

    15198

    1787838088

    Returns: 769056057

  65. 293169

    283862

    {}

    20

    {}

    {}

    241215

    282534

    1128032479

    Returns: 651686916

  66. 290983

    297039

    {}

    18

    {}

    {}

    4

    160

    588301709

    Returns: 775651074


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: