QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#770572#9543. Good PartitionsJEdwardTL 15ms10848kbPython32.5kb2024-11-21 22:26:042024-11-21 22:26:04

Judging History

你现在查看的是最新测评结果

  • [2024-11-21 22:26:04]
  • 评测
  • 测评结果:TL
  • 用时:15ms
  • 内存:10848kb
  • [2024-11-21 22:26:04]
  • 提交

answer

import sys
import math

def get_factors(x):
    factors = {}
    while x % 2 == 0:
        factors[2] = factors.get(2, 0) + 1
        x //= 2
    i = 3
    while i * i <= x:
        while x % i == 0:
            factors[i] = factors.get(i, 0) + 1
            x //= i
        i += 2
    if x > 1:
        factors[x] = factors.get(x, 0) + 1
    return factors

def count_divisors(x):
    if x == 0:
        return 0
    factors = get_factors(x)
    count = 1
    for exp in factors.values():
        count *= (exp + 1)
    return count

class SegmentTree:
    def __init__(self, size):
        self.N = 1
        while self.N < size:
            self.N <<= 1
        self.tree = [0] * (2 * self.N)
    
    def update(self, idx, value):
        idx += self.N - 1
        self.tree[idx] = value
        idx //= 2
        while idx >= 1:
            self.tree[idx] = math.gcd(self.tree[2*idx], self.tree[2*idx+1])
            idx //= 2
    
    def remove(self, idx):
        idx += self.N - 1
        self.tree[idx] = 0
        idx //= 2
        while idx >= 1:
            self.tree[idx] = math.gcd(self.tree[2*idx], self.tree[2*idx+1])
            idx //= 2
    
    def get_gcd(self):
        return self.tree[1] if self.N >= 1 else 0

def main():
    input = sys.stdin.read().split()
    ptr = 0
    T = int(input[ptr])
    ptr += 1
    for _ in range(T):
        n, q = int(input[ptr]), int(input[ptr+1])
        ptr += 2
        a = list(map(int, input[ptr:ptr+n]))
        ptr += n
        bad = set()
        for i in range(n-1):
            if a[i] > a[i+1]:
                bad.add(i+1)
        st = SegmentTree(n)
        for i in bad:
            st.update(i, i)
        g = st.get_gcd()
        if not bad:
            print(n)
        else:
            print(count_divisors(g))
        for __ in range(q):
            p, v = int(input[ptr]), int(input[ptr+1])
            ptr += 2
            a[p-1] = v
            for i in [p-1, p]:
                if 1 <= i <= n-1:
                    if a[i-1] > a[i]:
                        bad.add(i)
                    else:
                        if i in bad:
                            bad.remove(i)
            st = SegmentTree(n)
            for i in bad:
                st.update(i, i)
            g = st.get_gcd()
            if not bad:
                print(n)
            else:
                print(count_divisors(g))

if __name__ == "__main__":
    main()

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 15ms
memory: 10848kb

input:

1
5 2
4 3 2 6 1
2 5
3 5

output:

1
2
3

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 15ms
memory: 10788kb

input:

1
1 1
2000000000
1 1999999999

output:

1
1

result:

ok 2 lines

Test #3:

score: -100
Time Limit Exceeded

input:

1
200000 200000
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 ...

output:

160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160
200000
160...

result: