fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int Mod=998244353;
  5.  
  6. void solve() {
  7. int n,k;
  8. cin >> n >> k;
  9. vector<int> a(n),b(n);
  10. bool ok =false;
  11. for(int i=0;i<n;i++) cin >> a[i];
  12. for(int i=0;i<n;i++){
  13. cin >> b[i];
  14. if(b[i]!=-1) ok=true;
  15. }
  16. if(!ok){
  17. int ma=*max_element(a.begin(),a.end());
  18. int mi=*min_element(a.begin(),a.end());
  19. int s=ma-mi;
  20. cout << k-s+1 << '\n';
  21. return;
  22. }
  23. vector<int> need;
  24. int target=-1;
  25. for(int i=0;i<n;i++){
  26. if(target!=-1 && b[i]!=-1 && a[i]+b[i]!=target){
  27. cout << "0\n";
  28. return;
  29. }
  30. if(b[i]!=-1) target=a[i]+b[i];
  31. }
  32. for(int i=0;i<n;i++){
  33. if(b[i]==-1){
  34. need.push_back(target-a[i]);
  35. }
  36. }
  37. sort(need.begin(),need.end());
  38. for(int i=0;i<need.size();i++){
  39. if(need[i]<0 || need[i]>k){
  40. cout << "0\n";
  41. return;
  42. }
  43. }
  44. cout << "1\n";
  45. }
  46.  
  47. int main(){
  48. ios::sync_with_stdio(false);
  49. cin.tie(nullptr);
  50.  
  51. int t;
  52. cin >> t;
  53. while (t--) solve();
  54.  
  55.  
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0.01s 5320KB
stdin
7
3 10
1 3 2
-1 -1 1
5 1
0 1 0 0 1
-1 0 1 0 -1
5 1
0 1 0 0 1
-1 1 -1 1 -1
5 10
1 3 2 5 4
-1 -1 -1 -1 -1
5 4
1 3 2 1 3
1 -1 -1 1 -1
5 4
1 3 2 1 3
2 -1 -1 2 0
5 5
5 0 5 4 3
5 -1 -1 -1 -1
stdout
1
0
0
7
0
1
0