数组操作(非常规思维)

版权声明:个人做题总结专用~ https://blog.csdn.net/tb_youth/article/details/84638083

1687: 数组操作
Time Limit: 1 Sec Memory Limit: 128 MB
[Submit][Status][Web Board]
Description

给你一个初始的长度为n的数组。(1<=n<=105)

有两个操作:

Op1(l, r):给两个整数l和r(1<=l<=r<=当前数组长度)。你需要计算数组从l到r的所有元素的和。

Op2(x):给你一个整数x(|x| <= 109),你需要将x添加到数组的头部。原先的第一个元素变成第二个元素,第二个元素变成第三个,以此类推。并且数组的长度增加1.

Input

多组测试数据,处理到文件结尾。

每组测试数据,首先给出一个N(1 <= N <= 105), 表示初始的数组的元素个数。

第二行N个数字表示初始数组的n个元素,a1 a2 … aN.(|ai| <= 109)。

第三行有一个Q,表示有Q个操作。1 <= Q <= 105

接下来Q行,每行的第一个数表示操作的类型,只可能是1或者2,形式如下:

1 l r:操作1,求l到r的元素的和。

2 x:操作2,将x添加到数组头部。

Output

对于每组测试数据,首先输出"Case x:",表示当前是第x组测试数据。

然后对于每个操作1输出对应的答案,对于操作2不需要任何输出。

Sample Input

10
1 2 3 4 5 6 7 8 9 10
4
1 1 10
1 1 1
1 10 10
1 2 7
5
6 7 8 9 10
9
2 5
2 4
1 2 7
2 3
2 2
2 1
1 1 10
1 1 1
1 10 10

Sample Output

Case 1:
55
1
10
27
Case 2:
45
55
1
10

HINT

Source
AC代码~:

#include <stdio.h>
#include <string.h>
long long a[200005],b[200005];
int main()
{
    int n,c = 0;
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i = n; i > 0; i--)//数组indext倒过来存,加一个数时就方便了
        {
            scanf("%lld",&a[i]);
        }
        for(int j = 1; j <= n; j++)//最后一个数到倒数第j个数的和
        {
            b[j] = b[j-1] + a[j];
        }
        int Q,num,t;
        scanf("%d",&Q);
        printf("Case %d:\n",++c);
        while(Q--)
        {
            scanf("%d",&num);
            if(num==1)
            {
                int l,r;
                scanf("%d%d",&l,&r);
                printf("%lld\n",b[n-l+1]-b[n-r]);//草稿纸上推出这个公式
            }
            else
            {
                n++;
                scanf("%lld",&a[n]);
                b[n] = b[n-1] + a[n];
            }
        }
    }
    return 0;
}

(用动态数组+数状数组)时间超限代码:
//时间超限,树状数组中整合数组c更新慢(每次加一个数要更新,耗时啊!!)

#include <stdio.h>
#include <vector>
using namespace std;
vector<long long>a(200005),c(200005);
int n;
int lowbit(int x)
{
    return x&-x;
}
long long SUM(int y)
{
    long long sum = 0;
    for(int i = y; i > 0; i-=lowbit(i))
        sum += c[i];
    return sum;
}
void update(int y,int k)
{
    for(int i = y; i <= n; i+=lowbit(i))
        c[i] += k;
}
int main()
{
    int Q,C = 0;
    while(~scanf("%d",&n))
    {
        for(int i = 1; i<= n; i++)
        {
            scanf("%lld",&a[i]);
            update(i,a[i]);
        }
        scanf("%d",&Q);
        printf("Case %d:\n",++C);
        while(Q--)
        {
            int num,l,r,t;
            scanf("%d",&num);
            if(num==1)
            {
                scanf("%d%d",&l,&r);
                long long sum = SUM(r)-SUM(l-1);
                printf("%lld\n",sum);
            }
            else
            {
                scanf("%d",&t);
                a.insert(a.begin()+1,t);
                n++;
                fill(c.begin(),c.begin()+n+1,0);//动态数组赋值
                for(int i = 1; i<= n; i++)
                {
                    update(i,a[i]);
                }
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tb_youth/article/details/84638083
今日推荐