题意: 给一个序列,初始全为0,然后有4种操作: 1. 给区间[L,R]所有值+c 2.给区间[L,R]所有值乘c 3.设置区间[L,R]所有值为c 4.查询[L,R]的p次方和(1<=p<=3) 解法: 线段树,维护三个标记,addmark,mulmark,setmark分别表示3种更新,然后p[1],p[2],p[3]分别表示该节点的1,2,3次方和.标记传递顺序setmark一定是第一个,因为setmark可以使mulmark,addmark都失效还原,mulmark和addmark的顺…
自己写了一个带结构体的WA了7.8次 但是测了几组小数据都对..感觉问题应该出在模运算那里.写完这波题解去对拍一下. 以后线段树绝不写struct!一般的struct都带上l,r 但是一条线段的长度确定的话,每一个节点的l,r都可以确定的,没必要用struct存上,如果不带上l,r那不是更没必要用结构体hhh 这道题有点坑的地方就是顺序问题 pushdown就是一个最大的难点 change表示改数字 add表示加上数字 multiply表示乘以一个数字 正确的方法应该是 先change 再mul…
没什么说的裸线段树,注意细节就好了!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include<iomanip> #include<cmath> #include<cstring> #include<vector> #define ll __int64 #define pi acos(-1.0) #define MAX 100003…
Yuanfang is puzzled with the question below: There are n integers, a 1, a 2, …, a n. The initial values of them are 0. There are four kinds of operations. Operation 1: Add c to each number between a x and a y inclusive. In other words, do transformat…
Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others)Total Submission(s): 7144    Accepted Submission(s): 1811 Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2…
参考:http://www.cnblogs.com/oyking/p/3323306.html 相当不错的思路,膜拜之~ 个人理解改日补充. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 #define l…
Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有时候需要更新某位同学的成绩.   Input 本题目包含多组测试,请处理到文件结束. 在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目. 学生ID编号分别从1编到N.…
>>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<algorithm> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; ; ]; void build(int l, int r, int rt) { if (l == r){ cin &g…
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. Now Pudge wants to do some operations on the hook.…
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,-,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值. Input 第一行两个整数N和P(1≤P≤100000…
这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了ACdreamer的模板,在此基础上自己用宏定义来精简了一下代码: #include<cstdio> typedef long long LL; #define root int rt, int l, int r #define lson rt*2, l, mid #define rson rt*2…
Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 2496    Accepted Submission(s): 788 Problem Description Today is the 10th Annual of “September 11 attacks”, the Al Qaeda is about to attack…
Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4838    Accepted Submission(s): 1853 Problem Description In mathematics and computer science, an algorithm describes a set of procedures…
Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2030    Accepted Submission(s): 743 Problem Description The Game “Man Down 100 floors” is an famous and interesting game.You can enjoy t…
title: hdu-1540线段树刷题 date: 2018-10-18 19:55:21 tags: acm 刷题 categories: ACM-线段树 概述 哇,,,这道线段树的题可以说是到目前为止我所做过的最难的一道了吧QAQ,,,,,, 一开始读完题就是一脸懵逼,,,,完全不知道该从哪里下手,,,就是知道这是一道线段树的题也不知道该怎么下手啊啊啊,,,, 最后还是看了kaungbin大佬的代码,,,QAQ 光是读代码就花了一两个小时,,,(不过也有可能和今天贼困有关,,,脑袋不怎么转…
title: hdu-5023线段树刷题 date: 2018-10-18 13:32:13 tags: acm 刷题 categories: ACM-线段树 概述 这道题和上次做的那道染色问题一样,,,这次主要是看看我再过去两三天之后,,大概凭借以前的记忆敲出来得多长的时间,,,, 结果是,,,大体的框架没问题了,,,,一遍下来编译也没问题,,,但是,,细节问题有两个,,, 数组写成了1e6而不是1e6+10虽然对本题没什么影响,, 建树中的初始化操作时染色初始化为2,,,所以应该是从右往左数…
title: poj-2777线段树刷题 date: 2018-10-16 20:01:07 tags: acm 刷题 categories: ACM-线段树 概述 这道题是一道线段树的染色问题,,,, 做了几道染色的问题,,好像渐渐的熟悉的染色问题的大概的解体思路,,,不再像刚开始做的时候那样一脸懵逼,,,只能去翻博客去看别人的思路,,,好歹这次没有看别人博客自己写出来,,,(除了一些细节没考虑到wa的一发,,,,逃 分析与思路 题面 大概的意思就是给一个区间1~n,,,然后最多有30种颜色,…
title: zoj-1610线段树刷题 date: 2018-10-16 16:49:47 tags: acm 刷题 categories: ACM-线段树 概述 这道题是一道简单的线段树区间染色问题,,, 但是,,,,刚学lazy更新没多久的我看到这样的题心里还是发怵,,, 本来是一道简单的题一开始就是不知道怎么用线段树维护染色的区间,,,还有一个老毛病,,,还是不知道怎么把题目里的信息抽象出来,,, 明确线段树所要维护的信息以及如何对这些信息如何更新和查询,,, 思路分析 这道题和前几天做…
http://codeforces.com/contest/760/problem/E 题目大意:现在对栈有m个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶, 注意,已有操作要按它们的时间顺序进行. 思路:线段树好题啊啊,我们把push当成+1, pop当成-1,按操作的位置建立线段树,那么如何 寻找栈顶呢,我们计算每个点的后缀,栈顶就是下标最大的>0的后缀,我们拿后缀建立线段树, 剩下的就是区间加减法,和求区间最大值啦. #include<bits/stdc++.h>…
点此看题面 大致题意: 给出一个长度为\(n\)的数列\(a\)和一个长度为\(m\)的数列\(b\),求\(a\)有多少个长度为\(m\)的子串与\(b\)匹配.数列匹配指存在一种方案使两个数列中的数两两配对,数配对指它们的和不小于\(h\). 预处理 显然,要判断两个数列是否匹配,肯定是将一个数列从小到大排序,另一个数列从大到小排序,然后逐一判断相应位置上的两个数是否配对. 我们可以将其转化,把\(b_i\)变成\(h-b_i\),然后将\(a\)的某个子串和\(b\)都从大到小排序,再比较…
hdu1823 题意 单点更新,求二维区间最值. 分析 二维线段树模板题. 二维线段树实际上就是树套树,即每个结点都要再建一颗线段树,维护对应的信息. 一般一维线段树是切割某一可变区间直到满足所要查询区间,求最值.求和等,二维就是先切割第一维的区间,再去切割第二维的区间. code #include<bits/stdc++.h> using namespace std; #define lson l, m, rt << 1 #define rson m + 1, r, rt <…
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, A1, A2, ... , AN. You need to deal with two kinds of operations. One type o…
可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <vector> using namespace std; ]; ],Left[],Right[]; ]; v…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578 Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations.Operation 1: Add c to each…
Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others)Total Submission(s): 49    Accepted Submission(s): 16 Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2, …,…
Transformation Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 65535/65536 K (Java/Others)Total Submission(s): 3830    Accepted Submission(s): 940 Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2,…
https://cn.vjudge.net/problem/HDU-4578 题意 4种操作,区间加,区间乘,区间变为一个数,求区间的和.平方和以及立方和. 分析 明显线段树,不过很麻烦..看kuangbin大神的代码打的 用sum1,sum2,sum3分别代表和.平方和.立方和. 懒惰标记使用三个变量: lazy1:是加的数 lazy2:是乘的倍数 lazy3:是赋值为一个常数,为0表示没有. #include <stdio.h> #include <string.h> #inc…
学习线段树~~~~~~~~~~~~要好好理解 此题是单点更新的线段树,考虑基本的询问,更新. #include <iostream> #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <vector> #include &…
思路:就是树状数组的模板题,利用的就是单点更新和区间求和是树状数组的强项时间复杂度为m*log(n) 没想到自己以前把这道题当线段树的单点更新刷了. 树状数组: #include<iostream> #include<cstdio> #include<cstring> using namespace std; ; int tree[maxn], n; void add(int k, int num) { while (k <= n) { tree[k] += nu…
http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum就是当前数的逆序数. 这样查询完之后得到初始数列的逆序数,要求得所有序列的最小逆序数,还需要循环一次. 设初始序列abcde中逆序数为k,小于a的个数是t-1那么大于a的个数就是n-t,当把a左移一位,原来比a大的都变成了a的逆序对,即逆序数增加了n-t,但是原来比a小的数都变成了顺序, 因此逆序数…