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 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

首先,我们来设计一下顶层模型。看下图:

假设我们先插入了3段:

L1 R1 k1 c1

L2 R2 k2 c2

L3 R3 k3 c3

然后查询图中的i点,那么,我只要遍历所有插入的线段里包含i点的线段,然后判断(i-L)%k是否==0,若等于0,就将i点的初始值加上c。

实现:暴力实现肯定会超时,超时就在于找包含i点的线段上。我们可以用线段树来解决寻找包含i点的线段的问题。见下图:

像图中找点2时,其实线段树查询到2走过的每个线段都包含了2这个点。

那么,现在问题就转换为:只要在当前这个线段判断(i-l)%k==0,然后再+c就行了。又因为1<=k<=10,范围很小,所以每个线段都有一个数组sum[11]来记录插入该段的信息,比如当前这个线段内某点i满足(i-l)%k==0,就可以+c,那么sum[k] = c。

例如,我们插入:

a b k c

1 2 2 1

那么,[1,2]线段里的sum[2] = 1,假设我们现在查询1这个点,走到[1,2]这段时,因为(1-1)%2==0,所以最终答案就是初始值+1.

上面的例子是插入的线段往左儿子方向的情况,但是当要往右儿子方向的情况的时候会有一个问题,看下面的例子。

假设我们插入:

a b k c

2 4 2 1

我们发现一个问题,就是当[2,4]往右子树走时,区间变成了[3,4],但是[3,4]这个区间的左边界L=3,(L-a)%k = (3-2)%2 != 0,这样一来,我们查询点3的时候就会错,因为查询点3时,经过[3,4],(3-3)%2 == 0那么答案会+1,但是(3-2)%2 !=0。因此,我们插入的时候必须保证左端点(L-a)%k 一定等于0。所以,当往右边走的时候,要对区间稍作处理,一般我们会这样写:

update(mid+1 , r , 2*k+1);

现在是:

int tl = mid+1+(k-(mid+1)%k)%k;

update(tl , r , 2*k+1);

当然tl<=r,否则就不必往下插入了。

#include <iostream>
#include <cstdio>
using namespace std;

const int maxn = 50010;
struct tree{
    int l , r, k[12];
}a[4*maxn];
int num[maxn] , N , Q;

void build(int l , int r , int k){
    a[k].l = l;
    a[k].r = r;
    for(int i = 0; i < 12; i++) a[k].k[i] = 0;
    if(l != r){
        int mid = (l+r)/2;
        build(l , mid , 2*k);
        build(mid+1 , r , 2*k+1);
    }
}

void update(int l , int r , int k , int lk , int lc){
    if(l <= a[k].l && a[k].r <= r) a[k].k[lk] += lc;
    else{
        int mid = (a[k].l+a[k].r)/2;
        if(mid >= r) update(l , r, 2*k , lk , lc);
        else if(mid < l) update(l , r , 2*k+1 , lk , lc);
        else{
            update(l , mid , 2*k , lk , lc);
            int tl = mid+1+(lk-(mid+1-l)%lk)%lk;
            if(tl <= r) update(tl , r , 2*k+1 , lk , lc);
        }
    }
}

int getAns(int k , int i){
    int ans = 0;
    for(int j = 1; j <= 10; j++){
        if((i-a[k].l)%j == 0) ans += a[k].k[j];
    }
    return ans;
}

int query(int l , int r , int k){
    if(l <= a[k].l && a[k].r <= r) return getAns(k , l);
    else{
        int mid = (a[k].l+a[k].r)/2;
        int ans = getAns(k , l);
        if(mid >= r) return ans+query(l , r , 2*k);
        else return ans+query(l , r , 2*k+1);
    }
}

void computing(){
    for(int i = 1; i <= N; i++) scanf("%d" , &num[i]);
    build(1 , N , 1);
    scanf("%d" , &Q);
    int op , la, lb , lk , lc;
    while(Q--){
        scanf("%d" , &op);
        if(op == 1){
            scanf("%d%d%d%d" , &la , &lb , &lk , &lc);
            update(la , lb , 1 , lk , lc);
        }else{
            scanf("%d" , &la);
            printf("%d\n" , num[la]+query(la , la , 1));
        }
    }
}

int main(){
    while(~scanf("%d" , &N)){
        computing();
    }
    return 0;
}
时间: 2024-08-09 14:45:00

hdu 4267 A Simple Problem with Integers(树形结构-线段树)的相关文章

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,1

poj3468A Simple Problem with Integers区间和线段树

同上... #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set> #include

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

C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 1 #include<cstdio> 2 using namespace std; 3 const int maxn=1e5+6; 4 int n,a[maxn]; 5 struct Node{ 6 int l,r; 7 long long sum,lazy; 8 void update(long long x){//用于更新区间和 和懒标记 9 sum+=1ll*(r-l+1)*x; 10 lazy+=x; 11 } 12

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

思路:线段树,区间更新,区间查找 1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include<cmath> 5 #include<set> 6 #include<algorithm> 7 #include<cstdio> 8 #include<map> 9 #include<cstring> 10 #include<