Advertisement
apl-mhd

square root segmentation

Aug 10th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. struct node{
  7.    
  8.     //int start=0, end=0, sum=0;
  9.     int start, end, sum;
  10. } blockSum[100];
  11.  
  12.  
  13. void sumUp(int number[], int size, int blockSize){
  14.    
  15.    
  16.    
  17.    
  18.     int blockIndx=-1;
  19.    
  20.     for (int i=0; i<size; i++){
  21.         if((i % blockSize) == 0){
  22.             blockIndx++;
  23.            
  24.             blockSum[blockIndx].start=i;
  25.             blockSum[blockIndx].end=i+blockSize-1;
  26.            
  27.         }
  28.            
  29.         blockSum[blockIndx].sum+=number[i];
  30.        
  31.     }
  32.    
  33. }
  34.  
  35. int query(int qstart, int qend, int number[],int blockSize){
  36.    
  37.     int totalsum=0;
  38.    
  39.     for(int i=0; i<blockSize; i++){
  40.        
  41.         if(qstart<=blockSum[i].start  &&  blockSum[i].end<=qend ){
  42.            
  43.             totalsum+=blockSum[i].sum;
  44.             cout<<endl<<totalsum<<endl;
  45.            
  46.             cout<<'w';
  47.         }
  48.        
  49.        
  50.     else if(qstart<=blockSum[i].start && qend <=blockSum[i].end){
  51.        
  52.                 for(int j=blockSum[i].start; j<=qend; j++)
  53.                     totalsum+=number[j],cout<<'x';
  54.         }
  55.        
  56.     else if(qstart>=blockSum[i].start && blockSum[i].end <=qend){
  57.        
  58.                 for(int j=qstart; j<=blockSum[i].end; j++)
  59.                     totalsum+=number[j],cout<<'y';
  60.         }
  61.        
  62.        
  63.        
  64.     //else if(qstart<=blockSum[i].start && blockSum[i].end <=qend){
  65.        
  66.        
  67.         //}
  68.        
  69.     }
  70.    
  71.     return totalsum;
  72. }
  73.  
  74. int main(int argc, char **argv)
  75. {
  76.    
  77.    
  78.     int number[]={1,2,3,4,5,6,7,8,9};
  79.        
  80.     int size = sizeof(number) / sizeof(number[0]);
  81.    
  82.    
  83.     int blockSize = sqrt(size);
  84.  
  85.     cout<<blockSize<<endl;
  86.    
  87.     sumUp(number, size, blockSize);
  88.        
  89.     for(int i =0; i<blockSize; i++){
  90.         cout<<blockSum[i].sum<<" ";
  91.         cout<<blockSum[i].start<<" ";
  92.         cout<<blockSum[i].end<<endl<<" ";
  93.     }
  94.     cout<<endl<<query(1,4,number, blockSize);
  95.     cout<<endl<<query(0,5,number, blockSize);
  96.     //cout<<endl<<query(3,5,number, blockSize);
  97.     //cout<<endl<<query(0,5,number, blockSize);
  98.     //cout<<endl<<query(3,8,number, blockSize);
  99.     //cout<<endl<<query(0,8,number, blockSize);
  100.     //cout<<endl<<query(0,4,number, blockSize);
  101.     //cout<<endl<<query(0,5,number, blockSize);
  102.     //cout<<endl<<query(2,5,number, blockSize);
  103.     //cout<<endl<<query(6,8,number, blockSize);
  104.    
  105.    
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement