HDU4267 A Simple Problem with Integers

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4267

题意:题目中有两种操作

1.每隔k点更新;

2.查询最底层的叶子节点的值;

思路:因为k不是固定的,但是k的范围又很小,只有1-10,所以我们不妨在每个区域都开一个数组来标记不同的k,代表从最左端的那个点开始,在此区域内,每隔k个点都是有效点。要注意的是,每个区域的起始位置的那个点一定要是有效点。

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define Mid(x,y) (x+y)>>1
const int maxn=50010;
int n;
int arr[maxn];
struct node
{
	int left, right, val[11];
}p[4*maxn];

void Build(int l, int r, int rt)
{
	memset(p[rt].val, 0, sizeof(p[rt].val));
	p[rt].left=l;
	p[rt].right=r;
	if(r==l) return;
	int mid=Mid(l,r);
	Build(l, mid, L(rt));
	Build(mid+1, r, R(rt));
}

void Update(int l, int r, int k, int c,int rt)
{
	if(l>r) return;
	if(l==p[rt].left && r==p[rt].right)
	{
		p[rt].val[k]+=c;
		return;
	}
	int mid=Mid(p[rt].left, p[rt].right);
	if(r<=mid) Update(l,r,k,c,L(rt));
	else if(l>mid) Update(l,r,k,c,R(rt));
	else
	{
		Update(l,mid,k,c,L(rt));
		Update(k+l+((mid-l)/k)*k,r,k,c,R(rt));
	}
}

int find(int u)
{
	int rt=1;
	while(1)
	{
		if(p[rt].left==p[rt].right) return rt;
		int mid=Mid(p[rt].left, p[rt].right);
		if(u<=mid) rt=L(rt);
		else rt=R(rt);
	}
}

int Query(int u, int rt, int num)
{
	for(int i=1; i<11; i++)
	{
		if((u-p[rt].left)%i==0)
		{
			num+= p[rt].val[i];
		}
	}
	if(rt==1) return num;
	return Query(u, rt/2, num);
}

int main()
{
	while(~scanf("%d", &n))
	{
		for(int i=1; i<=n; i++)
		{
			scanf("%d", &arr[i]);
		}
		Build(1,n,1);
		int m=0,tmp=0;
		int a,b,k,c;
		scanf("%d", &m);
		while(m--)
		{
			scanf("%d", &tmp);
			if(tmp==1)
			{
				scanf("%d%d%d%d", &a, &b, &k, &c);
				Update(a,b,k,c,1);
			}
			if(tmp==2)
			{
				scanf("%d", &a);
				printf("%d\n", Query(a,find(a),arr[a]));
			}
		}
	}
	return 0;
}
时间: 2024-10-03 12:50:43

HDU4267 A Simple Problem with Integers的相关文章

HDU4267 A Simple Problem with Integers 线段树/树状数组

HDU4267 A Simple Problem with Integers  线段树/树状数组 2012长春网络赛A题 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operations. One type of operation is to add a given number to a few numbers in a given interval. T

E - A Simple Problem with Integers

#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #define N 100002 struct node { int l,r; long long lz,w; }q[4*N]; void pushup(int rt) { q[rt].w=q[rt*2].w+q[rt*2+1].w; } void pushdo

POJ 3468 A Simple Problem with Integers

链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77302 Accepted: 23788 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two ki

poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

poj3468 A Simple Problem with Integers 线段树区间更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ 3468 A Simple Problem with Integers(树状数组区间更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97217   Accepted: 30358 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ-3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 94899   Accepted: 29568 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

Poj 3468 A Simple Problem with Integers(线段树 区间更新 延迟标记)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 73239   Accepted: 22607 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4032    Accepted Submission(s): 1255 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with