ZOJ - 2112 Dynamic Rankings 动态主席树 主席树+树状数组
题目链接:https://vjudge.net/problem/ZOJ-2112
题意:能修改的查询第k大
学习博客:https://blog.csdn.net/WilliamSun0122/article/details/77885781
理解:修改pos位置x - >y的话,影响的是【pos,n】,所以用树状数组的思想维护,然后对于每个节点建立可持久化线段树,由x变为y的话就是x位置减一,y位置加一,复杂度:nlognlogn
#include <bits/stdc++.h>
using namespace std;
const int N = 50010;
#define lowbit(x) (x & (-x))struct node1 {char op[2];int x, y, k;
}q[N];
struct node2 {int l, r;int val;
}tree[N * 40];
int n, m;
int a[N], c[N], b[N * 2], len;
int tot;
int root[N], rot[N];
int ul[N], ur[N];
int update1(int pre, int l, int r, int pos, int val) {int cur = ++tot;tree[cur] = tree[pre];tree[cur].val += val;if(l == r) return cur;int mid = (l + r) >> 1;if(pos <= mid) tree[cur].l = update1(tree[pre].l, l, mid, pos, val);else tree[cur].r = update1(tree[pre].r, mid + 1, r, pos, val);return cur;
}
void update2(int pos, int x, int val) {while(pos <= n) {rot[pos] = update1(rot[pos], 1, len, x, val);pos += lowbit(pos);}
}
int query(int pl, int pr, int rtl, int rtr, int l, int r, int k) {if(l == r) return l;int res = tree[tree[rtr].l].val - tree[tree[rtl].l].val;for(int i = pr; i >= 1; i -= lowbit(i)) res += tree[tree[ur[i]].l].val;for(int i = pl; i >= 1; i -= lowbit(i)) res -= tree[tree[ul[i]].l].val;int mid = (l + r) >> 1;if(res >= k) {for(int i = pr; i >= 1; i -= lowbit(i)) ur[i] = tree[ur[i]].l;for(int i = pl; i >= 1; i -= lowbit(i)) ul[i] = tree[ul[i]].l;return query(pl, pr, tree[rtl].l, tree[rtr].l, l, mid, k);} else {for(int i = pr; i >= 1; i -= lowbit(i)) ur[i] = tree[ur[i]].r;for(int i = pl; i >= 1; i -= lowbit(i)) ul[i] = tree[ul[i]].r;return query(pl, pr, tree[rtl].r, tree[rtr].r, mid + 1, r, k - res);}
}
int main() {int T;int tmp;scanf("%d", &T);while(T--) {scanf("%d %d", &n, &m);len = 0;tot = 0;for(int i = 1; i <= n; i++) scanf("%d", &a[i]), b[++len] = a[i], c[i] = a[i];for(int i = 1; i <= m; i++) {scanf("%s", q[i].op);if(q[i].op[0] == 'Q') {scanf("%d %d %d", &q[i].x, &q[i].y, &q[i].k);} else {scanf("%d %d", &q[i].x, &q[i].y);a[q[i].x] = q[i].y;b[++len] = q[i].y;}}sort(b + 1, b + 1 + len);len = unique(b + 1, b + 1 + len) - (b + 1);for(int i = 1; i <= n; i++) {tmp = lower_bound(b + 1, b + 1 + len, c[i]) - b;root[i] = update1(root[i - 1], 1, len, tmp, 1);rot[i] = 0;}for(int i = 1; i <= m; i++) {if(q[i].op[0] == 'C') {tmp = lower_bound(b + 1, b + 1 + len, c[q[i].x]) - b;update2(q[i].x, tmp, -1);c[q[i].x] = q[i].y;tmp = lower_bound(b + 1, b + 1 + len, c[q[i].x]) - b;update2(q[i].x, tmp, 1);} else {for(int j = q[i].x - 1; j >= 1; j -= lowbit(j)) ul[j] = rot[j];for(int j = q[i].y; j >= 1; j -= lowbit(j)) ur[j] = rot[j];printf("%d\n", b[query(q[i].x - 1, q[i].y, root[q[i].x - 1], root[q[i].y], 1, len, q[i].k)]);}}}return 0;
}
相关文章:

CodeForces - 669E Little Artem and Time Machine 动态主席树 / CDQ分治
题目链接:https://vjudge.net/problem/CodeForces-669E 题意:1 x y 在第x秒y点的值1.、2 x y 在第x秒y点的值-1 、 3 x y 查询在x秒y点处的值 题解: 动态主席树:但要注意内存的限制 #include <bits/stdc.h> using na…...

2019CCPC网络赛 HDU - 6703 array 主席树 查询第一个大于等于k的数
题目链接:https://vjudge.net/problem/HDU-6703 题解:因为每次加1e7,k也只有[1, n],所以结果也就是[1, n 1],所以对于加了1e7的数我们set记录一下原先的值,对于每次查询,我们输出set中第一个大…...

2019CCPC网络赛 HDU - 6704 K-th occurrence 后缀数组+ST 二分+主席树
题目链接:https://vjudge.net/problem/HDU-6704 题解:ST表维护下 后缀排序后的公共长度 的最小值,然后二分找出左右符合的位置,主席树维护下排序后的序列,然后主席树查询第k大即可 #include <bits/stdc.h> usi…...

2019ccpc网络赛 HDU - 6705 path 贪心跑第k小的路径长度
题目链接:https://vjudge.net/problem/HDU-6705 题解:建立源点 汇点 跑A*,到最后也一直超内存也真是菜到家了,A*时间空间怎么也得n^2,这个题原来是个贪心。。。。 官方题解: 先把每条边以 形式放进堆&am…...

CodeForces - 1208B Uniqueness 二分+离散
题目链接:https://cn.vjudge.net/problem/CodeForces-1208B 题解:二分答案,然后枚举区间,注意要先离散,直接用map会T #include <bits/stdc.h> using namespace std; typedef long long ll; int mp[2010]; int …...

CodeForces - 1208C Magic Grid 构造
题目连接:https://cn.vjudge.net/problem/CodeForces-1208C 题解:因为都是4的倍数,想到二进制位的话应该是每四位一个整体,比如0-15,前两位表示行,后两位表示列,就能得到: 0 1 2 3…...

CodeForces - 1208D Restore Permutation 线段树
题目链接:https://cn.vjudge.net/problem/CodeForces-1208D 题解:从后往前操作,这样就能通过si确定出pi是什么,当然不能枚举,线段树维护下当前每个数如果放在当前位置的话si是多少,然后就是线段树的基本操…...

CodeForces - 1076E Vasya and a Tree 树剖?nono dfs+树状数组
题目链接:https://cn.vjudge.net/problem/CodeForces-1076E 题解:树状数组维护下深度,到达每个节点,对于每次更新容斥一下,对于直接查询即可 #include <bits/stdc.h> using namespace std; typedef long long …...

Contest1816 - 2019年我能变强组队训练赛第九场 G Lexical Sign Sequence 贪心+树状数组
问题 G: Lexical Sign Sequence 时间限制: 1 Sec 内存限制: 128 MB [提交] [状态] [命题人:admin] 题目描述 Andi likes numbers and sequences, especially, sign sequences. A sign sequence is a sequence which consists of -1 and 1. Andi is a curious person, thus,…...

Contest1818 - 2019年我能变强组队训练赛第十场 I: Isomorphic Inversion 字符串哈希
时间限制: 1 Sec 内存限制: 128 MB 题目描述 Let s be a given string of up to 106 digits. Find the maximal k for which it is possible to partition s into k consecutive contiguous substrings, such that the k parts form a palindrome. More precisely, we say…...
Contest1818 - 2019年我能变强组队训练赛第十场 问题 E: Eating Everything Efficiently 树形dp+记忆化
问题 E: Eating Everything Efficiently 时间限制: 1 Sec 内存限制: 128 MB Special Judge [提交] [状态] [命题人:admin] 题目描述 Margriet A. is in pizza heaven! She has bought a one-day access pass to Pizza World. Pizza World is a food festival, where all s…...

划分树 K-th Number POJ - 2104
划分树,类似线段树,主要用于求解某个区间的第k 大元素(时间复杂度log(n)) 讲解:https://www.cnblogs.com/hchlqlz-oj-mrj/p/5744308.html 大致就是分治递归的思想: int tree[22][N]; //对应每层每个位置…...
HDU - 3473 Minimum Sum 划分树
题目链接:https://cn.vjudge.net/problem/HDU-3473 题意:find a number x to make as small as possible! 题解:本来想三分x感觉会T,想了一下,当然x就是[l, r] 所有数的中位数了,至于为什么,可…...
问题 F: The King’s Ups and Downs dp
问题 F: The King’s Ups and Downs 时间限制: 1 Sec 内存限制: 128 MB 题目描述 The king has guards of all different heights. Rather than line them up in increasing or decreasing height order, he wants to line them up so each guard is either shorter than t…...

计算几何模板
二维几何: // 计算几何模板 //二维平面 using namespace std; const double eps 1e-8; const double inf 1e20; const double pi acos(-1.0); const int maxp 1010; //Compares a double to zero int sgn(double x){if(fabs(x) < eps)return 0;if(x < 0)…...

问题 E: Fruit Slicer n个圆求一条直线经过圆的数目最多
问题 E: Fruit Slicer 时间限制: 1 Sec 内存限制: 128 MB 提交: 25 解决: 4 [提交] [状态] [命题人:admin] 题目描述 John, a student who is taking the game development course, recently developed a mobile game called Fruit Slicer for his coursework. In the gam…...
问题 I: Monitoring Ski Paths 树链剖分+LCA+树状数组+贪心
问题 I: Monitoring Ski Paths 时间限制: 1 Sec 内存限制: 128 MB [提交] [状态] [命题人:admin] 题目描述 Fresh powder on a sunny day: it is a great time to ski! Hardcore skiers flock to a large mountain in the Rockies to enjoy these perfect conditions. The …...

POJ - 3580 SuperMemo 伸展树
题目链接:https://cn.vjudge.net/problem/POJ-3580#author0 伸展树学习: 很全:https://blog.csdn.net/qq_33184171/article/details/73549164 图:https://blog.csdn.net/wr132/article/details/50599747 效率:http…...

3224: Tyvj 1728 普通平衡树 伸展树
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id3224 题解:灵活运用,像这种不对区间操作的,相同的数放到一块就可以了 #include <bits/stdc.h> using namespace std; #define INF 2e9 10 const int N 1e5 …...

HDU - 1890 Robotic Sort 伸展树
题目链接:https://cn.vjudge.net/problem/HDU-1890 题意:每次将第i个位置到第i大的数所在位置 之间的数进行翻转,输出的是第i大的数所在的位置 题解:按照1-n建树,原序列排序,每次把节点 a[i].id 放到顶部…...

2019百度之星 - 复赛 HDU-6725 Diversity 树形dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid6725 题解:每个节点要么取l[i],要么取r[i],对于每个节点维护下取最小最大值时的结果即可 #include <bits/stdc.h> using namespace std; typedef long long ll; cons…...

2019百度之星 - 复赛 HDU-6726 Transformation 搜索剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid6726 题解:我们倒着往前找,对于(a,2b-a)->(x,y)那么xy为偶数,(x, y) 往前的话就是(x, (xy)/2)&…...

2019百度之星 - 复赛 HDU-6727 Quasi Binary Search Tree 构造 二叉树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid6727 题解:对于当前节点,肯定分为两个区间一个在左一个在右,就把小区间分到最小编号在的那个分支中,如果当前节点最小,那就尽可能让当前节点的标号小…...

2019 南京网络赛 A.The beautiful values of the palace 构造螺旋矩阵 + CDQ / 离线 + 树状数组
题目链接:https://nanti.jisuanke.com/t/41298 题解:首先我们要把每个坐标的val求出来,很明显每一圈的个数是一个等差数列,第x圈 a[x] -8x4n4,然后就是先等差前n项和求出之前每一圈的总个数,然后对于x 与…...

2018北京网络赛 HihoCoder - 1835 K-Dimensional Foil II 计算几何 贪心 二分
题目链接:https://vjudge.net/problem/HihoCoder-1835 题解:首先我们应该能想到到达图形的距离最近那肯定是垂直过去,也就是坐标变为(x1 - k, x2 - k, x3 - k...),假设图形原点的坐标为(x10, x20, x30 ...),如果某个x…...
2019南京网络赛 D.Robots 树上期望dp
题目链接:https://nanti.jisuanke.com/t/41301 题意:N个点M条边的有向无环图,1为唯一入度为0的点,N为唯一出度为0的点,现在你从1号点出发,每单位时间,你有相同的概率,走向下一节点或…...

bzoj1568: [JSOI2008]Blue Mary开公司 李超线段树
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id1568 李超线段树: 问题: 现在要求你在线动态维护一个二维平面直角坐标系,支持插入一条线段,询问与直线xx0相交的所有线段中交点y的最大/最小值 更新&a…...
牛客练习赛51 C、勾股定理 只一边求另外两边 结论
链接:https://ac.nowcoder.com/acm/contest/1083/C 来源:牛客网 勾股定理 时间限制:C/C 1秒,其他语言2秒 空间限制:C/C 32768K,其他语言65536K Special Judge, 64bit IO Format: %lld 题目描述 给出直…...

2019徐州网络赛 G Colorful String 马拉车+后缀
题目链接:https://nanti.jisuanke.com/t/41389 题解:马拉车处理回文串,每个位置记录后面每个字符出现的第一个位置,然后把每种字符额贡献加起来即可 #include<bits/stdc.h> using namespace std; typedef long long ll; c…...

2019徐州网络赛 I query 线段树 离线
题目链接:https://nanti.jisuanke.com/t/41391 题解:先找出每一对(pi,pj),那么i, j 记录为 区间[l, r],则查询就变为,在[L,R]内有多少区间,然后我们离线线段树就可以搞定了,按照…...

2019徐州网络赛 J、Random Access Iterator 树上 概率 dp
题目链接:https://nanti.jisuanke.com/t/41392 题解:dp[u] 表示 u这个点向下能到达最深深度的概率,因为对于每个点要找k次,只要其中有一次能到达最深即可,所以我们用容斥的思想,即表示为 1 - 不能到达最深…...

2019银川网络赛A? FZU - 2105 Digits Count 线段树 区间操作 | ^
题目链接:https://vjudge.net/problem/FZU-2105 其实我想放银川网络赛的链接,emmm 题解:首先或和与是能相互掩盖的,因此我们就可以把这两者用一个laz标记,laz1表示区间都变为1,laz-1表示区间都为0&#x…...

2019 徐州网络赛 F. Little M's attack plan 树上倍增+dfs序+二分
题目链接:https://nanti.jisuanke.com/t/41388 题解:这个题比赛最后也没改对,训练时突然想到有个地方写了emmmm 思路:我们先按照dfs序把每个点跑出来,然后vector记录下每个深度的点,很容易就可以想到&…...

HDU - 5489 Removed Interval LIS+枚举技巧
题目链接:https://vjudge.net/problem/HDU-5489 题解:删除m个后的最优的LIS在原序列中肯定存在一段间距大于等于m,那么我们就以这一段作为划分线,以后面第一个数x开始作为后半部分,那么后半部分的LIS我们可以倒着nlo…...

2019 南昌网络赛 C Hello 2019 / CodeForces - 750ENew Year and Old Subsequence 线段树维护矩阵
题目链接:https://vjudge.net/problem/CodeForces-750E 题解:哇,原来这个也四个原题。。。。。。维护矩阵的还真是第一次做,这里就说一下CF那题的思路吧 因为最后要2017,那么我们分为5个子序列 空 2 20 201 2017 每…...

构造螺旋矩阵 n*m
构造n * m的螺旋矩阵 讲解:https://wenku.baidu.com/view/7253dc78a26925c52cc5bff4.htm #include <bits/stdc.h> using namespace std; int getnum(int n, int m, int x, int y) {int lev min(min(x, n - 1 - x), min(y, m - 1 - y));int d x y - lev *…...

2019 上海网络赛 B、 Light bulbs 区间差分 / 线段树点维护区间
题目链接:https://nanti.jisuanke.com/t/41399 题解:看见的第一思路应该都是差分,但是这个用数组肯定就gg了,所以当时的时候就想到动态开点线段树,mlog(n)但是T了,然后我就把点离散了一下,用点…...

2019 上海网络赛 G、Substring 字符串哈希
题目链接:https://nanti.jisuanke.com/t/41415 题解:对于两边的,要哈希出相应的权重,中间的字符顺序无所谓,只关心是什么字符,所以哈希出每个字符的权重即可。 官方题解: • 但是因为询问有2…...

2019 沈阳网络赛 H. Texas hold'em Poker 模拟
题目链接:https://nanti.jisuanke.com/t/41408 细心细心细心 #include <bits/stdc.h> using namespace std; const int N 1e5 10; struct node {char name[15];int id;int b[5]; }p[N]; int n; int a[10]; bool judge1() {for(int i 1; i < 5; i) {if…...

2019 沈阳网络赛 B. Dudu's maze dfs 联通块选取
题目链接:https://nanti.jisuanke.com/t/41402 题解:首先和1联通的糖果屋都能到达,其他不能到达的肯定被恶魔屋挡住了,那就在挡住的所以恶魔屋中选择一个最优的就可以了,怎么算最优呢,因为到达恶魔屋之后&…...

51nod 1571 最近等对 CQD分治
题目链接:https://www.51nod.com/Challenge/Problem.html#problemId1571 现在有一个序列 a1, a2, ..., ana1, a2, ..., an ,还有m个查询 lj, rj (1 ≤ lj ≤ rj ≤ n)lj, rj (1 ≤ lj ≤ rj ≤ n) 。对于每一个查询,请找出距离最近的两个元素…...

牛客 Fruit Ninja 随机大法
链接:https://ac.nowcoder.com/acm/contest/163/A 来源:牛客网 题目描述 Fruit Ninja is a juicy action game enjoyed by millions of players around the world, with squishy, splat and satisfying fruit carnage! Become the ultimate bringer of…...

HDU - 6242 Geometry Problem 随机数
题目链接:https://vjudge.net/problem/HDU-6242 题意:n个点,找到一个圆,满足圆上点的数目大于等于⌈N / 2⌉ 题解:三个不在一条直线上的点确定一个圆,随机到圆上点的概率是1 / 2,那么三次就是…...

HDU - 6393 Traffic Network in Numazu 树上倍增求lca+dfs序+线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid6393 题意:n个点n条边的图,求两点间的最小距离 题解:如果是一个树的话,那么我们就可以倍增dfs序,然后用线段树维护下就可以了这样是nlogn的复杂度&a…...

CodeForces - 1220D Alex and Julian 二分图性质
题目链接:https://vjudge.net/problem/CodeForces-1220D 题意:n个数,如果节点 i 和 j 满足 |i - j | 属于B,则i和j有一条边,问B集合最少删除多少边,使得得到的图是个二分图 题解:根据二分图的…...

CodeForces - 1220E Tourism 无向图缩点+树形dp
题目链接:https://vjudge.net/problem/CodeForces-1220E 题意:n个点m条边的无向连通图,起点s,每个点有权值,求遍历无向图得到的最大权值和。但是不能走回头路,即如果从U走到V那么下一步不可以从V走到U 题…...

HDU - 6156 Palindrome Function 数位dp
题目链接:https://vjudge.net/problem/HDU-6156 题意:区间[L, R]的数,写成k进制(l<k<r)后,如果是回文串,那么f(x,k)k,否则f(x,k)1,对f求和 题解:一看这种题就是数位dp,当时想…...

HDU - 6153 A Secret 扩展kmp
题目链接:https://vjudge.net/problem/HDU-6153 题意:对于s2的每一个后缀,假设长度为l,在s1出现的次数为k,求l*k的和 题解:我们把两个串都倒过来,变为s1,s2,那么问题就变为&#x…...

Gym - 100589A Queries on the Tree 树状数组+分块
题目链接:https://vjudge.net/problem/Gym-100589A 题意:n个点,根节点为1的树,两种操作,1 L y 与根节点距离为L的节点权值全部加上y,2 x x子树的权值总和 题解:对于更新操作,因为更…...

HDU-6731 Angle Beats 计算几何 直角三角形个数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid6731 题意:n个点,m次查询,每次给你一个点,从n个点挑出两个能组成多少直角三角形 题解:假设询问的是点p,直角可能是p也可能是其他两点的某…...