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

为什么没用vector,就随机访问数组也超时

Posted by JeremyCorbyn at 2018-04-07 19:04:09 on Problem 3321
#include<cstdio>
#define N 100005

int n, atree[N],bit[N];

struct folk
{
	int apple, start, end,offcnt,p[10];
}tree[N];

int lowbit(int k)
{
	return k&(-k);
}

void add(int k, int v)
{
	while (k <= n)
	{
		atree[k] += v;
		k += bit[k];
	}
}

int sum(int k)
{
	int s=0;
	while(k>0)
	{
		s += atree[k];
		k -= bit[k];
	}
	return s;
}

void dfs(int a,int &pos)
{
	int i;
	tree[a].start = pos;
	for (i = 0; i < tree[a].offcnt; i++)
		dfs(tree[a].p[i], ++pos);
	tree[a].end = pos;
}

int main()
{
	int i,m,t1,t2;
	char c;
	scanf_s("%d", &n);
	for(i=0;i<n-1;i++)
	{
		scanf_s("%d%d", &t1, &t2);
		tree[t1].p[tree[t1].offcnt++] = t2;
	}
	for (i = 1; i <= n; i++)
		bit[i] = lowbit(i);
	t1 = 1;
	dfs(1, t1);
	for (i = 1; i <= n; i++)
		add(tree[i].start, 1);
	scanf_s("%d", &m);
	for(i=0;i<m;i++)
	{
		getchar();
		scanf_s("%c%d", &c, 1, &t1);
		if (c == 'C')
		{
			add(tree[t1].start, tree[t1].apple-(tree[t1].apple^1));
			tree[t1].apple ^= 1;
		}
		else
			printf("%d\n", sum(tree[t1].end) - sum(tree[t1].start - 1));
	}
	return 0;
}

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