📜  什么是竞争编程中基于在线和离线查询的问题

📅  最后修改于: 2022-05-13 01:57:50.002000             🧑  作者: Mango

什么是竞争编程中基于在线和离线查询的问题

竞技编程的查询题主要有两种:

  1. 离线查询。
  2. 在线查询。

离线查询

离线算法允许我们在打印任何答案之前操作要查询的数据。这通常仅在查询未在打印结果之前更新原始元素集时才有可能。

特征:

  1. 与在线查询相比,离线查询提供了更广泛的使用范围。
  2. 它们为自定义返回数据提供了更大的可用性和机会。
  3. 离线解决方案首先读取所有查询并对其进行预处理(例如对查询进行排序以使进一步处理也有效),然后以初始给定顺序计算并输出每个查询的答案。
  4. 在离线设置中,所有查询都会提前呈现。
  5. 问题在离线设置中更容易解决,因为可以重新排序查询或同时处理多个查询。
  6. 对查询和原始数据进行排序有助于提高 O(N + Q) 的效率,而不是 O(N * Q) 的效率,这在无法排序的情况下很常见,因为查询会像在线查询一样不断流入.

线段树是离线查询的一个例子。

例子:

离线查询的必要条件:

仅当一个查询的答案不依赖于先前查询的答案时才能使用此技术,因为在排序之后查询的顺序可能会发生变化。

何时使用离线查询:

  1. 大型报表应使用脱机查询。
  2. 当所有查询都是预先知道的。

在线查询

要按照查询的出现顺序回答查询,并且您不能预先操作查询以获得有效的方法,此类查询称为在线查询。

特征:

  1. 在此类查询中,我们的数据也可能会更新。
  2. 答案无法预先计算。
  3. 一个查询的答案也会影响进一步查询的答案。因此,每个查询都会被一一回答。
  4. 在线查询将在查询后不久提供响应,从而允许快速解决查询。

例子:

在此示例中,每次查询后,数组的数据都会发生变化。所以不能通过离线查询的方式来解决。

在线查询的必要条件:

仅当数据发生变化并且由于查询的连续流而无法进行排序时,才可以使用在线查询。

何时使用在线查询:

  1. 当需要在特定日期时间查询一些交易时,应使用在线查询。
  2. 当需要快速查询小查询时应该使用它们。

离线查询与在线查询

以下是离线查询和在线查询的一些区别:

S No.Offline QueryOnline Query
1This allows to manipulation of the data to be queried before any answer is printed.The queries cannot be pre-manipulated and answer the queries in order of their appearance.
2Offline queries are possible when the queries do not update the original element set before printing the result.In online queries, the data set may also get updated.
3Offline queries provide better efficiency of O(N + Q), since sorting of queries and original data is done before computing the result.In the case of online queries, the queries keep on streaming due to which sorting of queries is not possible and efficiency goes to O(N * Q).
4Offline queries should be used for large reports.Online queries should be used when it is required to query small queries quickly
5In offline queries, all queries are present in advance.In online queries, the queries keep streaming.
6In an offline query, the answer of one query does not depend on the answer of the previous query.In an online query, the answer of one query affects the answer of further queries.