段树Codechef TLE

时间:2013-12-27 20:03:45

标签: java algorithm data-structures segment-tree

我正在尝试解决this CodeChef problem

  

桌子上有N个硬币,编号从0到N-1。最初,每个硬币都保持垂直。
  您必须执行两种类型的操作:

     
      
  1. 翻转A和B之间编号的所有硬币。这由命令“0 A B”

  2. 表示   
  3. 回答在A和B之间编号的硬币数是多少。这由命令“1 A B”表示。

  4.         

    输入:第一行包含两个整数N和Q.如下所述,下一个Q行中的每一行都是“0 A B”或“1 A B”形式。

         

    输出:为“1 A B”形式的每个查询输出1行,其中包含相应查询的必需答案。

我使用的是段树。因此,每次用户输入类型1 A B的查询时,输出就是该区间[A,B]的总和。但是我收到超出时间限制的错误。我相信错误是由更新步骤0 A B引起的。更新数组中的元素后,我重新构建了树。代码如下。有人可以通过更快的方式帮助我更新吗?

BTW - 我得到了样本输入的所需输出。

public class SegmentTree
{
    private int[] tree;
    private int maxsize;
    private int height;
    private static int elems[];
    private  final int STARTINDEX = 0; 
    private  final int ENDINDEX;
    private  final int ROOT = 0;

    public SegmentTree(int size)
    {
        height = (int)(Math.ceil(Math.log(size) /  Math.log(2)));
        maxsize = 2 * (int) Math.pow(2, height) - 1;
        tree = new int[maxsize];
        ENDINDEX = size - 1; 
    }

    private int leftchild(int pos)
    {
        return 2 * pos + 1;
    }

    private int rightchild(int pos)
    {
        return 2 * pos + 2;
    }

    private int mid(int start, int end)
    {
        return (start + (end - start) / 2); 
    }

    private int getSumUtil(int startIndex, int endIndex, int queryStart, int queryEnd, int current)
    {
        if (queryStart <= startIndex && queryEnd >= endIndex)
        {
            return tree[current];
        }

        if (endIndex < queryStart || startIndex > queryEnd)
        {
            return 0;
        }

        int mid = mid(startIndex, endIndex);

        return  getSumUtil(startIndex, mid, queryStart, queryEnd, leftchild(current)) 
                 + getSumUtil( mid + 1, endIndex, queryStart, queryEnd, rightchild(current));
    }

    public int getSum(int queryStart, int queryEnd)
    {
        if(queryStart < 0 || queryEnd > tree.length)
        {
            return -1;
        }

        return getSumUtil(STARTINDEX, ENDINDEX, queryStart, queryEnd, ROOT);
    }

    private int constructSegmentTreeUtil(int startIndex, int endIndex, int current)
    {
        if (startIndex == endIndex)
        {
            tree[current] = elems[startIndex];
            return tree[current];   
        }

        int mid = mid(startIndex, endIndex);

        tree[current] = constructSegmentTreeUtil(startIndex, mid, leftchild(current))
                           + constructSegmentTreeUtil(mid + 1, endIndex, rightchild(current));

        return tree[current];
    }

    public void constructSegmentTree()
    {
        constructSegmentTreeUtil(STARTINDEX, ENDINDEX, ROOT);   
    }

    public static void main(String[]args) throws IOException
    {
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer str = new StringTokenizer(buf.readLine());
        int n = Integer.parseInt(str.nextToken());
        int q = Integer.parseInt(str.nextToken());
        SegmentTree segmentTree = new SegmentTree(n);
        int elements[] = new int[n];
        for(int i = 0; i < n; i++) {
            elements[i] = 0;
        }
        elems = elements;
        segmentTree.constructSegmentTree();
        while (q-- > 0) {
            str = new StringTokenizer(buf.readLine());
            int x = Integer.parseInt(str.nextToken());
            int a = Integer.parseInt(str.nextToken());
            int b = Integer.parseInt(str.nextToken());
            if(x == 0) {
                for(int j = a; j <= b; j++)
                {
                    elems[j] = elems[j]^1;
                }
                segmentTree.constructSegmentTree();
            }
            else {
                int num = segmentTree.getSum(a, b);
                System.out.println(num);
            }
        }
    }   
}

编辑:

According to GeeksForGeeks,树构造成本为O(n),更新方法为O(log n)。以下是更新的新方法:

private void updateTreeUtil(int startIndex, int endIndex, int updatePos, int update, int current)
{
    if ( updatePos < startIndex || updatePos > endIndex)
    {
        return;
    }

    tree[current] = tree[current] + update;

    if (startIndex != endIndex)
    {
        int mid = mid(startIndex, endIndex);
        updateTreeUtil(startIndex, mid, updatePos, update, leftchild(current));
        updateTreeUtil(mid+1, endIndex, updatePos, update, rightchild(current));
    }
}

public void update(int update, int updatePos)
{
    int updatediff = update - elems[updatePos];
    elems[updatePos] = update;
    updateTreeUtil(STARTINDEX, ENDINDEX, updatePos, updatediff, ROOT);
}

现在main方法中的if循环被修改为:

if(x == 0) {
    for(int j = a; j <= b; j++)
    {
        segmentTree.update(elems[j]^1, j);
    }
}

但仍然收到TLE错误。

1 个答案:

答案 0 :(得分:0)

在GeeksForGeeks的教程中,在更新单个元素的情况下,它们的更新运行时间为O(log n)。但是,在更新间隔时,必须使用Lazy Propagation来确保O(log n)更新时间,这基本上只是访问的更新节点,因此确保访问节点的总和是正确的。您可以搜索许多有关Lazy Propagation的好教程,例如:

http://se7so.blogspot.hk/2012/12/segment-trees-and-lazy-propagation.html

希望有所帮助。