HDU-4777 Rabbit Kingdomom(树状数组、区间离线)
title: HDU-4777 Rabbit Kingdomom(樹狀數(shù)組、區(qū)間離線)
date: 2018-12-14 11:20:15
tags: [樹狀數(shù)組,離線,區(qū)間]
categories: ACM
題目鏈接
Problem Description
Long long ago, there was an ancient rabbit kingdom in the forest. Every rabbit in this kingdom was not cute but totally pugnacious, so the kingdom was in chaos in season and out of season.
n rabbits were numbered form 1 to n. All rabbits’ weight is an integer. For some unknown reason, two rabbits would fight each other if and only if their weight is NOT co-prime.
Now the king had arranged the n rabbits in a line ordered by their numbers. The king planned to send some rabbits into prison. He wanted to know that, if he sent all rabbits between the i-th one and the j-th one(including the i-th one and the j-th one) into prison, how many rabbits in the prison would not fight with others.
Please note that a rabbit would not fight with himself.
Input
The input consists of several test cases.
The first line of each test case contains two integer n, m, indicating the number of rabbits and the queries.
The following line contains n integers, and the i-th integer Wi indicates the weight of the i-th rabbit.
Then m lines follow. Each line represents a query. It contains two integers L and R, meaning the king wanted to ask about the situation that if he sent all rabbits from the L-th one to the R-th one into prison.
(1 <= n, m, Wi <= 200000, 1 <= L <= R <= n)
The input ends with n = 0 and m = 0.
Output
For every query, output one line indicating the answer.
Sample Input
3 2 2 1 4 1 2 1 3 6 4 3 6 1 2 5 3 1 3 4 6 4 4 2 6 0 0Sample Output
2 1 1 3 1 2Hint
In the second case, the answer of the 4-th query is 2, because only 1 and 5 is co-prime with other numbers in the interval [2,6] .AC
- 詢問與區(qū)間內數(shù)字互質的個數(shù)
- 可以統(tǒng)計每個數(shù)字三個區(qū)間(左邊最近不互質點,i),(i, 右邊最近不互質點),(左邊最近不互質,右邊最近不互質)
- ans = 區(qū)間長度 - 區(qū)間1 - 區(qū)間2 + 區(qū)間3
總結
以上是生活随笔為你收集整理的HDU-4777 Rabbit Kingdomom(树状数组、区间离线)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客小白9 换个角度思考(离线+树状数组
- 下一篇: 牛客练习赛34 - C little w