Under construction

Still being built. Things may change

Slide Count

Interview

Time limit1sMemory limit1024 MB

Summary
Given an array and a constant C, count for each element how many distinct windows produced by a two-pointer scan contain it.
Level

Medium5 of 10

Topics
Two pointers, Sliding window, Prefix sum
Solved
No attempts yet

Problem

In your programming class, you are given an assignment to analyze an integer array with a sliding window algorithm. Given NN integers w1,…,wNw_1, \ldots, w_N and a constant CC, the algorithm maintains start and end indices ss and ee as follows.

  • Initially s=e=1s = e = 1.
  • As long as s≤Ns \leq N:
    • if e+1>Ne+1 > N, increment ss;
    • else if ws+⋯+we+1>Cw_s + \cdots + w_{e+1} > C, increment ss;
    • else increment ee.

Each distinct pair of indices (s,e)(s,e) that appears during the execution defines a window. Element wiw_i belongs to the window (s,e)(s,e) if s≤i≤es \leq i \leq e. If s>es > e, the window is empty.

For the first sample, the windows are (1,1)(1,1), (1,2)(1,2), (1,3)(1,3), (2,3)(2,3), (3,3)(3,3), (3,4)(3,4), (4,4)(4,4), (5,4)(5,4), (5,5)(5,5), and (6,5)(6,5). For each element wiw_i, count how many different windows it belongs to during the execution.

Input

The first line contains two integers NN (1≤N≤100 0001 \leq N \leq 100\,000), the number of elements, and CC (1≤C≤1 000 0001 \leq C \leq 1\,000\,000), the window constant.

The next line contains NN integers w1,…,wNw_1, \ldots, w_N (0≤wi≤C0 \leq w_i \leq C).

Output

For each element, in input order, print the number of different windows it belongs to during the execution of the algorithm.

Examples2

  1. Example 1

    Input
    5 3
    1 1 1 2 2
    
    Expected output
    3
    3
    4
    2
    1
    
  2. Example 2

    Input
    5 10
    1 2 3 4 5
    
    Expected output
    4
    4
    4
    5
    2