C - A Simple Problem with Integers - poj 3468(区间更新)
题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值。
- #include<stdio.h>
- #include<math.h>
- #include<string.h>
- #include<algorithm>
- using namespace std;
- #define maxn 100005
- #define Lson root<<1,L,tree[root].Mid()
- #define Rson root<<1|1,tree[root].Mid()+1,R
- struct Tree//op等于0的时候子树不需要更新,op等于1需要更新
- {
- int L, R, op;//需要向下更新的值为e
- long long sum, e;//因为区间和比较大,所以使用long long 保存
- int Mid(){return (L+R)/2;}
- int Len(){return (R-L+1);}
- }tree[maxn*4];
- long long val[maxn];
- void Down(int root)//向下更新
- {
- if(tree[root].op && tree[root].L != tree[root].R)
- {
- tree[root].op = false;
- tree[root<<1].op = tree[root<<1|1].op = true;
- tree[root<<1].e += tree[root].e;
- tree[root<<1|1].e += tree[root].e;
- tree[root<<1].sum += tree[root<<1].Len() * tree[root].e;
- tree[root<<1|1].sum += tree[root<<1|1].Len() * tree[root].e;
- tree[root].e = 0;
- }
- }
- void Build(int root, int L, int R)
- {
- tree[root].L = L, tree[root].R = R;
- tree[root].op = false;
- if(L == R)
- {
- tree[root].sum = val[L];
- return ;
- }
- Build(Lson);
- Build(Rson);
- tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
- }
- void Insert(int root, int L, int R, long long e)
- {
- Down(root);
- tree[root].sum += (R-L+1) * e;
- if(tree[root].L == L && tree[root].R == R)
- {
- tree[root].e = e, tree[root].op = true;
- return ;
- }
- if(R <= tree[root].Mid())
- Insert(root<<1, L, R, e);
- else if(L > tree[root].Mid())
- Insert(root<<1|1, L, R, e);
- else
- {
- Insert(Lson, e);
- Insert(Rson, e);
- }
- }
- long long Query(int root, int L, int R)
- {
- Down(root);
- if(tree[root].L == L && tree[root].R == R)
- return tree[root].sum;
- if(R <= tree[root].Mid())
- return Query(root<<1, L, R);
- else if(L > tree[root].Mid())
- return Query(root<<1|1, L, R);
- else
- return Query(Lson) + Query(Rson);
- }
- int main()
- {
- int i, N, Q;
- while(scanf("%d%d", &N, &Q) != EOF)
- {
- for(i=1; i<=N; i++)
- scanf("%I64d", &val[i]);
- Build(1, 1, N);
- int a, b; char s[10];
- long long c;
- while(Q--)
- {
- scanf("%s%d%d", s, &a, &b);
- if(s[0] == 'C')
- {
- scanf("%I64d", &c);
- Insert(1, a, b, c);
- }
- else
- {
- long long ans = Query(1, a, b);
- printf("%I64d\n", ans);
- }
- }
- }
- return 0;
- }
- /*
- 5 2
- 1 2 3 4 5
- C 1 5 5
- Q 1 1
- */
C - A Simple Problem with Integers - poj 3468(区间更新)的更多相关文章
- A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 69589 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- A Simple Problem with Integers~POJ - 3468
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- A Simple Problem with Integers POJ - 3468 (分块)
题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...
随机推荐
- POJ 1470 Closest Common Ancestors(LCA&RMQ)
题意比较费劲:输入看起来很麻烦.处理括号冒号的时候是用%1s就可以.还有就是注意它有根节点...Q次查询 在线st算法 /*************************************** ...
- 9.5noip模拟试题
题目名称 正确答案 序列问题 长途旅行 英文名称 answer sequence travel 输入文件名 answer.in sequence.in travel.in 输出文件名 answer.o ...
- codevs3008加工生产调度(Johnson算法)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- jsp页面可以巧用模态框
jsp页面使用模态框配合ajax出来的效果真的没话说,当然你也可以使用模态框配合action,但是在删除和更新的时候传值有点麻烦,用ajax 就没有这些问题 ,比如删除代码的时候在js文件中传值可以这 ...
- 关于异常的疑难解答:System.Runtime.InteropServices.COMException
COMException exception is thrown when an unrecognized HRESULT is returned from a COM method call.&qu ...
- Excel.Application手册
----转载:http://blog.csdn.net/xxfigo/article/details/6618129 定制模块行为(1) Option Explicit '强制对模块内所有变量进行声明 ...
- dbms_job dbms_scheduler简单比较
---------------------------陈旧的-------------------------------------/*--------------------- 创建job --- ...
- 关于NSURL的一些属性的记录
关于NSURL的一些属性的记录 NSLog(@"%@", request.URL.absoluteString); NSLog(@"%@", request.U ...
- Objective-C 类,函数调用
// // main.m // L02HelloObjC // // Created by JinXin on 15/11/25. // Copyright © 2015年 JinXin. All r ...
- Best Time to Buy and Sell sock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...