天天看點

HDU 5826 physics

Problem Description

There are n balls on a smooth horizontal straight track. The track can be considered to be a number line. The balls can be considered to be particles with the same mass.

At the beginning, ball i is at position Xi. It has an initial velocity of Vi and is moving in direction 

Di.(Di∈−1,1)

Given a constant C. At any moment, ball its acceleration Ai and velocity Vi have the same direction, and magically satisfy the equation that Ai * Vi = C.

As there are multiple balls, they may collide with each other during the moving. We suppose all collisions are perfectly elastic collisions.

There are multiple queries. Each query consists of two integers t and k. our task is to find out the k-small velocity of all the balls t seconds after the beginning.

* Perfectly elastic collision : A perfectly elastic collision is defined as one in which there is no loss of kinetic energy in the collision.

Input

The first line contains an integer T, denoting the number of testcases.

For each testcase, the first line contains two integers n <= 10^5 and C <= 10^9.

n lines follow. The i-th of them contains three integers Vi, Xi, Di. Vi denotes the initial velocity of ball i. Xi denotes the initial position of ball i. Di denotes the direction ball i moves in. 

The next line contains an integer q <= 10^5, denoting the number of queries.

q lines follow. Each line contains two integers t <= 10^9 and 1<=k<=n.

1<=Vi<=10^5,1<=Xi<=10^9

Output

For each query, print a single line containing the answer with accuracy of 3 decimal digits.

Sample Input

1

3 7

3 3 1

3 10 -1

2 7 1

3

2 3

1 2

3 3

Sample Output

6.083

4.796

7.141

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 370000;
const int read()
{
    char ch = getchar();
    while (ch<'0' || ch>'9') ch = getchar();
    int x = ch - '0';
    while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
    return x;
}
int T, n, m, c, a[N], x, y;

int main()
{
    T = read();
    while (T--)
    {
        scanf("%d%d", &n, &c);
        rep(i, 1, n) scanf("%d%d%d", &a[i], &x, &y);
        sort(a + 1, a + n + 1);
        scanf("%d", &m);
        while (m--)
        {
            scanf("%d%d", &x, &y);
            printf("%.3lf\n", sqrt(1.0 * a[y] * a[y] + 2.0 * c * x));
        }
    }
    return 0;
}