BZOJ3212: Pku3468 A Simple Problem with Integers

3212: Pku3468 A Simple Problem with Integers

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 810  Solved: 354
[Submit][Status]

Description

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 first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

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

Sample Output

4
55
9
15

HINT

The sums may exceed the range of 32-bit integers.

Source

题解:

想了想树状数组如何维护区间修改,区间求和,觉得貌似很简单?

差分了之后 用树状数组维护 a[i] 以及  i*a[i] 即可

sum(1,n)=(n+1)*sigma(a[i])-sigma(i*a[i])

靠着树状数组刷进了第一页。。。

代码:

  1 #include<cstdio>
  2
  3 #include<cstdlib>
  4
  5 #include<cmath>
  6
  7 #include<cstring>
  8
  9 #include<algorithm>
 10
 11 #include<iostream>
 12
 13 #include<vector>
 14
 15 #include<map>
 16
 17 #include<set>
 18
 19 #include<queue>
 20
 21 #include<string>
 22
 23 #define inf 1000000000
 24
 25 #define maxn 100000+1000
 26
 27 #define maxm 500+100
 28
 29 #define eps 1e-10
 30
 31 #define ll long long
 32
 33 #define pa pair<int,int>
 34
 35 #define for0(i,n) for(int i=0;i<=(n);i++)
 36
 37 #define for1(i,n) for(int i=1;i<=(n);i++)
 38
 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 40
 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 42
 43 #define mod 1000000007
 44
 45 using namespace std;
 46
 47 inline ll read()
 48
 49 {
 50
 51     ll x=0,f=1;char ch=getchar();
 52
 53     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
 54
 55     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
 56
 57     return x*f;
 58
 59 }
 60 ll s[2][maxn],n,m;
 61 inline void add(int k,int x,ll y)
 62 {
 63     for(;x<=n;x+=x&(-x))s[k][x]+=y;
 64 }
 65 inline ll sum(int k,int x)
 66 {
 67     ll t=0;
 68     for(;x;x-=x&(-x))t+=s[k][x];
 69     return t;
 70 }
 71
 72 int main()
 73
 74 {
 75
 76     freopen("input.txt","r",stdin);
 77
 78     freopen("output.txt","w",stdout);
 79
 80     n=read();m=read();
 81     ll x=0,y,z;
 82     for(ll i=1;i<=n;i++)
 83     {
 84         y=read();
 85         add(0,i,y-x);
 86         add(1,i,i*(y-x));
 87         x=y;
 88     }
 89     while(m--)
 90     {
 91         char ch[2];
 92         scanf("%s",ch);
 93         if(ch[0]==‘C‘)
 94         {
 95             x=read();y=read();z=read();
 96             add(0,x,z);add(1,x,x*z);
 97             add(0,y+1,-z);add(1,y+1,(-z)*(y+1));
 98         }
 99         else
100         {
101             x=read();y=read();
102             ll sum1=(x)*sum(0,x-1)-sum(1,x-1);
103             ll sum2=(y+1)*sum(0,y)-sum(1,y);
104             printf("%lld\n",sum2-sum1);
105         }
106     }
107
108     return 0;
109
110 }

时间: 2024-11-05 21:40:35

BZOJ3212: Pku3468 A Simple Problem with Integers的相关文章

bzoj3212: Pku3468 A Simple Problem with Integers(线段树)

3212: Pku3468 A Simple Problem with Integers 题目:传送门 题解: 感谢Rose_max大佬的倾情相(推)助(水题) 一起打线段树啊~~~~ 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 type

BZOJ3212 Pku3468 A Simple Problem with Integers 题解

题目大意: 一个数列,有两个操作:1.修改操作,将一段区间内的数加上c:2.查询操作,查询一段区间内的数的和. 思路: 线段树裸题,区间修改.区间查询,维护和以及加上的数,由于无序,不需要向下推标记,只需在子树更新完之后更新根节点即可. 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 6 long long sum[400001],mor[4

3212: Pku3468 A Simple Problem with Integers

3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1053  Solved: 468[Submit][Status][Discuss] Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of opera

BZOJ 3212 Pku3468 A Simple Problem with Integers

题目大意:你拍一,我拍一,大家一起刷水题. CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 100010 #define LEFT (pos << 1) #define RIGHT (pos << 1|1) #define CNT (r - l + 1) using namespace std

poj3468 A Simple Problem with Integers 2011-12-20

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

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