树状数组、线段树
薄层
努力就有收获~
展开
-
POJ 2528(线段树+离散化)
题意:n(n求出最后还能看见多少张海报。(题意是我抄的...)思路:能够想到用线段树去进行区间更新,求最终的更新结果。但发现给的数据会得到1000万,所以普通的线段树是开不了的,但是发现n的范围为1万,可以在这上面做点文章,所以可以对这给定的n个区间进行离散化。普通离散化的具体步骤为:对所有输入的数排序,然后进行去重,然后将数映射到1~n。但是普通离散化在本题中是不可行的,原创 2017-05-21 15:32:23 · 345 阅读 · 0 评论 -
树状数组区间更新+区间查询+单点查询
树状数组时间复杂度为O(MlogN), 实际用的时候优于线段树,且写得少。神牛是引入了差分数组,要维护的差分数组ks[i] = a[i] - a[i-1]; 可以容易得到a[i] = ks[1] + ks[2] + ... + ks[i]; 即前i项和,为方便记为sigma(ks, i),已经可以看到树状数组的影子了,所以求区间和随之得到a[1] + a[2] + .. + a[n] = sigma(ks, 1) + sigma(ks, 2) + ... + sigma(ks, n); = n*k原创 2017-02-21 15:36:25 · 1770 阅读 · 1 评论 -
线段树
主要写写自己的想法吧,网上的前几篇博文写的都很详细,线段树主要目的主要是用来高效地解决单点或连续区间的查询或操作。线段树的叶子节点记录的是原数组的值,然后每个非叶节点都有一个左儿子和右儿子。比方说要求某区间内的最小值,已知叶子节点记录的是原数组值,那么它的父节点存储的值则是它和另一个同父兄弟节点两者间的最小值,即父节点表示的区间范围内的最小值,依照此种思路,不断向上更新,线段树的节点会存储相应原创 2017-03-09 17:09:08 · 331 阅读 · 0 评论 -
树状数组的基本操作
不得不说,树状数组很优雅,网上前几篇博文写的很容易理解,在这里主要是记录下自己的学习。先来张图,方便理解树状数组最主要的几个部分,一是求lowbit,一个正数的相反数的二进制是正数的二进制取反加1,因此它们之间进行&运算,便得到了lowbit——一个数二进制的只保留一个最低位的1,高位的1全部清空。int lowbit(int k){ return k&-k;}通过原创 2017-03-10 15:04:10 · 535 阅读 · 0 评论 -
2017年第0届浙江工业大学之江学院程序设计竞赛决赛—J qwb又偷懒了
Descriptionqwb最近在做一个群众收入统计。ta非常懒,以至于忘记了今天领导要来视察。所以急忙催下属去做统计。在接下来长度为n的时间里,每个单位时间都有事情发生,可能会发下以下两种事件:1)下属递交了一份调查报告,由于太匆忙,上面只有一个整数x,代表一个居民的收入。2)领导来视察了,领导会来询问,收入在区间[l,r]内的居民的平均收入,qwb需要给出回答。qw原创 2017-06-02 19:05:00 · 309 阅读 · 0 评论 -
计蒜客-2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛G题Query on a strin(树状数组+暴力更新)
You have two strings S and T in all capitals.Now an efficient program is required to maintain a operation and support a query.The operation C i ch with given integer i and capital letter ch, changes the i-thcharacter of S into ch.The query Q i j asks t原创 2017-09-11 15:50:23 · 249 阅读 · 0 评论 -
CodeForces-540E Infinite Inversions(树状数组+离散化+加点)
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 the elements of the sequence on positions a and b. Yo原创 2017-09-27 21:02:08 · 302 阅读 · 0 评论 -
HDU-5792 World is Exploding(思维、树状数组+离散化)
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab,Ac>Ada≠b≠c≠d,1≤a<b≤n,1≤c<d≤n,Aa<Ab,Ac>Ad.InputThe input consists of multiple test cases. Each test case begin with an integer n in a single line原创 2017-09-02 17:22:06 · 282 阅读 · 0 评论 -
HDU-6205 card card card(思维+简单线段树)
2017 ACM/ICPC Asia Regional Shenyang OnlineAs a fan of Doudizhu, WYJ likes collecting playing cards very much. One day, MJF takes a stack of cards and talks to him: let's play a game and if you win, you can get all these cards. MJF randomly assigns these原创 2017-09-11 13:27:18 · 589 阅读 · 0 评论 -
Codeforces 827C DNA Evolution(多维树状数组)
Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A", "T", "G", "C". A DNA strand is a sequence of nucleotides. Scientists decided to track evolution of a rare species, which DNA strand was string s initially.E原创 2017-11-02 19:31:56 · 358 阅读 · 0 评论