Advertisement
Guest User

rangetree1

a guest
Aug 11th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.23 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 index, int s, int e);
  14. //void stampa(struct nodo tree[], int n);
  15. int query(struct nodo tree[], int index, int a, int b);
  16. void change(struct nodo tree[], 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.     struct nodo tree[2*n];
  30.     create(tree, 1, 0, n-1);
  31.    
  32.     FILE *pF2 = fopen("output.txt", "w");
  33.     int op, a, b;
  34.     for(i=0; i<righe; i++){
  35.         fscanf(pF, "\n%d %d %d", &op, &a, &b);
  36.  
  37. //      stampa(tree, 2*n);
  38.        
  39.        
  40.         if(op==1)
  41.             fprintf(pF2, "%d\n", query(tree, 1, a, b));
  42.         else
  43.             change(tree, 1, a, b);
  44.  
  45. //      stampa(tree, 2*n);
  46.     }
  47.     fclose(pF2);   
  48.     fclose(pF);
  49. }
  50.  
  51. void create(struct nodo tree[], int index, int s, int e){
  52.    
  53.     tree[index].value = 0;
  54.     tree[index].inverti = 0;
  55.     tree[index].aggiornato = 1;
  56.     tree[index].s = s;
  57.     tree[index].e = e;
  58.    
  59.     if(s != e){
  60.         create(tree, index*2, s, (s+e)/2);
  61.         create(tree, index*2+1, (s+e)/2+1, e);
  62.     }
  63. }
  64.  
  65. void change(struct nodo tree[], int index, int a, int b){
  66.  
  67.     if(tree[index].s > b || tree[index].e < a)
  68.         return;        
  69.  
  70.     if(tree[index].s >= a && tree[index].e <= b){
  71.         tree[index].inverti = (tree[index].inverti+1)%2;
  72.         tree[index].aggiornato = 0;
  73.         return; //non hai altro da fare in questo nodo
  74.     }
  75.    
  76.     tree[index].aggiornato = 0;
  77.    
  78.     change(tree, index*2, a, b);
  79.     change(tree, index*2+1, a, b);
  80.  
  81. }
  82.  
  83.  
  84. int query(struct nodo tree[], int index, int a, int b){
  85.    
  86.     if(tree[index].s > b || tree[index].e < a)
  87.         return 0;
  88.  
  89.     if(tree[index].s >= a && tree[index].e <= b){
  90.        
  91.         if(tree[index].aggiornato)
  92.             return tree[index].value;
  93.         else{          
  94.             if(tree[index].inverti){
  95.                 if(tree[index].s != tree[index].e){
  96.                     tree[index].inverti = 0;
  97.                     tree[index*2].inverti = (tree[index*2].inverti+1)%2;
  98.                     tree[index*2+1].inverti = (tree[index*2+1].inverti+1)%2;
  99.                     tree[index*2].aggiornato = 0;
  100.                     tree[index*2+1].aggiornato = 0;
  101.                    
  102.                     tree[index].value = query(tree, index*2, a, b) + query(tree, index*2+1, a, b);
  103.                     tree[index].aggiornato = 1;
  104.                     return tree[index].value;
  105.                 }
  106.                 else{
  107.                     tree[index].inverti = 0;
  108.                     tree[index].value = (tree[index].value+1)%2;
  109.                     tree[index].aggiornato = 1;
  110.                     return tree[index].value;
  111.                 }
  112.             }
  113.            
  114.         }
  115.     }
  116.    
  117.     if(tree[index].inverti){
  118.         tree[index].inverti = 0;
  119.         tree[index*2].inverti = (tree[index*2].inverti+1)%2;
  120.         tree[index*2+1].inverti = (tree[index*2+1].inverti+1)%2;
  121.         tree[index*2].aggiornato = 0;
  122.         tree[index*2+1].aggiornato = 0;
  123.     }
  124.    
  125.     tree[index].value = query(tree, index*2, a, b) + query(tree, index*2+1, a, b);
  126.     tree[index].aggiornato = 1;
  127.     return tree[index].value;
  128. }
  129.  
  130. /*
  131. void stampa(struct nodo tree[], int n){
  132.     printf("\n\n");
  133.     int i;
  134.     int c = 1;
  135.     for(i=1; i<n; i++){
  136.         if(c == i){
  137.             printf("\n");
  138.             c *= 2;
  139.         }
  140.         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);
  141.     }
  142. }
  143. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement