fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define mod 1000000007
  4. #define pb push_back
  5. #define mp make_pair
  6. typedef long long ll;
  7. typedef pair<int,int> ii;
  8. void fastio(){
  9. ios_base::sync_with_stdio(false);
  10. cin.tie(NULL);
  11. }
  12. int checkindex(int i,int j,int array[]){
  13. if(i==-1){
  14. return j;
  15. }
  16. if(j==-1){
  17. return i;
  18. }
  19. if(array[i]>=array[j]){
  20. return i;
  21. }
  22. else{
  23. return j;
  24. }
  25. }
  26. int query1(int segtree[],int low,int high,int qlow,int qhigh,int pos,int array[]){
  27. if(qhigh<low || qlow>high)
  28. return -1;
  29. else if(low>=qlow && high<=qhigh)
  30. return segtree[pos];
  31. int mid=(low+high)/2;
  32. return checkindex(query1(segtree,low,mid,qlow,qhigh,2*pos+1,array),query1(segtree,mid+1,high,qlow,qhigh,2*pos+2,array),array);
  33. }
  34. void constructTree(int segtree[],int array[],int low,int high,int pos){
  35. if(low==high){
  36. segtree[pos]=low;
  37. return;
  38. }
  39. int mid=(low+high)/2;
  40. constructTree(segtree,array,low,mid,2*pos+1);
  41. constructTree(segtree,array,mid+1,high,2*pos+2);
  42. segtree[pos]=checkindex(segtree[2*pos+1],segtree[2*pos+2],array);
  43. return;
  44. }
  45. int main(){
  46. fastio();
  47. int t;
  48. cin>>t;
  49. for(int caseno=1;caseno<=t;caseno++){
  50. int n;
  51. cin>>n;
  52. int k;
  53. cin>>k;
  54. int c[n];
  55. int d[n];
  56. for(int i=0;i<n;i++){
  57. cin>>c[i];
  58. }
  59. for(int i=0;i<n;i++){
  60. cin>>d[i];
  61. }
  62. int size=2*pow(2,((int)ceil(log2(n))))-1;
  63. int segtree1[size];
  64. int segtree2[size];
  65. constructTree(segtree1,c,0,n-1,0);
  66. constructTree(segtree2,d,0,n-1,0);
  67. long long ans=0;
  68.  
  69. for(int i=0;i<n;i++){
  70. long long l1=i+1,l2=i+1,r1=i-1,r2=i-1;
  71. int sl=0,el=i;
  72. while(sl<=el){
  73.  
  74. int mid=(sl+el)/2;
  75. int index2=query1(segtree1,0,n-1,mid,i,0,c);
  76. int index1=query1(segtree2,0,n-1,mid,i,0,d);
  77.  
  78. if(index2==i && d[index1]<=c[i]+k){
  79. l1=mid;
  80. el=mid-1;
  81. }
  82. else{
  83. sl=mid+1;
  84. }
  85. }
  86.  
  87.  
  88. int sr=i,er=n-1;
  89. while(sr<=er){
  90. int mid=(sr+er)/2;
  91. int index2=query1(segtree1,0,n-1,i,mid,0,c);
  92. int index1=query1(segtree2,0,n-1,i,mid,0,d);
  93.  
  94. if(index2==i && d[index1]<=c[i]+k){
  95.  
  96. r1=mid;
  97. sr=mid+1;
  98. }
  99. else{
  100. er=mid-1;
  101. }
  102. }
  103. sl=0,el=i;
  104. while(sl<=el){
  105.  
  106. int mid=(sl+el)/2;
  107. int index2=query1(segtree1,0,n-1,mid,i,0,c);
  108. int index1=query1(segtree2,0,n-1,mid,i,0,d);
  109. if(index2==i && d[index1]<c[i]-k){
  110. l2=mid;
  111. el=mid-1;
  112. }
  113. else{
  114. sl=mid+1;
  115. }
  116. }
  117.  
  118.  
  119. sr=i,er=n-1;
  120. while(sr<=er){
  121. int mid=(sr+er)/2;
  122. int index2=query1(segtree1,0,n-1,i,mid,0,c);
  123. int index1=query1(segtree2,0,n-1,i,mid,0,d);
  124.  
  125. if(index2==i && d[index1]<c[i]-k){
  126.  
  127. r2=mid;
  128. sr=mid+1;
  129. }
  130. else{
  131. er=mid-1;
  132. }
  133. }
  134.  
  135.  
  136. ans=ans+(i-l1+1)*(r1-i+1)-(i-l2+1)*(r2-i+1);
  137. }
  138. cout<<"Case #"<<caseno<<": "<<ans<<endl;
  139.  
  140. }
  141.  
  142.  
  143. return 0;
  144. }
  145.  
  146.  
  147.  
Runtime error #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty