yosupo06 / library-checker-problems

The problem data (Test case generator, judge's solution, task, ...) of Library Checker
https://judge.yosupo.jp/
Apache License 2.0
527 stars 120 forks source link

[Problem proposal] Point add Point set Rectangle Affine (to points) Rectangle Sum (of points) #1279

Open robinyqc opened 1 day ago

robinyqc commented 1 day ago

Problem Name Proposal: The current problem name is too lengthy and lacks precision. :( We need a more concise and descriptive name that captures the essence of the problem.


Problem Description

You are given an initial set of $N$ weighted points, $P = (P_0, P1, \dots, P{N - 1})$, on a two-dimensional plane. Each point $P_i$ ($0 \leq i < N$) is located at $(x_i, y_i)$ and has an associated weight $w_i$. Process $Q$ queries of the following types. For the $i$-th query:


Constraints

For each query type:


Solution

Use K-D Tree to solve the problem in the complexity of $O(N^{1.5})$.

The solution involves maintaining a dynamic forest of 2-D Trees, where each tree handles a specific subset of points. Specifically:


Notes and Discussion Points

Many thanks to ChatGPT for the assistance with this issue.

maspypy commented 1 day ago

In my experience, rectangle query processing using a KD Tree can result in substantial execution time, even with static point sets. I can’t imagine how long it would take if we were to target dynamic point sets.

First, considering the high demand, I propose creating the next version as follows. This generalizes the problem at https://judge.yosupo.jp/problem/range_affine_range_sum directly to a 2D point set.

Given $N$ weighted points $(x_i, y_i, a_i)$, process $Q$ queries of the following two types:

- 0 l r d u b c : Set $a_i := b \times a_i + c$ for each $i$ such that $l \leq x_i < r$ and $d \leq y_i < u$
- 1 l r d u : Find the sum of $w_i$ for each $i$ such that $l \leq x_i < r$ and $d \leq y_i < u$
robinyqc commented 1 day ago

As you mentioned, K-D trees indeed come with substantial constants when performing rectangle queries, and I can relate to that. However, making the point set dynamic doesn’t necessarily lead to an exponential increase in the constant factor. Theoretically, the constant approximately doubles, and in practice, it’s roughly the same, as shown by the tests on yosupo:

https://judge.yosupo.jp/submission/248590 https://judge.yosupo.jp/submission/248584

So, I believe setting the time limit to 10 seconds is reasonable. That said, reducing $Q$ to $10^5$ might be a good idea, as $Q \leq 2 \times 10^5$ could be challenging.

On a personal note, optimizing the point set to be dynamic was a memorable experience, and I agree it would be valuable to include this operation in the library checker. I’ve encountered problems requiring dynamic insertion (even though they enforced an online requirement).

Aside from the rectangle operations and adding new points, the operation that updates the weight of a specific point is perhaps not essential. I only mentioned it because it could be achieved in $O(\log n)$, which may be worth noting for those writing templates.

P.s. My practical experience with the constants involved in K-D trees is limited; I have only tested it on a few problems, such as the library checker templates.

robinyqc commented 22 hours ago

I’ve run some preliminary performance tests on the K-D Tree implementation, with results available in max_queries_result.txt in the repository robinyqc/rectangle_add_rectangle_sum_of_points.

The tests compare the online and offline versions across different hardware setups (Intel i3, M2, and AMD EPYC processors). The results show that the offline version provides only a modest improvement over the online version, with no substantial performance gains observed.

I recommend using the contents of this repository as a basis for creating the problem. It contains almost all the work that needs to be done.

maspypy commented 21 hours ago

Thank you very much. I would like to add both versions of the problem.

robinyqc commented 7 hours ago

In fact, this dynamic point set version could also be solved in an offline manner, which (perhaps) could include the static point set version proposed by @maspypy . Anyway, I will proceed with generating the test data for the dynamic version.

I'm going to call it "Dynamic Point Rectangle Affine Rectangle Sum", with problem id dynamic_point_rectangle_affine_rectangle_sum.