This page is still under construction.

Parts of this page are still being built. What you see may change.

Ice Skates

Time limit1sMemory limit128 MB

Summary
After each of m membership events, decide whether every current member can be assigned skates, given k pairs of each size and foot sizes with tolerance d.
Level

Hard8 of 10

Topics
Segment tree, Greedy, Prefix sum
Solved
No attempts yet

Problem

Byteasar runs a skating club. Its members meet regularly to train together, and they always use the club's ice skates. Skate sizes are numbered from 11 to nn.

Each member has a foot size, but that alone does not determine which skates they can wear: a skater has a size tolerance dd, so a skater whose foot size is rr can wear any skate of size from rr to r+dr + d. A skater always wears a single pair of skates of the same size, never two different sizes at once.

To stock the club, Byteasar bought kk pairs of skates of every size, that is kk pairs for each size from 11 to nn. Over time some people join the club while others leave, and Byteasar worries whether he will always have enough skates of a suitable size for every current member.

At the start the club has no members. You are given a sequence of mm events. Each event means that xx members with foot size rr have just joined the club (when x≥0x \ge 0) or just left it (when x<0x < 0). Immediately after every event, decide whether Byteasar has skates of a suitable size for every member of the club at that moment.

Input

The first line contains four integers nn, mm, kk, and dd (1≤n≤200 0001 \le n \le 200\,000, 1≤m≤500 0001 \le m \le 500\,000, 1≤k≤1091 \le k \le 10^9, 0≤d<n0 \le d < n), separated by single spaces: the largest skate size, the number of events, the number of skate pairs bought of each size, and the size tolerance.

Each of the next mm lines describes one event with two integers rir_i and xix_i (1≤ri≤n−d1 \le r_i \le n - d, −109≤xi≤109-10^9 \le x_i \le 10^9), separated by a single space. If xi≥0x_i \ge 0, then xix_i new members with foot size rir_i have just joined the club; if xi<0x_i < 0, then ∣xi∣|x_i| members with foot size rir_i have just left it. The sequence is always consistent: a member who never joined can never leave.

Output

Print mm lines. The ii-th line must contain TAK (Polish for yes) if, right after the ii-th event, Byteasar has skates of a suitable size for every club member, or NIE (Polish for no) otherwise.

Note

To see how a full assignment can work, suppose that at some moment three members can wear skates of size 11 or 22, two members can wear size 22 or 33, and three members can wear size 33 or 44. Two pairs of skates of each of the sizes 11, 22, 33, and 44 are then enough for everyone:

  • two members take skates of size 11;
  • the size-22 skates go to one member who can wear size 11 or 22 and one who can wear size 22 or 33;
  • the size-33 skates go to one member who can wear size 22 or 33 and one who can wear size 33 or 44;
  • the remaining two members take skates of size 44.

Examples1

  1. Example 1

    Input
    4 4 2 1
    1 3
    2 3
    3 3
    2 -1
    
    Expected output
    TAK
    TAK
    NIE
    TAK