提出 #76047452


ソースコード 拡げる

#include <iostream>
#include <map>
using namespace std;
const int MAX_N = 3e5 + 10;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, Q;
    cin >> N >> Q;
    // cnt[x]: 第x个细胞的实际块数 = 存储值 + base
    long long cnt[MAX_N] = {0};
    // base: 全局偏移量,用于模拟批量减1操作
    long long base = 0;
    // freq: 存储当前不同块数的细胞数量(使用存储值作为键)
    map<long long, int> freq;
    
    // 初始状态:所有细胞的存储值都是0,共N个
    freq[0] = N;
    
    while (Q--) {
        int type;
        cin >> type;
        
        if (type == 1) {
            int x;
            cin >> x;
            
            // 1. 更新频率哈希表:移除旧值的计数
            long long old_val = cnt[x];
            freq[old_val]--;
            if (freq[old_val] == 0) {
                freq.erase(old_val);
            }
            
            // 2. 增加目标细胞的块数
            cnt[x]++;
            
            // 3. 更新频率哈希表:添加新值的计数
            long long new_val = cnt[x];
            freq[new_val]++;
            
            // 4. 检查是否所有细胞都至少有1个块(实际值 = 存储值 + base >= 1)
            // 最小的实际值 = 最小存储值 + base >= 1
            if (!freq.empty() && freq.begin()->first + base >= 1) {
                // 执行全局减1操作:通过增加base来模拟
                base--;
                
                // 注意:此时所有细胞的实际值都减少了1,相当于存储值都增加了1
                // 但我们不需要修改cnt数组,只需要维护base即可
            }
        } else {
            int y;
            cin >> y;
            
            // 查询实际值 >= y的细胞数量,即存储值 >= y - base的细胞数量
            long long threshold = y - base;
            int ans = 0;
            
            // 使用upper_bound找到第一个大于等于threshold的键
            auto it = freq.lower_bound(threshold);
            while (it != freq.end()) {
                ans += it->second;
                ++it;
            }
            
            cout << ans << '\n';
        }
    }
    
    return 0;
}

提出情報

提出日時
問題 C - Drop Blocks
ユーザ Axiom_
言語 C++23 (GCC 15.2.0)
得点 300
コード長 2354 Byte
結果 AC
実行時間 60 ms
メモリ 6080 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 300 / 300
結果
AC × 1
AC × 50
セット名 テストケース
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, hand_14.txt, hand_15.txt, hand_16.txt, hand_17.txt, hand_18.txt, hand_19.txt, hand_20.txt, hand_21.txt, hand_22.txt, hand_23.txt, hand_24.txt, hand_25.txt, hand_26.txt, hand_27.txt, hand_28.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt
ケース名 結果 実行時間 メモリ
example_00.txt AC 2 ms 6000 KiB
hand_00.txt AC 23 ms 6080 KiB
hand_01.txt AC 23 ms 6076 KiB
hand_02.txt AC 25 ms 5992 KiB
hand_03.txt AC 24 ms 5916 KiB
hand_04.txt AC 24 ms 6044 KiB
hand_05.txt AC 27 ms 6004 KiB
hand_06.txt AC 21 ms 6044 KiB
hand_07.txt AC 20 ms 5992 KiB
hand_08.txt AC 21 ms 5944 KiB
hand_09.txt AC 22 ms 5980 KiB
hand_10.txt AC 21 ms 5960 KiB
hand_11.txt AC 25 ms 6020 KiB
hand_12.txt AC 24 ms 5916 KiB
hand_13.txt AC 23 ms 5960 KiB
hand_14.txt AC 25 ms 6024 KiB
hand_15.txt AC 27 ms 6024 KiB
hand_16.txt AC 2 ms 6004 KiB
hand_17.txt AC 23 ms 5960 KiB
hand_18.txt AC 23 ms 5960 KiB
hand_19.txt AC 23 ms 6064 KiB
hand_20.txt AC 20 ms 5908 KiB
hand_21.txt AC 27 ms 5848 KiB
hand_22.txt AC 24 ms 5980 KiB
hand_23.txt AC 23 ms 5936 KiB
hand_24.txt AC 24 ms 5916 KiB
hand_25.txt AC 2 ms 6020 KiB
hand_26.txt AC 2 ms 6044 KiB
hand_27.txt AC 4 ms 5980 KiB
hand_28.txt AC 24 ms 6080 KiB
random_00.txt AC 25 ms 6064 KiB
random_01.txt AC 60 ms 6020 KiB
random_02.txt AC 39 ms 5980 KiB
random_03.txt AC 30 ms 5916 KiB
random_04.txt AC 26 ms 6024 KiB
random_05.txt AC 22 ms 5908 KiB
random_06.txt AC 53 ms 5916 KiB
random_07.txt AC 28 ms 5980 KiB
random_08.txt AC 31 ms 6080 KiB
random_09.txt AC 29 ms 5916 KiB
random_10.txt AC 30 ms 6044 KiB
random_11.txt AC 50 ms 5944 KiB
random_12.txt AC 25 ms 5916 KiB
random_13.txt AC 34 ms 6064 KiB
random_14.txt AC 29 ms 6024 KiB
random_15.txt AC 22 ms 5976 KiB
random_16.txt AC 44 ms 5900 KiB
random_17.txt AC 29 ms 5960 KiB
random_18.txt AC 25 ms 5960 KiB
random_19.txt AC 26 ms 5876 KiB