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 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 of test cases.
The first line contains an integer N. (1 <= N <= 50000)
The
second line contains N numbers which are the initial values of A1, A2,
... , AN. (-10,000,000 <= the initial value of Ai <= 10,000,000)
The third line contains an integer Q. (1 <= Q <= 50000)
Each of the following Q lines represents an operation.
"1
a b k c" means adding c to each of Ai which satisfies a <= i <= b
and (i - a) % k == 0. (1 <= a <= b <= N, 1 <= k <= 10,
-1,000 <= c <= 1,000)
"2 a" means querying the value of Aa. (1 <= a <= N)

Output

For each test case, output several lines to answer all query operations.

Sample Input

4
1 1 1 1
14
2 1
2 2
2 3
2 4
1 2 3 1 2
2 1
2 2
2 3
2 4
1 1 4 2 1
2 1
2 2
2 3
2 4

Sample Output

1
1
1
1
1
3
3
1
2
3
4
1

(i-a)%k==0   即  i%k==a%k             分组   x%k==a%k的为一组,   参数 mod, k,x

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <map>
 8 #include <string>
 9 #include <vector>
10 #include <queue>
11 #include <stack>
12 #include <iomanip>
13 #include <sstream>
14 using namespace std;
15 //#define local
16 typedef long long LL;
17 const int INF=0x4fffffff;
18 const int EXP=1e-5;
19 const int MS=50005;
20
21 int C[11][11][MS];      //C[mod][k][x]
22 int num[MS];
23
24 int lowbit(int x)
25 {
26       return x&(-x);
27 }
28
29 //   修改区间,单点求职,   树状数组需要逆过来。
30
31 void updata(int mod,int k,int x,int d)
32 {
33       while(x>0)
34       {
35             C[mod][k][x]+=d;
36             x-=lowbit(x);
37       }
38 }
39
40 int getsum(int a,int x)
41 {
42       int res=0;
43       while(x<MS)     //x<=n
44       {
45             for(int k=1;k<=10;k++)
46             {
47                   res+=C[a%k][k][x];
48             }
49             x+=lowbit(x);
50       }
51       return res;
52 }
53
54 int main()
55 {
56       #ifdef local
57       freopen("in.txt","r",stdin);
58       freopen("out.txt","w",stdout);
59       #endif // local
60       int n;
61       while(scanf("%d",&n)!=EOF)
62       {
63             for(int i=1;i<=n;i++)
64                   scanf("%d",&num[i]);
65             memset(C,0,sizeof(C));
66             int m;
67             scanf("%d",&m);
68             while(m--)
69             {
70                   int op,a,b,k,c;
71                   scanf("%d",&op);
72                   if(op==1)
73                   {
74                         scanf("%d%d%d%d",&a,&b,&k,&c);
75                         updata(a%k,k,b,c);
76                         updata(a%k,k,a-1,-c);
77                   }
78                   else
79                   {
80                         scanf("%d",&a);
81                         int ans=getsum(a,a);
82                         printf("%d\n",ans+num[a]);
83                   }
84             }
85       }
86       return 0;
87 }
时间: 2024-11-03 20:46:49

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

POJ 3468 A Simple Problem with Integers 【树状数组】

题目链接:http://poj.org/problem?id=3468 题目大意:给出一组数组v[i],有两种操作,一种给出两个数a,b,要求输出v[a]到v[b]之间的和,另一种给出三个数a,b,c,让v[a]到v[b]之间的数全都加上c. 完全是树状数组能够实现的功能,但是如果就这样单纯的套用模板,做第二种操作是更新每个值,这样的操作就有可能超时. 换一种思路,既然第二种操作是给某区间上的所有数加上相同的值,那么应该是能够简化的才对. 假设数组sum[i]为原数组从v[1]到v[i]的和,数

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

Luogu P3368 【模板】树状数组 2 [区间修改-单点查询]

P3368 [模板]树状数组 2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. 第二行包含N个用空格分隔的整数,其中第i个数字表示数列第i项的初始值. 接下来M行每行包含2或4个整数,表示一个操作,具体如下: 操作1: 格式:1 x y k 含义:将区间[x,y]内每个数加上k 操作2: 格式:2 x 含义:输出第x个数的值 输出格式: 输出

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

poj3468 A Simple Problem with Integers (树状数组做法)

题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 142198   Accepted: 44136 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One

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

poj 3486 A Simple Problem with Integers(树状数组第三种模板改段求段)

1 /* 2 树状数组第三种模板(改段求段)不解释! 不明白的点这里:here! 3 */ 4 #include<iostream> 5 #include<cstring> 6 #include<cstdio> 7 #include<algorithm> 8 #define N 100005 9 using namespace std; 10 11 typedef long long LL; 12 13 LL ss[N], B[N], C[N]; 14 15

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

A Simple Problem with Integers(树状数组区间变化和区间求和)

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. Input The firs