QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#790438#9543. Good PartitionsJEdwardTL 13ms10712kbPython33.0kb2024-11-28 12:05:342024-11-28 12:05:34

Judging History

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

  • [2024-11-28 12:05:34]
  • 评测
  • 测评结果:TL
  • 用时:13ms
  • 内存:10712kb
  • [2024-11-28 12:05:34]
  • 提交

answer

import sys
import math

def precompute_divisors(n):
    divisors = [[] for _ in range(n+1)]
    for i in range(1, n):
        for j in range(i, n, i):
            divisors[j].append(i)
    return divisors

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    
    idx = 0
    T = int(data[idx])
    idx += 1
    for _ in range(T):
        n = int(data[idx])
        q = int(data[idx+1])
        idx += 2
        a = list(map(int, data[idx:idx+n]))
        idx += n
        modifications = []
        for _ in range(q):
            p = int(data[idx]) - 1  # 0-based index
            v = int(data[idx+1])
            modifications.append((p, v))
            idx += 2
        
        # Precompute divisors
        divisors = precompute_divisors(n)
        
        # Initial bad positions
        B = []
        for i in range(n-1):
            if a[i] > a[i+1]:
                B.append(i+1)  # 1-based index
        
        m = len(B)
        count_k = [0] * (n + 1)
        freq = [0] * (n + 1)
        
        for i in B:
            for d in divisors[i]:
                count_k[d] += 1
                if count_k[d] == 1:
                    freq[1] += 1
                else:
                    freq[count_k[d]] += 1
                    freq[count_k[d]-1] -= 1
        
        if m == 0:
            print(n)
        else:
            print(freq[m])
        
        for p, v in modifications:
            a[p] = v
            # Check p and p+1
            positions_to_check = []
            if p > 0:
                positions_to_check.append(p)
            if p < n-1:
                positions_to_check.append(p+1)
            
            added = []
            removed = []
            for pos in positions_to_check:
                one_b = a[pos-1] > a[pos]
                two_b = a[pos] > a[pos+1]
                if one_b:
                    if pos not in B:
                        B.append(pos)
                        added.append(pos)
                else:
                    if pos in B:
                        B.remove(pos)
                        removed.append(pos)
                if two_b:
                    if pos+1 not in B:
                        B.append(pos+1)
                        added.append(pos+1)
                else:
                    if pos+1 in B:
                        B.remove(pos+1)
                        removed.append(pos+1)
            
            for pos in added:
                for d in divisors[pos]:
                    freq[count_k[d]] -= 1
                    count_k[d] += 1
                    freq[count_k[d]] += 1
            for pos in removed:
                for d in divisors[pos]:
                    freq[count_k[d]] -= 1
                    count_k[d] -= 1
                    freq[count_k[d]] += 1
            
            m = len(B)
            if m == 0:
                print(n)
            else:
                print(freq[m])

if __name__ == "__main__":
    main()

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 10712kb

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: 13ms
memory: 10648kb

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: