This page is still under construction.

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

Typhoon

Time limit2sMemory limit1024 MB

Summary
Each typhoon covers a contiguous range of observation points; for each query, count how many typhoons in a given index range cover a given point.
Level

Medium7 of 10

Topics
Prefix sum, Sorting, Binary search, Array
Solved
No attempts yet

Problem

There is a straight road that is prone to damage from typhoons. On this road, the damage from a typhoon is always a single contiguous segment. Along the road there are kk observation points, numbered 11 to kk in order starting from the point closest to one end of the road.

There are records of nn typhoons that damaged this road. The record for typhoon ii is stored as the number aia_i of the lowest-numbered observation point damaged by typhoon ii and the number bib_i of the highest-numbered observation point damaged by typhoon ii. Typhoons are numbered 11 to nn in order from oldest to newest.

Recently it was discovered that studying typhoon damage on this road leads to a major advance in meteorology, and to carry out the research, mm pieces of information of the form "how many of the typhoons numbered qjq_j through rjr_j damaged observation point pjp_j" became necessary.

Given the typhoon records and the pairs of an observation point and a range of typhoon numbers for which information is needed to carry out the research (hereafter called queries), write a program that outputs, for each query, the number of typhoons that dealt damage.

Input

The first line of the input contains three integers nn, mm, kk separated by spaces. These mean that the number of typhoons in the records is nn, the number of queries given is mm, and the number of observation points is kk. They satisfy 1≤n,m≤100,0001 \le n, m \le 100,000 and 1≤k≤1,000,000,0001 \le k \le 1,000,000,000.

Line 1+i1 + i (1≤i≤n1 \le i \le n) contains two integers aia_i, bib_i separated by spaces. These mean that the lowest-numbered observation point damaged by typhoon ii is aia_i and the highest-numbered observation point damaged by typhoon ii is bib_i. They satisfy 1≤ai≤bi≤k1 \le a_i \le b_i \le k.

Line 1+n+j1 + n + j (1≤j≤m1 \le j \le m) contains three integers pjp_j, qjq_j, rjr_j separated by spaces. These mean that the jj-th query has point number pjp_j and typhoon number range from qjq_j to rjr_j. They satisfy 1≤pj≤k1 \le p_j \le k and 1≤qj≤rj≤n1 \le q_j \le r_j \le n.

Output

Output to standard output. For each query, output the number of typhoons that dealt damage, in the given order, separated by newlines. That is, on line jj (1≤j≤m1 \le j \le m), output a single integer representing how many of the typhoons numbered qjq_j through rjr_j damaged observation point pjp_j.

Examples1

  1. Example 1

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