fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. vector<int> consistency(int n){
  33.  
  34. vector<int> aa;
  35. vector<int> bb;
  36.  
  37. for(int i=0; i<n; i++){
  38. if(a[i] == 1 || a[i] == 3) aa.push_back(i);
  39. if(a[i] == 2 || a[i] == 3) bb.push_back(i);
  40. }
  41.  
  42. int sz1 = aa.size();
  43. int sz2 = bb.size();
  44.  
  45. vector<int> pfA(sz1+1), pfB(sz2+1);
  46.  
  47. for(int i=1; i<=sz1; i++) pfA[i] = pfA[i-1] + aa[i-1];
  48. for(int i=1; i<=sz2; i++) pfB[i] = pfB[i-1] + bb[i-1];
  49.  
  50. int tA = 0;
  51. for(int i=0; i<sz1; i++) tA += aa[i];
  52.  
  53. int tB = 0;
  54. for(int i=0; i<sz2; i++) tB += bb[i];
  55.  
  56. vector<int> ans(n);
  57.  
  58. for(int i=0; i<n; i++){
  59. int lA = upper_bound(begin(aa), end(aa), i) - begin(aa) - 1 ;
  60. int lB = upper_bound(begin(bb), end(bb), i) - begin(bb) - 1 ;
  61.  
  62. int ctA = lA+1;
  63. int scoreA = (ctA*i - pfA[lA+1]) + ( (tA - pfA[lA+1]) - (sz1-ctA)*i );
  64.  
  65.  
  66. int ctB = lB+1;
  67. int scoreB = (ctB*i - pfB[lB+1]) + ( (tB - pfB[lB+1]) - (sz2-ctB)*i );
  68.  
  69. ans[i] = abs(scoreA - scoreB);
  70.  
  71. }
  72.  
  73.  
  74.  
  75.  
  76. return ans;
  77.  
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. vector<int> practice(int n){
  95.  
  96.  
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103. void solve() {
  104.  
  105. int n, m;
  106. cin>> n >> m;
  107.  
  108. a.resize(m);
  109. for(int i=0; i<m; i++) cin >> a[i];
  110.  
  111. auto ans = consistency(m);
  112. for(auto& t : ans) cout << t << " "; cout << endl;
  113.  
  114.  
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121. int32_t main() {
  122. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  123.  
  124. int t = 1;
  125. cin >> t;
  126. while (t--) {
  127. solve();
  128. }
  129.  
  130. return 0;
  131. }
Success #stdin #stdout 0s 5288KB
stdin
2
1 5
2 1 3 0 1
1 8
1 1 3 0 2 0 3 3
stdout
5 2 1 0 1 
3 2 1 4 7 8 9 10