Advertisement
Dmaxiya

商品库存管理 参考代码

Mar 15th, 2025
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 300000 + 100;
  6. int n, m, cnt0, tmp;
  7. int l[maxn], r[maxn], num[maxn], cnt1[maxn];
  8.  
  9. int main() {
  10. #ifdef ExRoc
  11.     freopen("test.txt", "r", stdin);
  12. #endif
  13.     ios::sync_with_stdio(false);
  14.  
  15.     cin >> n >> m;
  16.     for (int i = 0; i < m; ++i) {
  17.         cin >> l[i] >> r[i];
  18.         ++num[l[i]];
  19.         --num[r[i] + 1];
  20.     }
  21.     for (int i = 1; i <= n; ++i) {
  22.         tmp += num[i];
  23.         cnt1[i] = cnt1[i - 1];
  24.         if (tmp == 0) {
  25.             ++cnt0;
  26.         } else if (tmp == 1) {
  27.             ++cnt1[i];
  28.         }
  29.     }
  30.     for (int i = 0; i < m; ++i) {
  31.         cout << cnt0 + cnt1[r[i]] - cnt1[l[i] - 1] << endl;
  32.     }
  33.  
  34.     return 0;
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement