树状数组
tomjobs
别慌,慌也没用
展开
-
LeetCode 315. 计算右侧小于当前元素的个数(树状数组)
思路: 树状数组裸题 class Solution { public: const static int maxn = 3e4 + 7; int c[maxn]; void add(int x,int v) { while(x < maxn) { c[x] += v; x += x & -x; } } int query(int x) { int res =.原创 2021-03-17 14:34:15 · 189 阅读 · 0 评论 -
Codeforces-1486 F. Pairs of Paths(计数,dfs序树状数组,LCA)
题意: 一棵树,给你mmm条路径,问有多少对路径满足只有一个交点。 思路: 设1为根,然后找出每个路径两个端点的lcalcalca。可以得到路径间仅一个交点只有两种情况:两个路径lcalcalca相同,一个路径的lcalcalca在另外一个路径上。 先对每个路径进行一下处理,除了记录两个端点x,yx,yx,y记录其lcalcalca,还要保存两个端点对应在lcalcalca子树中位置a,ba,ba,b。并且要保证a<ba<ba<b(交换实现)。对于xxx或者yyy本身就是lcalcal.原创 2021-02-20 16:22:30 · 269 阅读 · 0 评论 -
B - A Leap of faith Gym - 101864B(树状数组求区间相交)
题意: 两类区间,求相互之间有交集区间对数。 每次操作还可能增加某一类区间。 思路: 对每一类区间的左右下标分别开一个树状数组记录出现次数。 那么对于第一类区间[l,r][l,r][l,r],我们要计算其与第二类区间有交集的个数。直接计算不好算,我们算与其没有交集的个数,等价于区间右端点在lll之前,或者区间左端点在rrr之后的区间。这个统计过程就可以用树状数组了。 #include <cstdio> #include <cstring> #include <vector>原创 2020-11-22 17:56:18 · 270 阅读 · 0 评论 -
P1972 [SDOI2009]HH的项链(树状数组离线,主席树)
题意: 多次询问[l,r][l,r][l,r]中有多少不同的数。 思路: 本题卡了莫队。 树状数组离线:每个点代表这个位置的值,然后每次遇到这个数,就把上次的位置清空。这样当前维护的区间里面就没有重复数了。 可持久化线段树:其实和树状数组离线一样,就是基于上一个前缀的线段树,将当前位置的值设置为a[i]a[i]a[i],同时将a[i]a[i]a[i]上一次出现的位置的值减去a[i]a[i]a[i]。 可持久化线段树 #include <iostream> #include <string&原创 2020-10-31 11:20:47 · 401 阅读 · 3 评论 -
2020秦皇岛CCPC 7-2 Bounding Wall(树状数组+二分,并查集)
题意: 一个n*m的方格,每个格子可以是’#‘或者’.‘ 。每次操作可以改变一个格子的状态,或者询问经过(x,y)的最大方框面积,且这个方框不能经过’.’ 。 思路: 思路参考自大佬 每次询问一个点(x,y)(x,y)(x,y),我们只需要考虑以这个点为矩形下边的情况,然后枚举上面那条边。其他的情况只需要将方格旋转90°/180°/270°即可。 我们维护每个点最左边/右边/上边的’.'位置。那么每次修改点,只需要单独维护出这个点左边/右边/上边的位置,复杂度O(n+m)O(n+m)O(n+m)。 你想想原创 2020-10-23 12:19:39 · 290 阅读 · 0 评论 -
Codeforces 1430 E. String Reversal(树状数组)
You are given a string ????. You have to reverse it — that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal — and so on. For example, if原创 2020-10-11 21:34:21 · 579 阅读 · 0 评论 -
HHKB Programming Contest 2020 C - Neq Min(树状数组+二分)
题意: 求对于每个前缀的数第一个未出现的非负数 思路: 直接树状数组+二分乱搞 #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <queue> using namespace std; const int maxn = 2e5 + 10; int n; int vis[maxn]; int c[maxn]; void add(.原创 2020-10-11 20:30:36 · 186 阅读 · 0 评论 -
2020杭电多校第一场 Finding a MEX(分块+树状数组,维护MEX)
Problem Description Given an undirected graph G=(V,E). All vertices are numbered from 1 to N. And every vertex u has a value of Au. Let Su={Av│(u,v)∈E}. Also, F(u) equals MEX(minimum excludant) value of Su. A MEX value of a set is the smallest non-negative原创 2020-07-26 22:34:13 · 326 阅读 · 0 评论 -
ACWING243. 一个简单的整数问题2(树状数组区间修改查询)
给定一个长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1、“C l r d”,表示把 A[l],A[l+1],…,A[r] 都加上 d。 2、“Q l r”,表示询问 数列中第 l~r 个数的和。 对于每个询问,输出一个整数表示答案。 输入格式 第一行两个整数N,M。 第二行N个整数A[i]。 接下来M行表示M条指令,每条指令的格式如题目描述所示。 输出格式 对于每个询问,输出一个整数表示答案。 每个答案占一行。 数据范围 1≤N,M≤105, |d|≤10000, |A[i]|≤10000原创 2020-07-15 09:16:33 · 238 阅读 · 0 评论 -
ACWING242. 一个简单的整数问题(树状数组差分)
给定长度为N的数列A,然后输入M行操作指令。 第一类指令形如“C l r d”,表示把数列中第l~r个数都加d。 第二类指令形如“Q X”,表示询问数列中第x个数的值。 对于每个询问,输出一个整数表示答案。 输入格式 第一行包含两个整数N和M。 第二行包含N个整数A[i]。 接下来M行表示M条指令,每条指令的格式如题目描述所示。 输出格式 对于每个询问,输出一个整数表示答案。 每个答案占一行。 数据范围 1≤N,M≤105, |d|≤10000, |A[i]|≤1000000000 输入样例: 10 5 1原创 2020-07-15 09:10:05 · 175 阅读 · 0 评论 -
Apple Tree POJ - 3321(dfs序树状数组)
There is an apple tree outside of kaka’s house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by branches. Kaka numbers the f原创 2020-06-19 21:59:34 · 356 阅读 · 0 评论 -
Turing Tree HDU - 3333(求不同数的和,树状数组离线 / 莫队)
After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because Turing Tree could easily have the solution. As well, wily 3xian made lots of new problems about intervals. So, today, this sick thing happens again… Now gi原创 2020-06-19 21:29:04 · 248 阅读 · 0 评论 -
2019CCPC湘潭邀请赛 Chika and Friendly Pairs(莫队+树状数组)
Problem Description Chika gives you an integer sequence a1,a2,…,an and m tasks. For each task, you need to answer the number of “friendly pairs” in a given interval. friendly pair: for two integers ai...原创 2020-04-03 19:32:31 · 336 阅读 · 0 评论 -
Minimum Inversion Number HDU - 1394(逆序数变形)
The 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 ...原创 2020-01-29 12:52:29 · 3429 阅读 · 0 评论 -
Ping pong UVA - 1428(树状数组)
N (3 ≤ N ≤ 20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank, they often compete with each...原创 2020-01-28 00:28:03 · 209 阅读 · 0 评论 -
Counting Sequences HDU - 3450(多元上升组 树状数组)
For a set of sequences of integers{a1,a2,a3,…an}, we define a sequence{ai1,ai2,ai3…aik}in which 1<=i1<i2<i3<…<ik<=n, as the sub-sequence of {a1,a2,a3,…an}. It is quite obvious that a...原创 2020-01-26 17:06:44 · 354 阅读 · 0 评论 -
Japan POJ - 3067(逆序对)
Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 500000, N ...原创 2020-01-24 23:32:50 · 545 阅读 · 0 评论 -
Crazy Thairs POJ - 3378(10000进制加法 树状数组)
These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ≤ N ≤ 50000) numbers, which are no more than 109, Crazy Thair is a group of 5 numbers {i, j, k, l, m} satisfying: 1 ≤ i < ...原创 2020-01-24 16:06:57 · 4493 阅读 · 0 评论 -
可怜的ljb(树状数组,逆序对)
Description Ljb学长最爱出题了!只不过很可惜每次比赛,大家都能准确的避开ljb的题目,ljb欲哭无泪。这次ljb希望能有人写出自己的题目,所以他开始研究大佬们的出题顺序。 他看了一场cf的排名。一共有nn个人,编号为从1到nn的整数。 QQ图片20190213220457.png 第一行是第1小时结束时的rankrank从第1名到第nn名的同学的编号,第二行是第2小时结束时的rank...原创 2019-03-27 22:17:35 · 351 阅读 · 0 评论 -
ACWING241. 楼兰图腾(树状数组,三元上升组计数)
在完成了分配任务之后,西部314来到了楼兰古城的西部。 相传很久以前这片土地上(比楼兰古城还早)生活着两个部落,一个部落崇拜尖刀(‘V’),一个部落崇拜铁锹(‘∧’),他们分别用V和∧的形状来代表各自部落的图腾。 西部314在楼兰古城的下面发现了一幅巨大的壁画,壁画上被标记出了N个点,经测量发现这N个点的水平位置和竖直位置是两两不同的。 西部314认为这幅壁画所包含的信息与这N个点的相对位置有关,...原创 2019-09-10 23:57:37 · 168 阅读 · 0 评论 -
Lost Cows POJ - 2182 ACWING244. 谜一样的牛(树状数组+二分/倍增)
有n头奶牛,已知它们的身高为 1~n 且各不相同,但不知道每头奶牛的具体身高。 现在这n头奶牛站成一列,已知第i头牛前面有Ai头牛比它低,求每头奶牛的身高。 输入格式 第1行:输入整数n。 第2…n行:每行输入一个整数Ai,第i行表示第i头牛前面有Ai头牛比它低。 (注意:因为第1头牛前面没有牛,所以并没有将它列出) 输出格式 输出包含n行,每行输出一个整数表示牛的身高。 第i行输出第i头牛的身高...原创 2019-09-11 20:52:44 · 407 阅读 · 0 评论 -
A - Ultra-QuickSort OpenJ_Bailian - 2299(离散化求逆序对/归并排序)
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted i...原创 2019-09-20 21:25:08 · 340 阅读 · 0 评论 -
D - Infinite Inversions CodeForces - 540E(离散化树状数组)
There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, …}. We performed n swap operations with this sequence. A swap(a, b) is an operation of swapping...原创 2019-09-22 13:59:36 · 162 阅读 · 0 评论 -
A“一个部族,一个民族,一个弗雷尔卓德。”(素数筛,逆序对,树状数组)
“一个部族,一个民族,一个弗雷尔卓德。” Description 寒冰射手艾希新学会了一个技能,艾希通过这个技能成为了一名声名远扬的神箭手,从此再也无人敢侵犯弗雷尔卓德! 这个技能的描述如下(假设英雄联盟内的每个人都有一个编号): 假设艾希有x-1(x>=2)x−1(x>=2)个敌人,每个敌人的编号分别为1;;1~;;x-1x−1,那么艾希的编号就是xx。艾希每次使用这个技能,那么对于...原创 2019-03-27 09:29:19 · 278 阅读 · 0 评论