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
494 stars 117 forks source link

[Problem proposal] (Range Add Range Sum) #1137

Closed guoyiz23 closed 3 months ago

guoyiz23 commented 3 months ago

Problem name: Range Add Range Sum

Remarks

I found what appears to actually, as surprising as it may be, be a bug in the AtCoder lazysegtree library, for which I filed https://github.com/atcoder/ac-library/issues/175. The sample cases below correspond to the ones mentioned in this issue. It was https://atcoder.jp/contests/abc351/tasks/abc351_e that brought to this. I suppose one can use the test cases of this AtCoder problem to test.

Problem

You are given an integer sequence $a_0,a1,\ldots,a{N-1}$ with length $N$. Process the following $Q$ queries in order:

Constraint

Sample cases

Input 1

3 12
2 5 5
0 0 3 -2
1 0 0
1 1 3
0 0 3 2
0 0 3 -5
1 0 1
1 2 3
0 0 3 5
0 0 3 -5
1 0 2
1 3 3
0 0 3 5

Output 1

0
6
-3
0
-3
0

Input 2

3 12
3 3 4
0 0 3 -3
1 0 0
1 1 3
0 0 3 3
0 0 3 -3
1 0 1
1 2 3
0 0 3 3
0 0 3 -4
1 0 2
1 3 3
0 0 3 4

Output 2

0
1
0
1
-2
0

Concluding remarks

Considering how the AtCoder segment tree library is entirely generic, perhaps we can comprehensively test it across a large number and variety of segment tree problems. I was very surprised that AtCoder lazysegtree did not work for that problem. I initially thought that I was misusing it, but then, when I used another lazy segment tree library, it did work: https://atcoder.jp/contests/abc351/submissions/52896462.

template <class S,
          S (*op)(S, S),
          S (*e)(),
          class F,
          S (*mapping)(F, S),
          F (*composition)(F, F),
          F (*id)()>
struct lazy_segtree {
maspypy commented 3 months ago

On your implementation

I think Your implementation in https://github.com/atcoder/ac-library/issues/175 is just wrong, and it is not a bug of AtCoder Library.

As writtern in the document of lazy segtree of ACL: https://github.com/atcoder/ac-library/blob/master/document_en/lazysegtree.md $f(x\cdot y)=f(x)\cdot f(y)$ is needed. $f\colon x\mapsto x+c$ doesn't satisfy the condition.

I think the correct implementation looks like:

struct T {
  lint count, sum;
};

T op(T a, T b) { return {a.count + b.count, a.sum + b.sum}; }
T e() { return {0, 0}; }

T mapping(lint f, T x) { return {x.count, x.sum + f * x.count}; }
lint composition(lint f, lint g) { return f + g; }

We need to add $f$ to some aggregated data of a interval. If we keep only the sum, we can't find the resulted data. By keeping both count and sum, we can get the result.

maspypy commented 3 months ago

On the problem proposal

We can verify the lazysegtree implementation by the following problem. https://judge.yosupo.jp/problem/range_affine_range_sum

Furthermore, range add operation is a special case of range affine operation ($b=1$). So I think we don't need a new problem. However, we could consider adding it if there are new algorithms that need verification and cannot be tested in the existing problem.

If some hack cases are required, it should be added to range_affine_range_sum.

guoyiz23 commented 3 months ago

ごめんなさい、悪いです。 どうもありがとう。

I got AC now. As for the machine translate generated above, I do not actually know Japanese other than the meaning of the Kanji, but AtCoder did inspire me to more seriously learn it. xD

私は実際に日本人とあまり話したことはありませんが、そのうち話すようになるかもしれません。

wangyingkuan commented 1 month ago

@guoyiz23 can you delete my commit in https://github.com/telegramdesktop/tdesktop/issues/26626#issuecomment-1807434346,a lot guys add my qq account ,is my bad.