http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线段树的题目,就是线段树的用区间更新就可以,我也是第一次用.. #include <stdio.h> #include <string.h> #define maxn 100050 long long arra[ maxn ]; struct note{ //要用long long 类型…
题意: 给定一个数字n,表示这段区间的总长度.然后输入n个数,然后输入q,然后输入a,b,表示查询a,b,区间和,或者输入c 再输入三个数字a,b,c,更改a,b区间为c 思路: 线段树首先就是递归建树,可以从左子树开始,将数据输入到叶子节点上.当左区间等于右区间的时候就是叶子节点.在回溯的时候,父亲节点的值根据左右儿子得出值. 区间修改的时候需要用到一个懒惰标记,当前走到的区间如果完全被需要修改的区间包含的时候,只需更改当前节点的值,并且在这进行一个懒惰标记.不需要继续向下递归.如果下面的值查…
参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; struct Node{ int l,r; long long sum,lazy; void update(long long x){//用于更新区间和 和懒标记 sum+=1ll*(r-l+)*x; lazy+=x; } }tree[maxn*]; void push_up(int x){//用于用子区间重新计算该…
题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample Output 455915 # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include &l…
POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m; static final int N = 100005; static int ls[] = new int[N << 2]; static int rs[] = new int[N << 2]; static long M[] = new long[N << 2];…
http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注意 是改为z,不是加或减,最后输出区间总值 也是线段树加lazy操作 #include<cstdio> using namespace std; struct point { int l,r; int val,sum; }; point tree[]; void build(int i,int l…
Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16941    Accepted Submission(s): 5190 Problem Description Give you a sequence and ask you the kth big number of a inteval.   Input The…
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. In…
这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和查询都要pushdown. #include <cstdio> typedef long long LL; + ; int n, m, qL, qR, v; LL sum[maxn << ], add[maxn << ]; inline void maintain(int…
题目连接 //位运算 k<<1 相当于 k*2 k<<1|1 相当于 k*2+1 /* 修改区间内的值,并且维护区间和. 详见代码 */ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int maxn=100000+10; int a[maxn]; struct tree { int l…