# Untitled

unknown
plain_text
3 years ago
1.3 kB
30
Indexable
Never
```package SegMentTree;
import java.util.Scanner;
public class Main
{

public static void gsum(int a[], int tree[],int nut, int l  , int r)
{
if(l == r)
{
tree[nut] = a[l];
return;
}
int mid = (l+r)/2;
gsum(a,tree,nut*2+1,l,mid);
gsum(a,tree,nut*2+2,mid+1,r);
tree[nut] = tree[2*nut+1] + tree[2*nut + 2];
}

public static int gsum1( int tree[], int nut, int l, int r, int u, int v)
{
if(v < l || r < u)
{
return 0;
}
else if(u <= l && v <= r)
{
return tree[nut];
}
else
{
int mid = (l+r)/2;
return gsum1(tree,nut*2+1,l,mid,u,v) + gsum1(tree,nut*2+2,mid+1,r,u,v);
}
}

public static void main(String[] args)
{
Scanner n = new Scanner(System.in);

System.out.println("Nhap so phan tu:");
int t = n.nextInt();
int [] a = new int[100];
int[] tree = new int[100];

int i;
System.out.println("Nhap phan tu:");
for(i = 0;i<t;i++)
{
a[i] = n.nextInt();
}

int nut = 0;
gsum(a,tree,nut,0,t-1);

System.out.println("Nhap u , v:");
int u = n.nextInt();
int v = n.nextInt();

int sum = gsum1(tree,nut,0,t-1,u,v);
System.out.print(sum);
}
}```