![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
线段树
tomjobs
别慌,慌也没用
展开
-
Google Kickstart Round-D D. Locked Doors(线段树+二分)
题意:nnn个点,相邻两点之间有一条边,每条边边权不同。有qqq次询问,每次询问给你一个起点sss,从这个点出发,每次往左右边权更小的点跑,求第kkk个点是什么。思路:参考代码:https://doowzs.com/code/ks2020d-d/想到了线段树,没想到二分。假设是求第kkk个点,那结果可以看作是能包含kkk个点的窗口。假设窗口左移,那么窗口左部分(起点sss左部分)的最大值一定会大于右部分最大值,否则移动的过程就会优先往左移。同理窗口右移,那么左部分最大值一定会小于有部分最大原创 2021-03-09 13:53:43 · 238 阅读 · 0 评论 -
2020江苏省赛A - Array Gym - 102875A(线段树,欧拉降幂)
思路:有加有乘有次方,看起来好像是不可维护的,但是注意到模数很小,可以从这里下手。lazy[i]lazy[i]lazy[i]代表iii转换为lazy[i]lazy[i]lazy[i],因为ppp很小,所以最多只需要维护30个lazylazylazy标记就好了。不过这样常数有点大,我们在计算快速幂的时候使用一下欧拉降幂优化常数。#include <cstdio>#include <iostream>#include <cstring>#include &l..原创 2020-12-02 12:26:58 · 476 阅读 · 1 评论 -
2020小米网络赛第一场 E.Phone Network(线段树)
题意:对于每个i,1≤i≤ni,1≤i≤ni,1≤i≤n,求出包含前iii种数的最小区间。思路:定义f[l][i]f[l][i]f[l][i]代表以lll为左端点时,包含前iii种数的最小右端点。则对于第iii种数出现的位置p1,p2,p3,p4p1,p2,p3,p4p1,p2,p3,p4…以更新区间[p1+1,p2][p1+1,p2][p1+1,p2]为例,这个区间更新满足f[l][i]=max(f[l][i−1],p2)f[l][i]=max(f[l][i-1],p2)f[l][i]=max.原创 2020-11-09 10:15:04 · 206 阅读 · 0 评论 -
2020小米网络赛第二场 Data Structure Problem(线段树)
思路:最大值一定是ai+bi+bi+1+...+bxa_i+b_i+b_{i+1}+...+b_xai+bi+bi+1+...+bx的形式,用sumisum_isumi代表bbb数组前缀和,最大值可以表示成ai−sumi+sumxa_i-sum_i+sum_xai−sumi+sumx。所以直接用线段树维护ai−sumia_i-sum_iai−sumi。#include<cstdio>#include<iostream>#include<algor.原创 2020-11-08 11:33:20 · 81 阅读 · 0 评论 -
2020 CCPC 绵阳7-10 Joy of Handcraft(线段树)
题意:每个灯泡亮的时间为[2kti+1,2kti+ti][2kt_i+1,2kt_i+t_i][2kti+1,2kti+ti],灭的时间为[2kti+ti+1,2kti+2∗ti][2kt_i+t_i+1,2kt_i+2*t_i][2kti+ti+1,2kti+2∗ti],每个灯泡亮度为a[i]a[i]a[i]。求每个时刻最亮灯泡亮度。思路:将相同周期灯泡合并,那么所有灯泡周期都不同了。这样的话更新区间的复杂度就是一个调和级数,所以可以用线段树维护。#include <cst.原创 2020-11-04 20:36:37 · 270 阅读 · 0 评论 -
Snowy Smile HDU - 6638(扫描线,线段树区间合并)
There are n pirate chests buried in Byteland, labeled by 1,2,…,n. The i-th chest’s location is (xi,yi), and its value is wi, wi can be negative since the pirate can add some poisonous gases into the chest. When you open the i-th pirate chest, you will get原创 2020-10-31 19:34:47 · 210 阅读 · 0 评论 -
2020CCPC威海 G Caesar Cipher(线段树+哈希)
Have you ever heard of the Caesar cipher? It is one of the simplest and best-known encryption techniques. Named after Julius Caesar, he used this cipher to communicate with his generals.Caesar cipher is a type of substitution cipher in which each letter i原创 2020-10-30 17:46:47 · 355 阅读 · 0 评论 -
2020牛客多校第四场 Eliminate++(线段树优化贪心)
链接:https://ac.nowcoder.com/acm/contest/5669/E来源:牛客网题目描述ZYB likes to create puzzles for himself and then solve them.There are \ N N (\ N N is odd) distinct integers written in a line on the blackboard, and you decide to erase those numbers from the blac原创 2020-10-16 19:06:55 · 343 阅读 · 0 评论 -
Codeforces 1422F. Boring Queries(线段树+二分、可持久化线段树)
Yura owns a quite ordinary and boring array ???? of length ????. You think there is nothing more boring than that, but Vladik doesn’t agree!In order to make Yura’s array even more boring, Vladik makes ???? boring queries. Each query consists of two intege原创 2020-10-15 00:45:57 · 346 阅读 · 2 评论 -
E - Yet Another Data Structure Problem ZOJ - 3998(线段树维护区间乘积,k次方)
题意:3种询问。1 l r v,代表[l,r]区间每个数乘以v2 l r v,代表[l,r]区间每个数变成v次方3 l r,求[l,r]区间乘积思路:裸的线段树维护区间次幂和区间乘积。lazy1表示子区间还需要乘以lazy1,lazy2表示子区间要变成lazy2次方。注意维护次幂的时候要模(mod-1)(欧拉降幂),且要先维护lazy2,再维护lazy1。#include <cstdio>#include <cstring>#include <algorit原创 2020-10-14 14:10:54 · 299 阅读 · 1 评论 -
Codeforces383 C. Propagating tree(dfs序线段树)
Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numbered from 1 to n, each node i having an initial value ai. The root of the tree is node 1.This tree has a special property: whe原创 2020-09-24 21:54:30 · 364 阅读 · 1 评论 -
Codeforces1418 G. Three Occurrences(线段树,标记不下传)
You are given an array ???? consisting of ???? integers. We denote the subarray ????[????…????] as the array [????????,????????+1,…,????????] (1≤????≤????≤????).A subarray is considered good if every integer that occurs in this subarray occurs there exact原创 2020-09-17 20:41:44 · 238 阅读 · 0 评论 -
Codeforces1401 F. Reverse and Swap(线段树,区间翻转)
You are given an array ???? of length 2????. You should process ???? queries on it. Each query has one of the following 4 types:????????????????????????????(????,????) — change ???????? to ????;????????????????????????????(????) — reverse each subarray [原创 2020-09-09 20:08:15 · 241 阅读 · 0 评论 -
CSUSTOJ 你真的会!(线段树)
思路:对于f(L,R)f(L,R)f(L,R),可以发现L=RL=RL=R时,f(L,R)=a[L]+1f(L,R)=a[L]+1f(L,R)=a[L]+1。否则等于f(L,K)∗f(K,R),L≤K≤Rf(L,K)*f(K,R),L≤K≤Rf(L,K)∗f(K,R),L≤K≤R。所以直接用线段树维护就好了。#include <cstdio>#include <cstring>#include <algorithm>using namespace std.原创 2020-08-14 09:26:46 · 229 阅读 · 0 评论 -
P2471 [SCOI2007]降雨量(线段树,模拟)
题意:假线段树,真模拟。对于给出的年份x,yx,yx,y如果x≥yx≥yx≥y,则肯定不合理。如果x,yx,yx,y都没出现过,则有可能合理如果xxx出现过,yyy没出现过,则合理的条件为x的值大于max(x+1~y)如果xxx没出现过,yyy出现过,则合理的条件为y的值大于max(x~y-1)如果x,yx,yx,y都出现过,则按题意模拟,如果符合题意,则看这个区间中是否有年份没有值,如果存在这一的年份,则变成可能合理。#include <cstdio>#include &l.原创 2020-07-17 15:23:14 · 273 阅读 · 0 评论 -
P4198 楼房重建(线段树,维护LIS)
思路:实际上是在维护两个值:区间最值mxmxmx和区间最长上升子序列lenlenlen。只不过这里的最长上升子序列有限制:要求能取尽量取,也就1 5 2 3 4的lenlenlen是2不是4。想清楚了要维护的值,我们就要想怎么维护。对于lenlenlen值,我们只需要对pushuppushuppushup进行修改。int pushup2(int i,double mx) { if(t[i].mx <= mx) return 0; if(t[i].l == t[i].r) r.原创 2020-07-17 15:18:43 · 319 阅读 · 0 评论 -
P4560 [IOI2014]Wall 砖墙(线段树,标记重复覆盖)
题目背景原题为交互试题,但在此请提交完整程序。题目描述给定一个长度为 nn且初始值全为 00的序列。你需要支持以下两种操作:Add L, R, hL,R,h:将序列 [L, R][L,R]内所有值小于 hh的元素都赋为 hh,此时不改变高度大于 hh的元素值Remove L, R, hL,R,h:将序列 [L, R][L,R]内所有值大于 hh的元素都赋为 hh,此时不改变高度小于 hh的元素值你需要输出进行 kk次上述操作之后的序列。输入格式输入的第一行包含两个正整数 n, kn,k,分别原创 2020-07-17 14:59:27 · 217 阅读 · 0 评论 -
F - Finally, christmas! Gym - 102448F(扫描线)
The christmas spirit is taking over the city of Arcoverde! In honor of this special day, the mayor of this renowned metropolis decided to decorate all of the city’s front view. To do this, he asked his engineer friends Icaro and Cortizo how much it would c原创 2020-06-29 14:16:08 · 368 阅读 · 0 评论 -
B - Bezas Hangover Gym - 102448B(线段树)
Friday nights are tricky for UFPE’s ICPC competitors - they must be careful with their plans, after all, they train on saturdays and must be in good shape to help their team. Beza, however, went to a party on his friday night and said that no booze could b原创 2020-06-29 14:10:04 · 379 阅读 · 0 评论 -
The Child and Sequence CodeForces - 438D(线段树区间取模)
At the children’s day, the child came to Picks’s house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks.Fortunately, Picks remembers how to repair the sequence. Initially h原创 2020-06-27 17:47:24 · 260 阅读 · 0 评论 -
Round C 2020 - Kick Start 2020 Candies(线段树)
ProblemCarl has an array of N candies. The i-th element of the array (indexed starting from 1) is Ai representing sweetness value of the i-th candy. He would like to perform a series of Q operations. There are two types of operation:Update the sweetness原创 2020-05-18 00:35:05 · 504 阅读 · 0 评论 -
B - Mountainous landscape Gym - 100543B(线段树+计算几何)
题意: 给出n个点,求出每个点演当前角度看到的是第几条线段。思路:参考自:https://blog.csdn.net/ezoiHQM/article/details/82154368用线段树维护凸包,可以通过Graham扫描法维护这个凸包(对应add函数)。而向量是否与这个凸包相交可以二分维护,这个部分还没有弄懂。#include <cstdio>#include <algorithm>#include <cstring>#include <cma.原创 2020-05-11 19:38:04 · 297 阅读 · 0 评论 -
Editor CodeForces - 1263E(线段树)
The development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text.Your editor consists of a line with infinite length and cursor, which points to...原创 2020-01-31 16:12:04 · 335 阅读 · 0 评论 -
POJ 2991 Crane(计算几何+线段树)
DescriptionACM has bought a new crane (crane – jeřáb) . The crane consists of n segments of various lengths, connected by flexible joints. The end of the i-th segment is joined to the beginning of th...原创 2020-01-28 20:42:50 · 281 阅读 · 0 评论 -
LCIS HDU - 3308(线段树区间合并)
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].Inpu...原创 2020-01-23 21:49:32 · 257 阅读 · 0 评论 -
Buy Tickets POJ - 2828(线段树离线)
Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…The Lunar New Year was approaching, but unluckily the Little Cat still had sche...原创 2020-01-23 20:43:34 · 365 阅读 · 0 评论 -
Count Color POJ - 2777(线段树cover标记)
Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.There is a very long board with length L centimeter, L is ...原创 2020-01-23 17:55:44 · 207 阅读 · 0 评论 -
Horizontally Visible Segments POJ - 1436(线段树区间覆盖)
There is a number of disjoint vertical line segments in the plane. We say that two segments are horizontally visible if they can be connected by a horizontal line segment that does not have any common...原创 2020-01-23 16:25:27 · 262 阅读 · 0 评论 -
Help with Intervals POJ - 3225(线段树区间异或)
LogLoader, Inc. is a company specialized in providing products for analyzing logs. While Ikki is working on graduation design, he is also engaged in an internship at LogLoader. Among his tasks, one is...原创 2020-01-23 00:34:40 · 309 阅读 · 0 评论 -
"Ray, Pass me the dishes!" UVA - 1400(线段树区间合并)
After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hungry. In returnfor Neal’s help, Ray makes a great dinner for Neal. When it is time for dinner, Ray arranges all thed...原创 2020-01-22 17:42:28 · 311 阅读 · 0 评论 -
Mayor's posters POJ - 2528(离散化 线段树)
The 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 d...原创 2020-01-21 22:28:18 · 199 阅读 · 0 评论 -
Memory Control HDU - 2871(线段树区间合并)
Memory units are numbered from 1 up to N.A sequence of memory units is called a memory block.The memory control system we consider now has four kinds of operations:Reset Reset all memory units fre...原创 2020-01-21 21:13:42 · 201 阅读 · 0 评论 -
Who Gets the Most Candies? POJ - 2886(线段树)
N children are sitting in a circle to play a game.The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from...原创 2020-01-21 15:22:31 · 357 阅读 · 0 评论 -
J.哭泣的阿木木(线段树模板题)
哭泣的阿木木Description没啥用的背景故事:在远古的恕瑞玛,有一个孤独而又忧郁的灵魂,阿木木。他在世间游荡,只为找到一个朋友。他遭受了一种远古的巫术诅咒,注定忍受永世的孤单,因为被他触碰就意味着死亡,被他爱怜就意味着蹂躏。所有自称见过阿木木的人都说他是一具活生生的死尸,身材矮小,通体捆绑着青灰色的绷带。世人围绕阿木木编造了许多神话故事、民间传说和史诗传奇,世代传颂了很长时间,以至于没...原创 2019-03-27 22:17:49 · 231 阅读 · 0 评论 -
Atlantis HDU - 1542(线段树扫描线)
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe diff...原创 2019-08-10 23:56:41 · 251 阅读 · 0 评论 -
1798: [Ahoi2009]Seq 维护序列seq(线段树乘法加法)
Description老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成。 有长为N的数列,不妨设为a1,a2,…,aN 。有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值。Input第一行两个整数N和P(1≤P≤1000000000)。第二行含有N个非负整数,...原创 2019-08-25 20:51:58 · 185 阅读 · 0 评论 -
P2253 好一个一中腰鼓!(线段树区间合并)
展开题目背景话说我大一中的运动会就要来了,据本班同学剧透(其实早就知道了),我萌萌的初二年将要表演腰鼓[喷],这个无厘头的题目便由此而来。Ivan乱入:“忽一人大呼:‘好一个安塞腰鼓!’满座寂然,无敢哗者,遂与外人间隔。”题目描述设想一下,腰鼓有两面,一面是红色的,一面是白色的。初二的苏大学神想给你这个oier出一道题。假设一共有N(1<=N<=20,000)个同学表演,表演...原创 2019-08-22 20:19:33 · 133 阅读 · 0 评论 -
Two Segments CodeForces - 193D(线段树,维护区间段数,好题)
Nick has some permutation consisting of p integers from 1 to n. A segment [l, r] (l ≤ r) is a set of elements pi satisfying l ≤ i ≤ r.Nick calls a pair of segments [a0, a1] and [b0, b1] (1 ≤ a0 ≤ a1 ...原创 2019-09-03 17:49:47 · 241 阅读 · 0 评论 -
ACWING245. 你能回答这些问题吗(线段树区间合并)
给定长度为N的数列A,以及M条指令,每条指令可能是以下两种之一:1、“1 x y”,查询区间 [x,y] 中的最大连续子段和,即 maxx≤l≤r≤y{∑ri=lA[i]}。2、“2 x y”,把 A[x] 改成 y。对于每个查询指令,输出一个整数表示答案。输入格式第一行两个整数N,M。第二行N个整数A[i]。接下来M行每行3个整数k,x,y,k=1表示查询(此时如果x>y,请...原创 2019-09-10 16:15:47 · 271 阅读 · 0 评论 -
ACWING246. 区间最大公约数(线段树差分)
给定一个长度为N的数列A,以及M条指令,每条指令可能是以下两种之一:1、“C l r d”,表示把 A[l],A[l+1],…,A[r] 都加上 d。2、“Q l r”,表示询问 A[l],A[l+1],…,A[r] 的最大公约数(GCD)。对于每个询问,输出一个整数表示答案。输入格式第一行两个整数N,M。第二行N个整数A[i]。接下来M行表示M条指令,每条指令的格式如题目描述所示。...原创 2019-09-10 20:14:11 · 292 阅读 · 0 评论