关键词:
题目链接:http://poj.org/problem?id=3468
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 83959 | Accepted: 25989 | |
Case Time Limit: 2000MS |
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
Source
对于更新树是为了避免改动到最底下而导致超时问题。所以每次改动仅仅改动相相应的区间就可以。然后记录一个add。下次更新或者查询的时候,假设查到该节点,就把add直接加到子节点上去,在将add变为0,避免下次还会反复加。这样仅仅更新到查询的子区间,不须要再往下找了,所以时间复杂度为O(n),更新树和查询树都须要这样。
由于add不为0,该add从根一直加到了该节点,之前的都加过了,假设更新到时候不加到子节点。还要通过子节点更新当前节点,当前节点的sum值里面含有的add就会被“抹掉”,就不能保证正确性了。还须要注意的就是要用__int64。
#include <iostream> #include <cstdio> using namespace std; #define LL __int64 struct node { int l,r; LL sum; LL add; //int flag;//用来表示有几个加数 } s[100000*4]; void InitTree(int l,int r,int k) { s[k].l=l; s[k].r=r; s[k].sum=0; s[k].add=0; if (l==r) return ; int mid=(l+r)/2; InitTree(l,mid,2*k); InitTree(mid+1,r,2*k+1); } void UpdataTree(int l,int r,LL add,int k) { if (s[k].l==l&&s[k].r==r) { s[k].add+=add; s[k].sum+=add*(r-l+1); return ; } if (s[k].add!=0)//加数为0就不须要改变了 { s[2*k].add+=s[k].add; s[2*k+1].add+=s[k].add; s[2*k].sum+=s[k].add*(s[2*k].r-s[2*k].l+1); s[2*k+1].sum+=s[k].add*(s[2*k+1].r-s[2*k+1].l+1); s[k].add=0; } int mid=(s[k].l+s[k].r)/2; if (l>mid) UpdataTree(l,r,add,2*k+1); else if (r<=mid) UpdataTree(l,r,add,2*k); else { UpdataTree(l,mid,add,2*k); UpdataTree(mid+1,r,add,2*k+1); } s[k].sum=s[2*k].sum+s[2*k+1].sum; } LL SearchTree(int l,int r,int k) { if (s[k].l==l&&s[k].r==r) return s[k].sum; if (s[k].add!=0) { s[2*k].add+=s[k].add; s[2*k+1].add+=s[k].add; s[2*k].sum+=s[k].add*(s[2*k].r-s[2*k].l+1); s[2*k+1].sum+=s[k].add*(s[2*k+1].r-s[2*k+1].l+1); s[k].add=0; } int mid=(s[k].l+s[k].r)/2; if (l>mid) return SearchTree(l,r,2*k+1); else if (r<=mid) return SearchTree(l,r,2*k); else return SearchTree(l,mid,2*k)+SearchTree(mid+1,r,2*k+1); } int main() { int n,q; LL w; while (~scanf("%d%d",&n,&q)) { InitTree(1,n,1); for (int i=1; i<=n; i++) { scanf("%lld",&w); UpdataTree(i,i,w,1); } for (int i=1; i<=q; i++) { char ch; int a,b; LL c; getchar(); scanf("%c%d%d",&ch,&a,&b); if (ch=='C') { scanf("%lld",&c); UpdataTree(a,b,c,1); } else if (ch=='Q') { LL ans=SearchTree(a,b,1); printf ("%lld ",ans); } } } return 0; }
poj3468(代码片段)
POJ3468题目链接POJ3468题目概述给出一个包含有(N)个元素的数组(a),然后是(m)次操作,操作有以下两种类型:Qxy((xleqy))计算(sum_i=x^ya[i])Cxyd((xleqy))将区间([x,y])内部的每一个(a[i])加上(d).对于第一种查询操作输出对应的结果,数据规模:[1leqN,M... 查看详情
poj3468asimpleproblemwithintegers
DescriptionYouhaveNintegers,A1,A2,...,AN.Youneedtodealwithtwokindsofoperations.Onetypeofoperationistoaddsomegivennumbertoeachnumberinagiveninterval.Theotheristoaskforthesumofnumbersinagiveninterval.In 查看详情
线段树poj3468
DescriptionYouhaveNintegers,A1,A2,...,AN.Youneedtodealwithtwokindsofoperations.Onetypeofoperationistoaddsomegivennumbertoeachnumberinagiveninterval.Theotheristoaskforthesumofnumbersinagiveninterval.In 查看详情
poj-3468asimpleproblemwithintegers
YouhaveNintegers,A1,A2,...,AN.Youneedtodealwithtwokindsofoperations.Onetypeofoperationistoaddsomegivennumbertoeachnumberinagiveninterval.Theotheristoaskforthesumofnumbersinagiveninterval.InputThefirst 查看详情
[poj3468]asimpleproblemwithintegers
DescriptionYouhaveNintegers,A1,A2,...,AN.Youneedtodealwithtwokindsofoperations.Onetypeofoperationistoaddsomegivennumbertoeachnumberinagiveninterval.Theotheristoaskforthesumofnumbersinagiveninterval.In 查看详情
poj3468asimpleproblemwithintegers分块
题解:分块解题报告:是个板子题呢qwq先占坑,今天会写,over 查看详情
poj3468(线段树)(代码片段)
题目链接:http://poj.org/problem?id=3468Youhave N integers, A1, A2,..., AN.Youneedtodealwithtwokindsofoperations.Onetypeofoperationistoaddsomegivennumbertoeachnumberinagiveninterva 查看详情
poj3468
成段更新这题数据很大,注意要__int64。其他差不多,/*poj3468注意标签的作用,注意是+=,才能传递正确的值下去,一定要注意到这定,标签的值*/#include<stdio.h>#include<stdlib.h>#defineMAX150000usingnamespacestd;typedefstructnode{ints,e;__i... 查看详情
poj3468asimpleproblemwithintegers
题目链接:http://poj.org/problem?id=3468区间更新,区间求和。1#include<cstdio>2#include<algorithm>3usingnamespacestd;4#definelllonglong5#definelsonl,m,rt<<16#definersonm+1,r,rt<<1|17constintma 查看详情
poj3468asimpleproblemwithintegers
ASimpleProblemwithIntegersTimeLimit:5000MS MemoryLimit:131072KTotalSubmissions:110999 Accepted:34570CaseTimeLimit:2000MSDescriptionYouhaveNintegers,A1,A2,...,AN.Youneedtodealwithtwokindsofop 查看详情
asimpleproblemwithintegers~poj-3468
Youhave N integers, A1, A2,..., AN.Youneedtodealwithtwokindsofoperations.Onetypeofoperationistoaddsomegivennumbertoeachnumberinagiveninterval.Theotheristoaskforthesumofnumbers 查看详情
poj3468asimpleproblemwithintegers
ASimpleProblemwithIntegersTimeLimit: 5000MS MemoryLimit: 131072KTotalSubmissions: 110087 Accepted: 34277CaseTimeLimit: 2000MSDescriptionYouhave N integers, 查看详情
poj3468asimpleproblemwithintegers——线段树区间修改
题目链接:https://vjudge.net/problem/POJ-3468 Youhave N integers, A1, A2,..., AN.Youneedtodealwithtwokindsofoperations.Onetypeofoperationistoaddsomegivennumbertoeachnumberinag 查看详情
poj3468asimpleproblemwithintegers(线段树单点更新+区间求和)
题目链接:http://poj.org/problem?id=3468题意:单点更新,区间求和。题解:裸题1//POJ3468ASimpleProblemwithIntegers2//单点更新区间求和3#include<cstdio>4#include<iostream>5#include<algorithm>6usingnamespacestd;78typed 查看详情
poj3468asimpleproblemwithintegers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem?id=3468ASimpleProblemwithIntegersTimeLimit: 5000MS MemoryLimit: 131072KTotalSubmissions: 83959 Accepted: 25989CaseTimeLimit:&n 查看详情
poj3468asimpleproblemwithintegers
TimeLimit: 5000MS MemoryLimit: 131072KTotalSubmissions: 106771 Accepted: 33308CaseTimeLimit: 2000MSDescriptionYouhave N integers, A1, A2,...,&nbs 查看详情
poj3468asimpleproblemwithintegers(代码片段)
Youhave N integers, A1, A2,..., AN.Youneedtodealwithtwokindsofoperations.Onetypeofoperationistoaddsomegivennumbertoeachnumberinagiveninterval.Theotheristoaskforthesumofnumbers 查看详情
poj3468
TLE+WA了无数次,发现了不少毛病,做个记录先1#include<iostream>2#include<cstring>3#include<cstdio>4#include<cstdlib>5#include<algorithm>6#include<cmath>7usingnamespacestd;8910#definels 查看详情