This page is still under construction.

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

Desperate Fire Survive

Time limit3sMemory limit256 MB

Summary
For each query [l, r, k], count the sub-segments that can be merged and trimmed into one node of level exactly k.
Level

Hard9 of 10

Topics
Segment tree, Greedy, Divide and conquer
Solved
No attempts yet

Problem

Rikka is running with her every cell to the hall where the contest will be held.

Well, EC Final is growing bigger and bigger. More and more computers are connected to the weak 2000W main wires, and heat builds up in every row.

When Rikka enters the hall, she finds LCR there rearranging the wires with the volunteers. However, the girl might not be able to do anything helpful but observe.

"Listen. We have no time to calculate parameters for the new circuit. Are you good at data structures? Help us, please..."

The circuitry is a sequence of nn nodes, where there are mm possible levels of nodes in total, numbered from 11 to mm. A kk-level node has a power limit twice that of a (k−1)(k-1)-level node, so Rikka can merge two adjacent kk-level nodes into a (k+1)(k+1)-level node if k<mk < m. She can also remove any node at any time from the circuitry, and the order of the remaining nodes stays the same.

The volunteers have qq queries in total. Each query gives a segment [l,r][l,r] and an integer level kk. Rikka needs to count how many sub-segments of the given segment (that is, segments [x,y][x, y] with l≤x≤y≤rl \le x \le y \le r) can provide a kk-level node. A sub-segment [x,y][x, y] provides a kk-level node if it is possible to turn the sequence [x,y][x, y] into a single kk-level node by merging adjacent nodes at the same level and removing nodes. These two operations can be performed any number of times in any order. The level must be exactly kk, not higher or lower.

Input

The first line contains three integers nn, mm, qq (1≤n,m,q≤2×1051 \le n, m, q \le 2 \times 10^5), the length of the circuitry sequence, the maximal level, and the number of queries.

The second line contains nn integers A1,A2,…,AnA_1, A_2, \dots, A_n (1≤Ai≤m1 \le A_i \le m), the levels of the nodes in order.

Each of the following qq lines contains three integers ll, rr, kk (1≤l≤r≤n1 \le l \le r \le n, 1≤k≤m1 \le k \le m), describing a query. The integers in a line are separated by spaces.

Output

Output qq lines. Each line contains one integer, the answer to the corresponding query.

Examples2

  1. Example 1

    Input
    5 5 5
    1 1 1 1 1
    1 5 1
    1 5 2
    1 5 3
    1 5 4
    1 5 5
    
    Expected output
    15
    10
    3
    0
    0
    
  2. Example 2

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