A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 135904   Accepted: 42113
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

思路:线段树水题,建议手写线段树,熟悉模板,注意数据需要使用long long

》》点击进入原题测试《《

#include<string>
#include<iostream>
#include<algorithm> #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long using namespace std; const int maxn = 1e5 + ; struct Tree{
ll l, r, w, f;
}tree[maxn << ];
ll ans; inline void PushDown(int rt)
{
tree[rt << ].f += tree[rt].f;
tree[rt << | ].f += tree[rt].f;
tree[rt << ].w += tree[rt].f*(tree[rt << ].r - tree[rt << ].l + );
tree[rt << | ].w += tree[rt].f*(tree[rt << | ].r - tree[rt << | ].l + );
tree[rt].f = ;
}
void build(int l, int r, int rt)
{
tree[rt].l = l;
tree[rt].r = r;
tree[rt].f = ;
if (l == r){
cin >> tree[rt].w;
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
tree[rt].w = tree[rt << ].w + tree[rt << | ].w;
}
void update(int L, int R, int l, int r, int rt)
{
if (L <= l&& r <= R){
tree[rt].w += ans*(r - l + );
tree[rt].f += ans;
return;
}
if (tree[rt].f)PushDown(rt);
ll m = (l + r) >> ;
if (L <= m)update(L, R, lson);
if (R > m) update(L, R, rson);
tree[rt].w = tree[rt << ].w + tree[rt << | ].w;
}
ll query(ll L, ll R, ll l, ll r, ll rt)
{
if (L <= l&&r <= R){
return tree[rt].w;
}
if (tree[rt].f)PushDown(rt);
ll m = (l + r) >> , cnt = ;
if (L <= m)cnt += query(L, R, lson);
if (R > m)cnt += query(L, R, rson);
return cnt;
}
void Print(int l, int r, int rt)
{
if (l == r){
cout << rt << " = " << tree[rt].w << endl;
return;
}
//cout << rt << " = " << tree[rt].w << endl;
int m = (l + r) >> ;
if (l <= m)Print(lson);
if (r > m)Print(rson);
}
int main()
{
std::ios::sync_with_stdio(false); ll n, q;
cin >> n >> q; build(, n, );
string flag; ll a, b;
while (q--){
cin >> flag >> a >> b;
if (flag == "C"){
cin >> ans;;
update(a, b, , n, );
//Print(1, n, 1);
}
else if (flag == "Q"){
cout << query(a, b, , n, ) << endl;
}
} return ;
}

POJ 3468 A Simple Problem with Integers(线段树水题)的更多相关文章

  1. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  2. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  3. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  4. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  5. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  6. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  7. POJ 3468 A Simple Problem with Integers //线段树的成段更新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   ...

  8. poj 3468 A Simple Problem with Integers 线段树加延迟标记

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  9. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  10. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

随机推荐

  1. POJ2208 Pyramids 四面体体积

    POJ2208给定四面体六条棱(有序)的长度 求体积 显然用高中立体几何的方法就可以解决. 给出代码 #include<iostream> #include<cstdio> # ...

  2. 堆排序的C实现

    这几天有点抵触情绪,看过了快速排序还有一些别的东西,但是一点都不想写有点复杂的代码0 0拖到了今天终于写了前几天就应该自己写一下的堆排序,完全用C语言写的,下面把代码贴一下.很多地方写得并不好,不过已 ...

  3. Git简介(转载)

    转自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137396284551 ...

  4. MySQL 操作语句

    解释:|:或;{}:必选;[]:可选 创建数据库并指定字符编码: CREATE {DATABASE|SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTE ...

  5. bzoj 1704: [Usaco2007 Mar]Face The Right Way 自动转身机【贪心+差分】

    首先O(n^3)的贪心很好想,就是枚举k然后从前往后扫,扫到反就翻转区间 然后考虑优化掉翻转区间维,就是搞成差分的形式,在翻转区间的尾部打上标记,再用一个变量维护当前的翻转次数,加到当前状态上来判断是 ...

  6. uva 11292 The Dragon of Loowater(贪心)

    题目大意:   你的王国里有一条n个头的恶龙,你希望雇一些骑士把它杀死(即砍掉所有头).村里有m个骑士可以雇佣,一个能力值为x的骑士可以砍掉恶龙一个直径不超过x的头,且需要支付x个金币.如何雇佣骑士才 ...

  7. [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色,每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101),则 ...

  8. 区间DP UVA 1351 String Compression

    题目传送门 /* 题意:给一个字符串,连续相同的段落可以合并,gogogo->3(go),问最小表示的长度 区间DP:dp[i][j]表示[i,j]的区间最小表示长度,那么dp[i][j] = ...

  9. dubbo之日志适配及访问日志

    日志适配 自 2.2.1 开始,dubbo 开始内置 log4j.slf4j.jcl.jdk 这些日志框架的适配 1,也可以通过以下方式显示配置日志输出策略: 命令行 java -Ddubbo.app ...

  10. (转) 淘淘商城系列——使用SolrJ查询索引库

    http://blog.csdn.net/yerenyuan_pku/article/details/72908538 我们有必要在工程中写查询索引库的代码前先进行必要的测试.我们先到Solr服务页面 ...