hdu 4267 A Simple Problem with Integers

题目链接:hdu 4267 A Simple Problem with Integers

类似于题目:hdu 1556 Color the ball 的技巧实现树状数组的段更新点查询。

由于该题对于段的更新并不是连续的,从而可以构造多个树状数组。因为$k \in [1,10] $,从而可以把更新划分为如下类型:

1,2,3,4,5...

-------------

1,3,5,7,9...

2,4,6,8,10...

-------------

1,4,7,10,13...

2,5,8,11,14...

3,6,9,12,15...

-------------

...

一共55棵树状数组。对于每一种更新,相应的更新该类型对应的树状数组。

在查询时,同样寻找符合该查询类型的树状数组并最终加上初始值即可完成。

代码如下:

 1 #include <cstdlib>
 2 #include  <cstdio>
 3 #include  <cstring>
 4 #include  <iostream>
 5 #define     MAXN 50005
 6 using namespace std;
 7 int bit[12][12][MAXN];
 8 int arr[MAXN];
 9 int N;
10 int lowbit(int x)
11 {
12     return x&(-x);
13 }
14 int query(int a, int b, int l)
15 {
16     int sum = 0;
17     while( l > 0 )
18     {
19         sum += bit[a][b][l];
20         l -= lowbit(l);
21     }
22     return sum;
23 }
24 void add(int a, int b, int l, int x)
25 {
26     while( l <= N )
27     {
28         bit[a][b][l] += x;
29         l += lowbit(l);
30     }
31 }
32 int main(int argc, char *argv[])
33 {
34     while(scanf("%d", &N)!= EOF)
35     {
36         memset(bit, 0, sizeof(bit));
37         for( int i = 1 ; i <= N ; i++ )
38         {
39             scanf("%d", &arr[i]);
40         }
41         int Q;
42         scanf("%d", &Q);
43         for( int i = 0 ; i < Q ; i++ )
44         {
45            int ty;
46            scanf("%d", &ty);
47            if( ty == 1 )
48            {
49                int a, b, k, c;
50                scanf("%d%d%d%d", &a, &b, &k, &c);
51                add(k, a%k, a, c);
52                add(k, a%k, b+1, -c);
53            }
54            if( ty == 2 )
55            {
56                int l;
57                scanf("%d", &l);
58                int ans = arr[l];
59                for( int i = 1 ; i <= 10 ; i++ )
60                {
61                    ans += query(i, l%i, l);
62                }
63                printf ( "%d\n", ans );
64            }
65         }
66     }
67     return 0;
68 }
时间: 2024-11-04 06:02:38

hdu 4267 A Simple Problem with Integers的相关文章

hdu 4267 A Simple Problem with Integers(树形结构-线段树)

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

HDU 4267 A Simple Problem with Integers(树状数组区间更新)

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

HDU 4267 A Simple Problem with Integers (树状数组)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 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

HDU 4267 A Simple Problem with Integers(树状数组)

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

【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状数组的单点查询:求某点a的值就是求数组中1~a的和. (i-a)%k==0把区间分隔开了,不能直接套用树状数组的区间修改单点查询 这道题的K很小,所以可以枚举k,对于每个k,建立k个树状数组,所以一共建立55棵树 所以就可以多建几棵树..然后就可以转换为成段更新了~~ [AC] 1 #include

HDU - 4267 A Simple Problem with Integers(树状数组的逆操作)

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. The other is to query the value of some element. Input There are a lot

HDU 3468 A Simple Problem with Integers

线段树区间更新求和 /* *********************************************** Author :Zhou Zhentao Email :[email protected] Created Time :2015/11/20 17:21:35 File Name :acm.cpp ************************************************ */ #include <stdio.h> #include <strin

hdu 4267/poj 3468 A Simple Problem with Integers (分状态的树状数组)

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

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