VK Cup 2018 Round 2: D. Contact ATC(思维+树状数组)

探讨了在给定风速范围内,如何计算可能同时到达指定位置的不同飞机组合数量的问题。通过两次排序和树状数组来实现高效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  standard output

Arkady the air traffic controller is now working with n planes in the air. All planes move along a straight coordinate axis with Arkady's station being at point 0 on it. The i-th plane, small enough to be represented by a point, currently has a coordinate of xi and is moving with speed vi. It's guaranteed that xi·vi < 0, i.e., all planes are moving towards the station.

Occasionally, the planes are affected by winds. With a wind of speed vwind (not necessarily positive or integral), the speed of the i-th plane becomes vi + vwind.

According to weather report, the current wind has a steady speed falling inside the range [ - w, w] (inclusive), but the exact value cannot be measured accurately since this value is rather small — smaller than the absolute value of speed of any plane.

Each plane should contact Arkady at the exact moment it passes above his station. And you are to help Arkady count the number of pairs of planes (i, j) (i < j) there are such that there is a possible value of wind speed, under which planes i and j contact Arkady at the same moment. This value needn't be the same across different pairs.

The wind speed is the same for all planes. You may assume that the wind has a steady speed and lasts arbitrarily long.

Input

The first line contains two integers n and w (1 ≤ n ≤ 100 0000 ≤ w < 105) — the number of planes and the maximum wind speed.

The i-th of the next n lines contains two integers xi and vi (1 ≤ |xi| ≤ 105w + 1 ≤ |vi| ≤ 105xi·vi < 0) — the initial position and speed of the i-th plane.

Planes are pairwise distinct, that is, no pair of (i, j) (i < j) exists such that both xi = xj and vi = vj.

Output

Output a single integer — the number of unordered pairs of planes that can contact Arkady at the same moment.

Examples
input
5 1
-3 2
-3 3
-1 2
1 -3
3 -5
output
3
input
6 1
-3 2
-2 2
-1 2
1 -2
2 -2
3 -2
output
9


这题难在卡精度吧。。其实只用排两次序然后一个很简单的树状数组就搞定了

题意:在数轴上有n架飞机,它们的的位置分别是x[i],它们各以v[i]的速度,朝着原点移动(x[i]*v[i]<0),可是按道理这个时候是有风的,风的范围是[-w, w],你不知道风具体多大,只知道无论风多大每架飞机都一定还可以到达原点(也就是不会被吹不动或者 变成反方向),问你有多少架飞机可能在某个情况下同时到达原点

也就是问有多少对点满足方程的解p∈[-w, w]


对这个方程求导你就会发现关于p的函数是单调的,这就意味着当且仅当p = -w或者p = w时方程f(p)取得极值

这也就意味着:对于飞机A和B,如果在风速为-w时,A先到达原点,而在风速为w时B先到达点,那么A和B就有可能同时到达原点,这道题就是统计有多少对飞机满足这个要求

具体怎么统计呢?

先按风速为-w时每架飞机到达原点时间排序,再按风速为w时每架飞机到达远点排序,如果A和B满足在这两个序列中偏序不同,就说明他们可以同时到达原点,统计很好办,按照第一个排序序列一个一个的将飞机加入树状数组,然后在第二个排序序列中查询它前面还有多少架飞机没有加入树状数组中就OK了,复杂度O(nlogn)

为什么会卡常数?

因为有可能刚好风速为-w和w时两架飞机在原点相遇,即函数f(p)在极值点时值为0,这个时候它们在某个序列中应该没有顺序可言,而这个时候按顺序统计就有可能出错

解决方法①:很容易想到其实只要将w加上一个很小的值就可以搞定了,我就是这个方法,但是这个值是多少就很难说了,太大了可能本来无法相遇算成相遇,太小了精度有损失,我试了当这个值为0.00001的时候刚好AC

解决方法②:除法计算改成乘法计算,然后排序之前将飞机离散化,这样应该答案一定是对的,但很麻烦就是了


#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define LL long long
typedef struct Res
{
	int id;
	double x, v;
	bool operator < (const Res &b) const
	{
		if(fabs(x*b.v)<fabs(v*b.x))
			return 1;
		return 0;
	}
}Point;
Point s[100005];
int n, a[100005], b[100005], tre[100005];
int Query(int k)
{
	int ans = 0;
	while(k)
	{
		ans += tre[k];
		k -= k &-k;
	}
	return ans;
}
void Update(int k, int val)
{
	while(k<=n)
	{
		tre[k] += val;
		k += k&-k;
	}
}
int main(void)
{
	int i;
	LL ans;
	double w;
	scanf("%d%lf", &n, &w);
	w += 1e-6;
	for(i=1;i<=n;i++)
	{
		scanf("%lf%lf", &s[i].x, &s[i].v);
		s[i].id = i, s[i].v -= w;
	}
	sort(s+1, s+n+1);
	for(i=1;i<=n;i++)
		a[i] = s[i].id;
	for(i=1;i<=n;i++)
		s[i].v += 2*w;
	sort(s+1, s+n+1);
	for(i=1;i<=n;i++)
		b[s[i].id] = i;
	ans = 0;
	for(i=1;i<=n;i++)
	{
		ans += i-1-Query(b[a[i]]);
		Update(b[a[i]], 1);
	}
	printf("%lld\n", ans);
	return 0;
}


E Sync task failure [CONTEXT service_id=51 ] bali: 29504: Network error at baor.F(:com.google.android.gms@221821047@22.18.21 (190800-453244992):218) at baor.o(:com.google.android.gms@221821047@22.18.21 (190800-453244992):61) at baor.C(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0) at baor.n(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0) at com.google.android.gms.phenotype.sync.HeterodyneSyncTaskChimeraService.f(:com.google.android.gms@221821047@22.18.21 (190800-453244992):17) at com.google.android.gms.phenotype.sync.HeterodyneSyncTaskChimeraService.a(:com.google.android.gms@221821047@22.18.21 (190800-453244992):4) at amql.call(:com.google.android.gms@221821047@22.18.21 (190800-453244992):4) at java.util.concurrent.FutureTask.run(FutureTask.java:264) at wwq.c(:com.google.android.gms@221821047@22.18.21 (190800-453244992):6) at wwq.run(:com.google.android.gms@221821047@22.18.21 (190800-453244992):7) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637) at xbv.run(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0) at java.lang.Thread.run(Thread.java:1012) Caused by: java.io.InterruptedIOException at dgpu.d(:com.google.android.gms@221821047@22.18.21 (190800-453244992):3) at dgpu.b(:com.google.android.gms@221821047@22.18.21 (190800-453244992):5) at dgpu.a(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0) at dgpr.l(:com.google.android.gms@221821047@22.18.21 (190800-453244992):4) at dgpr.getResponseCode(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0) at baox.a(:com.google.android.gms@221821047@22.18.21 (190800-453244992):20) at baor.F(:com.google.android.gms@221821047@22.18.21 (190800-453244992):176) at baor.o(:com.google.android.gms@221821047@22.18.21 (190800-453244992):61)  at baor.C(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0)  at baor.n(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0)  at com.google.android.gms.phenotype.sync.HeterodyneSyncTaskChimeraService.f(:com.google.android.gms@221821047@22.18.21 (190800-453244992):17)  at com.google.android.gms.phenotype.sync.HeterodyneSyncTaskChimeraService.a(:com.google.android.gms@221821047@22.18.21 (190800-453244992):4)  at amql.call(:com.google.android.gms@221821047@22.18.21 (190800-453244992):4)  at java.util.concurrent.FutureTask.run(FutureTask.java:264)  at wwq.c(:com.google.android.gms@221821047@22.18.21 (190800-453244992):6)  at wwq.run(:com.google.android.gms@221821047@22.18.21 (190800-453244992):7)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)  at xbv.run(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0)  at java.lang.Thread.run(Thread.java:1012)  Caused by: java.lang.InterruptedException at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:2056) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2090) at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:433) at dgpu.d(:com.google.android.gms@221821047@22.18.21 (190800-453244992):1) at dgpu.b(:com.google.android.gms@221821047@22.18.21 (190800-453244992):5)  at dgpu.a(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0)  at dgpr.l(:com.google.android.gms@221821047@22.18.21 (190800-453244992):4)  at dgpr.getResponseCode(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0)  at baox.a(:com.google.android.gms@221821047@22.18.21 (190800-453244992):20)  at baor.F(:com.google.android.gms@221821047@22.18.21 (190800-453244992):176)  at baor.o(:com.google.android.gms@221821047@22.18.21 (190800-453244992):61)  at baor.C(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0)  at baor.n(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0)  at com.google.android.gms.phenotype.sync.HeterodyneSyncTaskChimeraService.f(:com.google.android.gms@221821047@22.18.21 (190800-453244992):17)  at com.google.android.gms.phenotype.sync.HeterodyneSyncTaskChimeraService.a(:com.google.android.gms@221821047@22.18.21 (190800-453244992):4)  at amql.call(:com.google.android.gms@221821047@22.18.21 (190800-453244992):4)  at java.util.concurrent.FutureTask.run(FutureTask.java:264)  at wwq.c(:com.google.android.gms@221821047@22.18.21 (190800-453244992):6)  at wwq.run(:com.google.android.gms@221821047@22.18.21 (190800-453244992):7)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)  at xbv.run(:com.google.android.gms@221821047@22.18.21 (190800-453244992):0)  at java.lang.Thread.run(Thread.java:1012)  2025-06-16 17:56:05.552 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 17:57:40.734 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 18:00:22.639 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 18:22:13.792 1228-2530 NetworkScheduler.ATC com.google.android.gms.persistent E Error finding compatible intent-filter to handle SERVICE_ACTION_EXECUTE_TASK for com.google.android.gms/.chimera.GmsInternalBoundBrokerService [CONTEXT service_id=218 ] 2025-06-16 18:26:05.538 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 18:27:40.917 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 18:27:43.406 8169-8169 droid.bluetooth pid-8169 E [0616/102743.406297:ERROR:message_loop_thread.cc(103)] DoInThreadDelayed: message loop is null for thread bt_main_thread(-1), from pc:0x7972619f8479 2025-06-16 18:27:43.406 8169-8169 droid.bluetooth pid-8169 E [0616/102743.406526:ERROR:btu_task.cc(94)] do_in_main_thread: failed from pc:0x7972619f8479 2025-06-16 18:27:43.406 8169-8169 droid.bluetooth pid-8169 E [0616/102743.406678:ERROR:message_loop_thread.cc(103)] DoInThreadDelayed: message loop is null for thread bt_main_thread(-1), from pc:0x7972619f8479 2025-06-16 18:27:43.407 8169-8169 droid.bluetooth pid-8169 E [0616/102743.407441:ERROR:btu_task.cc(94)] do_in_main_thread: failed from pc:0x7972619f8479 2025-06-16 18:27:43.407 8169-8169 droid.bluetooth pid-8169 E [0616/102743.407834:ERROR:message_loop_thread.cc(103)] DoInThreadDelayed: message loop is null for thread bt_main_thread(-1), from pc:0x7972619f8479 2025-06-16 18:27:43.408 8169-8169 droid.bluetooth pid-8169 E [0616/102743.408232:ERROR:btu_task.cc(94)] do_in_main_thread: failed from pc:0x7972619f8479 2025-06-16 18:28:43.107 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 18:56:05.539 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 18:57:41.176 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 19:00:38.651 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 19:26:05.592 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 19:27:41.441 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 19:32:51.392 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 19:33:01.258 627-1353 OpenGLRenderer system_server W Failed to initialize 101010-2 format, error = EGL_SUCCESS 2025-06-16 19:33:03.277 627-651 Looper system_server W Slow dispatch took 145ms android.ui h=com.android.server.am.ActivityManagerService$UiHandler c=com.android.server.am.ErrorDialogController$$ExternalSyntheticLambda0@7b9f872 m=0 2025-06-16 19:33:06.252 627-1353 OpenGLRenderer system_server W Failed to initialize 101010-2 format, error = EGL_SUCCESS 2025-06-16 19:33:06.290 8689-8755 tencentmap pid-8689 E Auth Fail: ******************************************************************************** 腾讯地图鉴权失败,请访问 lbs.qq.com 检查 key 配置 错误码:-202 错误信息:KEY_FORMAT_ERROR ******************************************************************************** 2025-06-16 19:33:06.330 627-651 CoreBackPreview system_server D Window{3c09982 u0 Application Error: com.example.myapplication}: Setting back callback OnBackInvokedCallbackInfo{mCallback=android.window.WindowOnBackInvokedDispatcher$OnBackInvokedCallbackWrapper@11f8885, mPriority=0} 2025-06-16 19:33:06.330 627-651 Looper system_server W Slow dispatch took 131ms android.ui h=com.android.server.am.ActivityManagerService$UiHandler c=com.android.server.am.ErrorDialogController$$ExternalSyntheticLambda3@7552801 m=0 2025-06-16 19:33:33.890 627-651 CoreBackPreview system_server D Window{3c09982 u0 Application Error: com.example.myapplication}: Setting back callback null 2025-06-16 19:33:33.912 627-651 InputManager-JNI system_server W Input channel object '3c09982 Application Error: com.example.myapplication (client)' was disposed without first being removed with the input manager! 2025-06-16 19:33:53.386 627-667 BatteryExt...tatsWorker system_server W error reading Bluetooth stats: 9 2025-06-16 19:36:12.558 627-1353 OpenGLRenderer system_server W Failed to initialize 101010-2 format, error = EGL_SUCCESS 2025-06-16 19:36:19.167 627-651 Looper system_server W Slow dispatch took 112ms android.ui h=com.android.server.am.ActivityManagerService$UiHandler c=com.android.server.am.ErrorDialogController$$ExternalSyntheticLambda1@f559ba9 m=0 2025-06-16 19:36:21.636 8915-8952 Bugle com.google.android.apps.messaging W AssistantIntegrationStartupTask: Error getting assistant availability. java.lang.IllegalStateException: Failed to query AGSA value. This is most likely caused by a Google signature check failure. Please make sure both of the AGSA app and the client app are either release or dev builds. at bbsy.call(PG:17) at brhq.call(PG:2) at bwax.a(PG:1) at bvzz.run(PG:4) at bway.run(PG:1) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637) at java.lang.Thread.run(Thread.java:1012) 2025-06-16 19:36:24.449 627-1353 OpenGLRenderer system_server W Failed to initialize 101010-2 format, error = EGL_SUCCESS 2025-06-16 19:36:24.520 627-651 CoreBackPreview system_server D Window{f7c916b u0 Application Error: com.example.myapplication}: Setting back callback OnBackInvokedCallbackInfo{mCallback=android.window.WindowOnBackInvokedDispatcher$OnBackInvokedCallbackWrapper@d37c2e3, mPriority=0} 2025-06-16 19:36:24.520 627-651 Looper system_server W Slow dispatch took 109ms android.ui h=com.android.server.am.ActivityManagerService$UiHandler c=com.android.server.am.ErrorDialogController$$ExternalSyntheticLambda3@c19aa3f m=0 2025-06-16 19:36:24.585 9675-9779 tencentmap com.example.myapplication E Auth Fail: ******************************************************************************** 腾讯地图鉴权失败,请访问 lbs.qq.com 检查 key 配置 错误码:-202 错误信息:KEY_FORMAT_ERROR ********************************************************************************怎么解决
最新发布
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值