Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

请教大牛——我的线段树的复杂度不是O(LogN)嘛?谢谢!

Posted by zerocpp at 2010-08-25 12:05:46 on Problem 3468
问题:
下面的代码是我花了一上午的时间打出来的(好吧,欢迎鄙视本菜^_^),不明白为什么会超时,我觉得我的add和query函数都是O(logN)的复杂度呀(估计不是,否则不会超时了……),请各位大牛看看为什么会超时(帮忙分析一下时间复杂度),谢谢。
代码:
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>

using namespace std;
#define M 100005
struct A
{
	int lt, rt;
	long long sum;
}tree[M * 5];
int len;
void maketree(int = 1, int = len, int = 1);
void insert(int , int , int = 1);
long long query(int, int, int = 1);
void add(int, int, long long, int = 1);
void swap(int &, int &);
void print();
int main()
{
	int qs, t;
	// memset(tree, 0, sizeof tree);
	scanf("%d %d", &len, &qs);
	maketree();
	//print();
	for (int i = 1; i <= len; ++ i)
	{
		scanf("%d", &t);
		insert(t, i);
	}
	//print();
	char str[2];
	int a, b;
	long long c;
	for (int i = 0; i < qs; ++ i)
	{
		scanf("%s", str);
		if (str[0] == 'Q')
		{
			scanf("%d%d", &a, &b);
			if (a > b) swap(a, b);
			printf("%lld\n", query(a, b));
		}
		else
		{
			scanf("%d%d%lld", &a, &b, &c);
			if (a > b) swap(a, b);
			add(a, b, c);
			//print();
		}
	}
	//system("pause");
	return 0;
}
// 调试用 
void print()
{
	printf("----打印开始----\n");
	printf("下标\t左端点\t右端点\t和值\n");
	for (int i = 0; i < 30; ++ i)
	{
		printf("%d\t%d\t%d\t%d\n", i, tree[i].lt, tree[i].rt, tree[i].sum);
	} 
	printf("----打印结束----\n");
}
void maketree(int lt, int rt, int pos)
{
	tree[pos].sum = 0;
	tree[pos].lt = lt;
	tree[pos].rt = rt;
	if (lt == rt) return;
	int mid = (lt + rt) / 2;
	maketree(lt, mid, pos << 1);
	maketree(mid + 1, rt, (pos << 1) + 1);
}
void insert(int value, int id, int pos)
{
	tree[pos].sum += value;
	if (tree[pos].lt == tree[pos].rt) return;
	int mid = (tree[pos].lt + tree[pos].rt) / 2;
	if (id <= mid)
		insert(value, id, pos << 1);
	else
		insert(value, id, (pos << 1) + 1);
}
long long query(int lt, int rt, int pos)
{
	if (lt == tree[pos].lt && rt == tree[pos].rt)
		return tree[pos].sum;
	int mid = (tree[pos].lt + tree[pos].rt) / 2;
	if (rt <= mid)
		return query(lt, rt, pos << 1);
	else if (lt > mid)
		return query(lt, rt, (pos << 1) + 1);
	else
		return query(lt, mid, pos << 1) + query(mid + 1, rt, (pos << 1) + 1);
}
void add(int lt, int rt, long long value, int pos)
{
	tree[pos].sum += value * (rt - lt + 1);
	if (tree[pos].lt == tree[pos].rt)
		return;
	int mid = (tree[pos].lt + tree[pos].rt) / 2;
	if (rt <= mid)
		add(lt, rt, value, pos << 1);
	else if (lt > mid)
		add(lt, rt, value, (pos << 1) + 1);
	else
	{
		add(lt, mid, value, pos << 1);
		add(mid + 1, rt, value, (pos << 1) + 1);
	}
}
void swap(int & a, int & b)
{
	int t = a;
	a = b;
	b = t;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator