随笔分类 - 线段树
摘要:题意: 知道了 n 个人的坐标和一个边长为R的正方形,问正方形内最多可以包含多少人。分析:离散化坐标,将y坐标映射到区间上,按x坐标从小到达扫描。#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define maxn 10005long long max(long long a, long long b){ return a>b?a:b;}struct seg{ long long x, y, val;}s[maxn*2];int add[max
阅读全文
摘要:题意:Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting from 0) Q A B: output the length of the longest consecutive increasing subsequence (LCIS) in [a, b].分析:区间合并类线段树。 las[maxn<<2] 区间左端起最长的序列长度 ras[maxn<<2] 区间右端起最长的序列长度mov[maxn<<2]区间最优值#in
阅读全文
摘要:题意: 有连续 n 个操作,一开始集合为空,对应有三种操作: add x :如果x 不在集合中,加入 x 到集合中 del x: 如果x 在集合中,删除之 sum: 集合中存在的数排好序之后输出满足 序号%5=3 的数值的和分析:每个区间维护两个数组num[i] i 区间内部不同数值的个数 s[i][5] i 区间内部数值序号对 5 取模后各种情况的和#include<stdio.h>#include<string.h>#include<map>#include<algorithm>using namespace std;#define maxn
阅读全文
摘要:题意: 有 n 个数,从做到到右依次为1..n,有m 组操作,每次输入一个值k,讲序列中的第k大元素取出,问最后取到的数字的和为多少。分析: 线段树,num[] 表示该区间有多少个数。#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define clr(x)memset(x,0,sizeof(x))#define maxn 270000int num[maxn<<3];void creat(int l,int r,int rt){ if(l==r
阅读全文
摘要:题意: 现有一个空的集合,有连续的 m 组操作 B x 将 x加入集合 A x 询问集合中的元素 y % x 的取得最小值的 y分析:鸽巢定理+线段树然后对每个y,由鸽巢定理,连续的y+1个数中必然存在mod y相同的数,可以多区间查询, 即[0, y - 1] [y , 2 * y - 1] 取最优解当y比较小的时候,直接遍历会更快,即当 y<log(n) 的时候采用遍历的方法 由于数据比较少,可以先进行离散化来提高效率。#include<stdio.h>#include<string.h>#define clr(x)memset(x,0,sizeof(x))#
阅读全文
摘要:题意:有一个长度为 N 的墙,定义两种操作: 1 a b c 将 区间[a,b]涂成 c颜色 2 a b c 询问区间[a,b]中c颜色的的个数。分析 : 区间合并类线段树。#include<cstdio>#include<cstring>#define clr(x)memset(x,0,sizeof(x))#define maxn 100005#define max(a,b)(a)>(b)?(a):(b)#define min(a,b)(a)<(b)?(a):(b)int hi[maxn<<2];int lo[maxn<<2];in
阅读全文
摘要:题意: 给出一个序列和 M 个询问,对于每个询问输出询问区间内从右向左第一个出现次数超过 2 的数。分析:预处理用 before[i] 表示第 i 个数之前出现的距离第 i 个数最近求和 第 i 数相等的数的值。 区间查询的时候先查询右儿子区间,再查询能合并的区间,再查询左儿子区间。#include<stdio.h>#include<string.h>#include<map>using namespace std;#define maxn 500005#define max(a,b)(a)>(b)?(a):(b)int va[maxn];int be
阅读全文
摘要:题意: 给一列数对应两种操作: 1 a b v, 把[a, b] 的值改为v,即A[a] = A[a+1] = ... = A[b] = v。 2 a b, 查询[a, b] 之间的相同数的连续和最大值。分析:线段树,区间合并 + 成段更新 ... int lva[maxn<<4];// 区间最左面的值 int lnu[maxn<<4]; // 区间最左面的数的个数 int rva[maxn<<4]; // 区间最右面的值 int rnu[maxn<<4]; // 区间最右面的数的个数 int mva[maxn<<4]; // 区间最
阅读全文
摘要:题意: 有N 个人站队,告诉每个人想插队的位置,要求确定最终的站队位置。分析: 越往后插队的人位置越固定,因此可以从后向前插入,每个人插入的位置为从第1 个位置起的第 pos+1 空位置。 用sum[i] 表示 I 区间的人数之和,用线段树维护即可。#include<stdio.h>#include<string.h>#define clr(x)memset(x,0,sizeof(x))#define maxn 200005struct P{ int pos,val;}q[maxn];int sum[maxn<<3];int va[maxn<<3
阅读全文
摘要:题意: 有一个有N 个区间木板,存在最多 30 种颜色,一开始木板的颜色都为第一种颜色。 定义两种操作: Cxyc 将区间[x,y]涂成 c 颜色。 P xy询问区间[x,y]共有多少种颜色。分析: 比较典型的成段更新的线段树染色问题,使用延迟标记数组。#include<stdio.h>#include<string.h>#define clr(x)memset(x,0,sizeof(x))#define maxn 100005int col[maxn<<3];int v[33];void creat(int l,int r,int rt){ col[rt]
阅读全文
摘要:题目:View Code Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remember, vividly, on the beautiful Zhuhai Campus, 4 years ago, from the moment I saw you smile, as you were walking out of the classroom and turned your head back, with the soft sunset
阅读全文
摘要:题意: 有 n 个矩形,求总的覆盖面积。分析: 离散化浮点数坐标,以 x 轴 建树, 将每个矩形拆分成上下两条线段,从下向上扫描。/*************************************** 离散化浮点数,把矩形分成上边和下边, ** 按 x 坐标建树,从下向上扫描 ****************************************/#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#define clr(x)memse
阅读全文
摘要:题意: 一个有 N 个连续房间的旅馆,对 m 次询问对应两种操作。 1 a 询问是不是有连续长度为a的空房间,有的话住进最左边 2 a b 将[a,a+b-1]的房间清空分析: 区间合并类线段树。update 成段更新 query 找到满足条件的最左端点#include<stdio.h>#include<string.h>#define maxn 50005int max(int a,int b){ return a>b?a:b; }int lsum[maxn<<2];int rsum[maxn<<2];int msum[maxn<&
阅读全文
摘要:题意: 有两个字符串,给出 Q 个询问,每个询问有两种方式: 1 p i c 把第 p 个字符串的第i 个字符换成 字符 c, 2 i 从位置i 开始,两个字符串连续相同的子串的最大长度为多少。分析: 线段树。 数组len[rt] 保存rt 区间从最左端开始的最长子串, 合并时,如果len[rt<<1]==mid-l+1 则 len[rt]=len[rt<<1]+len[rt<<1|1] ,说明可以把右儿子区间直接接到左儿子区间上,否则由于左儿子区间与右儿子区间断开, len[rt]=len[rt<<1].#include<stdio.h&
阅读全文
摘要:题意: 有n 个敌人,告诉每个船的初始血量,攻击时告诉你一个区间,这个区间里面的船每个船的血变成以前的根号倍,询问的时候输出每个区间的总和。分析:注意的地方就是当这个区间的和等于区间长度时,就不用再更新。View Code #include<stdio.h>#include<string.h>#include<math.h>const int maxn=111111;__int64 sum[maxn<<2];void creat(int l,int r,int rt){ if(l==r) { scanf("%I64d",&am
阅读全文
摘要:DescriptionThe citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rul
阅读全文
摘要:DescriptionYou 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.InputThe first line contains two numbers N and Q. 1 ≤
阅读全文
摘要:Problem DescriptionIn 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.Let us number the consecutive metallic stick
阅读全文
摘要:Problem DescriptionAt the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important inf
阅读全文
摘要:Problem DescriptionThe inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence.
阅读全文


浙公网安备 33010602011771号