2309mo's algorithm

2309   mo's algorithm

题目描述

We all know that a problem is hard when it requires more than one algorithm in the same moment.

         But we still tend to believe bravery can help us overcome difficulties.

         The result of this problem will depend on how brave you are.

You have an array a and its’s size equals n.

There are exactly n positive constant number in your array.

         Now you are faced with q query which consists of four integer: L1, R1, L2, R2.

         Please answer how many different pair(i, j) where L1 <= i <= R1 and L2 <= j <= R2 and a[i] = a[j].

We consider two pair is same if and only if they look the same completely.

For example, (3, 3) is different from (3, 5).

(3, 5) and (3, 5) look the same.
        Attention![L1, R1] may intersect with [L2, R2]

输入格式:

An integer T on the first line representing there will be T test cases1 <= T <= 100

An integer n on the second line representing your array’s size.1 <= n <= 100

There are exactly n positive integers ai on the third line separated by space, each represents the ith element.1 <= ai <= 100

An integer q on the fourth line representing that there will be q query waiting for you.

Q lines follows, each contains four integer: L1, R1, L2, R2.

1 <= L1 <= R1 <= n

1 <= L2 <= R2 <= n

输出格式:

For each query, print an integer respecting your answer in one line.

输入样例 复制
1
5
1 2 3 2 1
4
1 1 5 5
1 5 1 5
1 2 4 5
1 3 3 5
输出样例 复制
1
9
2
3

说明

All legal pair(i, j) of the first query:(15

All legal pair(i, j) of the second query:(11),(15),(22),(24),(33),(42),(44),(51),(55

All legal pair(i, j) of the third query:(15),(24

All legal pair(i, j) of the fifth query:(15),(24),(33

Someone said : “Inclusion-exclusion principle + Off-line algorithms +Mo’s algorithmnow you are accepted.”

17
18
通过提交
时空限制1000ms/128mb
题目来源
评测方式在线评测
题目类型
难        度