package kotlin.reflect.w.internal.z0;

/* JADX INFO: loaded from: classes2.dex */
public final class c<V> {
    public static final c<Object> a = new c<>();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final long f9138b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final V f9139c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final c<V> f9140d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final c<V> f9141e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final int f9142f;

    public c() {
        this.f9142f = 0;
        this.f9138b = 0L;
        this.f9139c = null;
        this.f9140d = null;
        this.f9141e = null;
    }

    public c(long j2, V v2, c<V> cVar, c<V> cVar2) {
        this.f9138b = j2;
        this.f9139c = v2;
        this.f9140d = cVar;
        this.f9141e = cVar2;
        this.f9142f = cVar.f9142f + 1 + cVar2.f9142f;
    }

    public V a(long j2) {
        c<V> cVar;
        if (this.f9142f == 0) {
            return null;
        }
        long j3 = this.f9138b;
        if (j2 < j3) {
            cVar = this.f9140d;
        } else {
            if (j2 <= j3) {
                return this.f9139c;
            }
            cVar = this.f9141e;
        }
        return cVar.a(j2 - j3);
    }

    public c<V> b(long j2, V v2) {
        if (this.f9142f == 0) {
            return new c<>(j2, v2, this, this);
        }
        long j3 = this.f9138b;
        return j2 < j3 ? c(this.f9140d.b(j2 - j3, v2), this.f9141e) : j2 > j3 ? c(this.f9140d, this.f9141e.b(j2 - j3, v2)) : v2 == this.f9139c ? this : new c<>(j2, v2, this.f9140d, this.f9141e);
    }

    public final c<V> c(c<V> cVar, c<V> cVar2) {
        if (cVar == this.f9140d && cVar2 == this.f9141e) {
            return this;
        }
        long j2 = this.f9138b;
        V v2 = this.f9139c;
        int i2 = cVar.f9142f;
        int i3 = cVar2.f9142f;
        if (i2 + i3 > 1) {
            if (i2 >= i3 * 5) {
                c<V> cVar3 = cVar.f9140d;
                c<V> cVar4 = cVar.f9141e;
                if (cVar4.f9142f < cVar3.f9142f * 2) {
                    long j3 = cVar.f9138b;
                    return new c<>(j3 + j2, cVar.f9139c, cVar3, new c(-j3, v2, cVar4.d(cVar4.f9138b + j3), cVar2));
                }
                c<V> cVar5 = cVar4.f9140d;
                c<V> cVar6 = cVar4.f9141e;
                long j4 = cVar4.f9138b;
                long j5 = cVar.f9138b + j4 + j2;
                V v3 = cVar4.f9139c;
                c cVar7 = new c(-j4, cVar.f9139c, cVar3, cVar5.d(cVar5.f9138b + j4));
                long j6 = cVar.f9138b;
                long j7 = cVar4.f9138b;
                return new c<>(j5, v3, cVar7, new c((-j6) - j7, v2, cVar6.d(cVar6.f9138b + j7 + j6), cVar2));
            }
            if (i3 >= i2 * 5) {
                c<V> cVar8 = cVar2.f9140d;
                c<V> cVar9 = cVar2.f9141e;
                if (cVar8.f9142f < cVar9.f9142f * 2) {
                    long j8 = cVar2.f9138b;
                    return new c<>(j8 + j2, cVar2.f9139c, new c(-j8, v2, cVar, cVar8.d(cVar8.f9138b + j8)), cVar9);
                }
                c<V> cVar10 = cVar8.f9140d;
                c<V> cVar11 = cVar8.f9141e;
                long j9 = cVar8.f9138b;
                long j10 = cVar2.f9138b;
                long j11 = j9 + j10 + j2;
                V v4 = cVar8.f9139c;
                c cVar12 = new c((-j10) - j9, v2, cVar, cVar10.d(cVar10.f9138b + j9 + j10));
                long j12 = cVar8.f9138b;
                return new c<>(j11, v4, cVar12, new c(-j12, cVar2.f9139c, cVar11.d(cVar11.f9138b + j12), cVar9));
            }
        }
        return new c<>(j2, v2, cVar, cVar2);
    }

    public final c<V> d(long j2) {
        return (this.f9142f == 0 || j2 == this.f9138b) ? this : new c<>(j2, this.f9139c, this.f9140d, this.f9141e);
    }
}
