[CF]Codeforces Round #515 (Div. 3)

A. Vova and Train

Description

Vova plans to go to the conference by train. Initially, the train is at the point 11 and the destination point of the path is the point LL. The speed of the train is 11 length unit per minute (i.e. at the first minute the train is at the point 11, at the second minute — at the point 22 and so on).

There are lanterns on the path. They are placed at the points with coordinates divisible by vv (i.e. the first lantern is at the point vv, the second is at the point 2v2v and so on).

There is also exactly one standing train which occupies all the points from ll to rr inclusive.

Vova can see the lantern at the point pp if pp is divisible by vv and there is no standing train at this position (p[l;r]p∉[l;r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.

Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to tt different conferences, so you should answer tindependent queries.

Input

The first line of the input contains one integer tt (1t1041≤t≤104) — the number of queries.

Then tt lines follow. The ii-th line contains four integers Li,vi,li,riLi,vi,li,ri (1L,v1091≤L,v≤109, 1lrL1≤l≤r≤L) — destination point of the ii-th path, the period of the lantern appearance and the segment occupied by the standing train.

Output

Print tt lines. The ii-th line should contain one integer — the answer for the ii-th query.

Examples

Input

4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000

Output

3
0
1134
0

Note

For the first example query, the answer is 33. There are lanterns at positions 22, 44, 66, 88 and 1010, but Vova didn't see the lanterns at positions 44 and 66 because of the standing train.

For the second example query, the answer is 00 because the only lantern is at the point 5151 and there is also a standing train at this point.

For the third example query, the answer is 11341134 because there are 12341234 lanterns, but Vova didn't see the lanterns from the position 100100 to the position 199199 inclusive.

For the fourth example query, the answer is 00 because the standing train covers the whole path.

正确解法:

一条路上有L米 [1,L] ,其中v的整数倍有灯笼,在[l,r]中没有灯笼,问这条路上总共有多少个灯笼。

简单的一个一个求整数倍肯定tle。

那就算 l是不是v的整数倍,r是不是v的整数倍。

这样分为四种情况后,发现只要验证 l 是不是整数倍就ok了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<cstring>
 5 #include<map>
 6 #include<set>
 7 #include<vector>
 8 #include<queue>
 9 #include<algorithm>
10 #include<cmath>
11 using namespace std;
12 typedef long long ll;
13 const int inf=0x7fffffff;
14 const int N=1010;
15 const int M=100000+10;
16 ll T,l,v,ll1,rr;
17 int main()
18 {
19     scanf("%d",&T);
20     while(T--)
21     {
22         ll ans=0;
23         scanf("%d %d %d %d",&l,&v,&ll1,&rr);
24         ans=l/v;
25         if(ll1%v==0)
26             ans=ans-(rr/v-ll1/v)-1;
27         else
28             ans=ans-(rr/v-ll1/v);
29         printf("%d\n",ans);
30     }
31 
32     return 0;
33 }
View Code

B. Heaters

Description

Vova's house is an array consisting of nn elements (yeah, this is the first problem, I think, where someone livesin the array). There are heaters in some positions of the array. The ii-th element of the array is 11 if there is a heater in the position ii, otherwise the ii-th element of the array is 00.

Each heater has a value rr (rr is the same for all heaters). This value means that the heater at the position posposcan warm up all the elements in range [posr+1;pos+r1][pos−r+1;pos+r−1].

Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.

Vova's target is to warm up the whole house (all the elements of the array), i.e. if n=6n=6, r=2r=2 and heaters are at positions 22 and 55, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first 33 elements will be warmed up by the first heater and the last 33 elements will be warmed up by the second heater).

Initially, all the heaters are off.

But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimumnumber of heaters on in such a way that each element of his house is warmed up by at least one heater.

Your task is to find this number of heaters or say that it is impossible to warm up the whole house.

Input

The first line of the input contains two integers nn and rr (1n,r10001≤n,r≤1000) — the number of elements in the array and the value of heaters.

The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai10≤ai≤1) — the Vova's house description.

Output

Print one integer — the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.

Examples

Input

6 2
0 1 1 0 0 1

Output

3

Input

5 3
1 0 0 0 1

Output

2

Note

In the first example the heater at the position 22 warms up elements [1;3][1;3], the heater at the position 33 warms up elements [2,4][2,4] and the heater at the position 66 warms up elements [5;6][5;6] so the answer is 33.

In the second example the heater at the position 11 warms up elements [1;3][1;3] and the heater at the position 55warms up elements [3;5][3;5] so the answer is 22.

正确解法:

找第一个放蜡烛的点,然后依次往后面找,下一个蜡烛的位置是 【前一个蜡烛的位置+1,前一个蜡烛的位置+2*r-1】

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<cstring>
 5 #include<map>
 6 #include<set>
 7 #include<vector>
 8 #include<queue>
 9 #include<algorithm>
10 #include<cmath>
11 using namespace std;
12 typedef long long ll;
13 const int inf=0x7fffffff;
14 const int N=1010;
15 const int M=100000+10;
16 int n,r,now=0,ans=0;
17 int a[N];
18 int main()
19 {
20     scanf("%d %d",&n,&r);
21     for(int i=1;i<=n;i++)
22         scanf("%d",&a[i]);
23     int flag=0;
24     for(int i=r;i>=1;i--)
25         if(a[i]==1)
26         {
27             now=i;
28             ans++;
29             flag=1;
30             break;
31         }
32     if(flag==0)
33         {
34             printf("-1\n");
35             return 0;
36         }
37 
38     while(now+r<=n)
39     {
40         flag=0;
41         for(int i=min(now+2*r-1,n);i>=now+1;i--)
42         {
43             if(a[i]==1)
44             {
45                 now=i;
46                 //cout<<now<<endl;
47                 ans++;
48                 flag=1;
49                 break;
50             }
51         }
52         if(flag==0)
53         {
54             printf("-1\n");
55             return 0;
56         }
57     }
58     printf("%d\n",ans);
59 
60     return 0;
61 }
View Code

C. Books Queries

Description

You have got a shelf and want to put some books on it.

You are given qq queries of three types:

  1. idid — put a book having index idid on the shelf to the left from the leftmost existing book;
  2. idid — put a book having index idid on the shelf to the right from the rightmost existing book;
  3. idid — calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index idid will be leftmost or rightmost.

You can assume that the first book you will put can have any position (it does not matter) and queries of type 33are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so idids don't repeat in queries of first two types.

Your problem is to answer all the queries of type 33 in order they appear in the input.

Note that after answering the query of type 33 all the books remain on the shelf and the relative order of books does not change.

Input

The first line of the input contains one integer qq (1q21051≤q≤2⋅105) — the number of queries.

Then qq lines follow. The ii-th line contains the ii-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 33, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).

It is guaranteed that there is at least one query of type 33 in the input.

In each query the constraint 1id21051≤id≤2⋅105 is met.

Output

Print answers to queries of the type 33 in order they appear in the input.

Examples

Input

8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1

Output

1
1
2

 

正确解法:

依次放序号为i的书,放在左边或右边,问 序号为i的书 min(从左边拿,从右边拿)。

桶排序,一个 l=r=0 记录这个书的下标。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<cstring>
 5 #include<map>
 6 #include<set>
 7 #include<vector>
 8 #include<queue>
 9 #include<algorithm>
10 #include<cmath>
11 using namespace std;
12 typedef long long ll;
13 const int inf=0x7fffffff;
14 const int N=2*1e5+10;
15 const int M=100000+10;
16 int a[N],l=0,r=1,q,x;
17 char s;
18 int main()
19 {
20     scanf("%d",&q);
21     while(q--)
22     {
23         cin>>s;
24         scanf("%d",&x);
25         if(s=='L')
26             a[x]=l--;
27         else if(s=='R')
28             a[x]=r++;
29         else
30             printf("%d\n",min(a[x]-l,r-a[x])-1);
31     }
32 
33     return 0;
34 }
View Code

A. Vova and Train

Description

Vova plans to go to the conference by train. Initially, the train is at the point 11 and the destination point of the path is the point LL. The speed of the train is 11 length unit per minute (i.e. at the first minute the train is at the point 11, at the second minute — at the point 22 and so on).

There are lanterns on the path. They are placed at the points with coordinates divisible by vv (i.e. the first lantern is at the point vv, the second is at the point 2v2v and so on).

There is also exactly one standing train which occupies all the points from ll to rr inclusive.

Vova can see the lantern at the point pp if pp is divisible by vv and there is no standing train at this position (p[l;r]p∉[l;r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.

Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to tt different conferences, so you should answer tindependent queries.

Input

The first line of the input contains one integer tt (1t1041≤t≤104) — the number of queries.

Then tt lines follow. The ii-th line contains four integers Li,vi,li,riLi,vi,li,ri (1L,v1091≤L,v≤109, 1lrL1≤l≤r≤L) — destination point of the ii-th path, the period of the lantern appearance and the segment occupied by the standing train.

Output

Print tt lines. The ii-th line should contain one integer — the answer for the ii-th query.

Examples

Input

4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000

Output

3
0
1134
0

Note

For the first example query, the answer is 33. There are lanterns at positions 22, 44, 66, 88 and 1010, but Vova didn't see the lanterns at positions 44 and 66 because of the standing train.

For the second example query, the answer is 00 because the only lantern is at the point 5151 and there is also a standing train at this point.

For the third example query, the answer is 11341134 because there are 12341234 lanterns, but Vova didn't see the lanterns from the position 100100 to the position 199199 inclusive.

For the fourth example query, the answer is 00 because the standing train covers the whole path.

正确解法:

猜你喜欢

转载自www.cnblogs.com/Kaike/p/10564101.html