Advertisement
Guest User

rangetree1

a guest
Aug 11th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4.  
  5. struct nodo{
  6.     int s;
  7.     int e;
  8.     int value;
  9.     int aggiornato;
  10.     int inverti;
  11. };
  12.  
  13. void create(struct nodo tree[], int n, int index, int s, int e);
  14. void stampa(struct nodo tree[], int n);
  15. int query(struct nodo tree[], int n, int index, int a, int b);
  16. void change(struct nodo tree[], int n, int index, int a, int b);
  17.  
  18. main(){
  19.     FILE *pF = fopen("input.txt", "r");
  20.     int n,righe;
  21.     fscanf(pF, "%d %d", &n, &righe);
  22.    
  23.     int i = 0;
  24.     while(n > pow(2,i))
  25.         i++;
  26.        
  27.     while(n != pow(2,i))
  28.         n++;
  29.        
  30.     struct nodo tree[2*n];
  31.    
  32.     create(tree, 2*n,1,  n, 2*n-1);
  33.    
  34.    
  35.  
  36.     FILE *pF2 = fopen("output.txt", "w");
  37.     int op, a, b;
  38.     for(i=0; i<righe; i++){
  39.         fscanf(pF, "\n%d %d %d", &op, &a, &b);
  40.  
  41. //      stampa(tree, 2*n);
  42.        
  43.         if(op==1)
  44.             fprintf(pF2, "%d\n", query(tree, n*2, 1, a+n, b+n));
  45.         else
  46.             change(tree, n*2, 1, a+n, b+n);
  47.        
  48. //      stampa(tree, 2*n);
  49.     }
  50.     fclose(pF2);   
  51.     fclose(pF);
  52. }
  53.  
  54. void create(struct nodo tree[], int n, int index, int s, int e){
  55.    
  56.     tree[index].value = 0;
  57.     tree[index].inverti = 0;
  58.     tree[index].aggiornato = 1;
  59.     tree[index].s = s;
  60.     tree[index].e = e;
  61.    
  62.     if(index*2 < n){
  63.         create(tree, n, index*2, s, s+(e-s-1)/2);
  64.         create(tree, n, index*2+1, s+(e-s+1)/2, e);
  65.     }
  66. }
  67.  
  68. void change(struct nodo tree[], int n, int index, int a, int b){
  69.  
  70.     if(a == tree[index].s && b == tree[index].e){
  71.         tree[index].inverti = (tree[index].inverti+1)%2;
  72.         tree[index].aggiornato = 0;
  73.     }
  74.     else{
  75.        
  76.         tree[index].aggiornato = 0;
  77.        
  78.         if(a <= tree[index*2].e && b >= tree[index*2+1].s){
  79.            
  80.             change(tree, n, index*2, a, tree[index*2].e);
  81.             change(tree, n, index*2+1, tree[index*2+1].s, b);
  82.            
  83.         }
  84.         else{
  85.            
  86.             if(b <= tree[index*2].e)
  87.                 change(tree, n, index*2, a, b);
  88.            
  89.             if(a >= tree[index*2+1].s)
  90.                 change(tree, n, index*2+1, a, b);
  91.            
  92.         }
  93.     }
  94.    
  95. }
  96.  
  97.  
  98. int query(struct nodo tree[], int n, int index, int a, int b){
  99.  
  100.     if(a == tree[index].s && b == tree[index].e){
  101.         if(tree[index].aggiornato == 1){
  102.             return tree[index].value;
  103.         }
  104.         else{
  105.             if(tree[index].inverti){
  106.                 if(index*2 < n){
  107.                     tree[index].inverti = 0;
  108.                     tree[index*2].inverti = (tree[index*2].inverti+1)%2;
  109.                     tree[index*2+1].inverti = (tree[index*2+1].inverti+1)%2;
  110.                     tree[index*2].aggiornato = 0;
  111.                     tree[index*2+1].aggiornato = 0;
  112.                 }
  113.                 else{
  114.                     tree[index].inverti = 0;
  115.                     tree[index].value = (tree[index].value+1)%2;
  116.                     tree[index].aggiornato = 1;
  117.                     return tree[index].value;
  118.                 }
  119.             }
  120.             if(index*2 > n){
  121.                 tree[index].aggiornato = 1;
  122.                 return tree[index].value;
  123.             }
  124.            
  125.            
  126.             int c,d;
  127.             c = query(tree, n, index*2, a, a + (b-a-1)/2);
  128.             d = query(tree, n, index*2+1, a+(b-a+1)/2, b);
  129.            
  130.             tree[index].aggiornato = 1;
  131.             tree[index].value = c+d;
  132.             return tree[index].value;
  133.         }
  134.     }
  135.     else{
  136.            
  137.         if(a <= tree[index*2].e && b >= tree[index*2+1].s){
  138.             int c=0,d=0;
  139.            
  140.             c = query(tree, n, index*2, a, tree[index*2].e);
  141.             d = query(tree, n, index*2+1, tree[index*2+1].s, b);
  142.            
  143.             return c+d;
  144.         }
  145.         else{
  146.             int c=0,d=0;
  147.             if(b <= tree[index*2].e)
  148.                 c = query(tree, n, index*2, a, b);
  149.            
  150.             if(a >= tree[index*2+1].s)
  151.                 d = query(tree, n, index*2+1, a, b);
  152.            
  153.             return c+d;
  154.         }
  155.     }
  156. }
  157.  
  158. /*
  159. void stampa(struct nodo tree[], int n){
  160.     printf("\n\n");
  161.     int i;
  162.     int c = 1;
  163.     for(i=1; i<n; i++){
  164.         if(c == i){
  165.             printf("\n");
  166.             c *= 2;
  167.         }
  168.         printf("\nNodo %d, start = %d, end = %d, aggiornato = %d, inverti = %d, value = %d", i, tree[i].s, tree[i].e, tree[i].aggiornato, tree[i].inverti, tree[i].value);
  169.     }
  170. }
  171. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement