fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. static const int MOD = 1000000007;
  6.  
  7. int main(){
  8. ios::sync_with_stdio(false);
  9. cin.tie(nullptr);
  10.  
  11. int n, q;
  12. cin >> n >> q;
  13.  
  14. // shareMod[p]: số cổ phiếu của p (lưu mod MOD)
  15. // cash_base[p]: số tiền p đang có (đã ghi nhận trước những sự kiện chưa rút) (mod MOD)
  16. // lastG[p]: giá trị G tại lần cập nhật cuối của p
  17. vector<int> shareMod(n+1, 0), cash_base(n+1, 0), lastG(n+1, 0);
  18.  
  19. int G = 0; // tích lũy "tiền cổ tức trên mỗi cổ phiếu" (mod MOD)
  20.  
  21. while(q--){
  22. int type;
  23. cin >> type;
  24. if(type == 1){
  25. int p;
  26. long long x;
  27. cin >> p >> x;
  28. // 1) Đồng bộ tài khoản p với G (áp dụng cổ tức đang đọng)
  29. int deltaG = (G - lastG[p] + MOD) % MOD;
  30. // pending = shareMod[p] * deltaG mod
  31. ll pend = ( (ll)shareMod[p] * deltaG ) % MOD;
  32. cash_base[p] = (cash_base[p] + (int)pend) % MOD;
  33.  
  34. // 2) Cập nhật số cổ phiếu: shareMod[p] += x (x có thể âm)
  35. int xmod = (int)((x % MOD + MOD) % MOD);
  36. shareMod[p] = (shareMod[p] + xmod) % MOD;
  37.  
  38. // 3) Gán lại lastG[p] = G
  39. lastG[p] = G;
  40. }
  41. else if(type == 2){
  42. int v;
  43. cin >> v;
  44. // Chỉ việc cộng vào G
  45. G = (G + v) % MOD;
  46. }
  47. else {
  48. // type == 3
  49. int p;
  50. cin >> p;
  51. // Tính tiền pending
  52. int deltaG = (G - lastG[p] + MOD) % MOD;
  53. ll pend = ( (ll)shareMod[p] * deltaG ) % MOD;
  54. int total = (int)( (cash_base[p] + pend) % MOD );
  55.  
  56. // In ra kết quả
  57. cout << total << "\n";
  58.  
  59. // Reset tài khoản p về 0
  60. cash_base[p] = 0;
  61. lastG[p] = G;
  62. }
  63. }
  64.  
  65. return 0;
  66. }
  67.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty