zoj 2112

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N =50000+10;
const int maxn=N*18;
int tot;
int T[N],A[N],t[N],s[N];
int lson[maxn],rson[maxn],c[maxn];
int n,q,m;
void init()
{
sort(t+1,t+1+n);
m=unique(t+1,t+1+n)-t-1;
}
void build(int l,int r)
{
int rt=tot++;
c[rt]=0;
if (l>=r) return;
int mid=(l+r)>>1;
lson[rt]=build(l,mid);
rson[rt]=build(mid+1,r);
return rt;
}
int inserts(int rt,int loc,int val)
{
int newrt=tot++,tmp=newrt;
int l=1,r=m;
c[newrt]=c[rt]+val;
while (l<r)
{
int mid=(l+r)>>1;
if (loc<=mid){
lson[newrt]=tot++;
rson[newrt]=rson[rt];
rt=lson[rt];
newrt=lson[newrt];
r=mid;
}
else{
rson[newrt]=tot++;
lson[newrt]=lson[rt];
rt=rson[rt];
newrt=rson[newrt];
l=mid+1;
}
c[newrt]=c[rt]+val;
}
return tmp;
}
void modify(int loc,int p,int val)
{
while (loc<=n)
{
s[loc]=inserts(s[loc],loc,val);
loc+=loc&(-loc);
}
}
int main()
{
char op[5];
int t,a,b,k;
scanf("%d",&t);
while (t--)
{
tot=0;
scanf("%d%d",&n,&q);
for (int i=1;i<=n;i++) scanf("%d",&A[i]),t[i]=A[i];
init();
T[0]=build(0,m-1);
for (int i=0;i<=n;i++) s[i]=0;
for (int i=1;i<=n;i++){
int pos=lower_bound(t+1,t+m+1,A[i])-t;
T[i]=inserts(T[i-1],pos,1);
}
while (q--)
{
scanf("%s",op);
if (op[0]==‘Q‘){
scanf("%d%d%d",&a,&b,&k);
}
else{
scanf("%d%d",&a,&b);
int pos;
pos=lower_bound(t+1,t+1+n,A[a])-t;
modify(a,pos,-1);
pos=lower_bound(t+1,t+1+n,A[b])-t;
modify(a,pos,1);
A[a]=b;
}
}
}
}

zoj 2112

时间: 2024-10-09 07:34:39

zoj 2112的相关文章

ZOJ 2112 Dynamic Rankings(主席树の动态kth)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. T

ZOJ 2112 Dynamic Rankings(带修改的区间第K大,分块+二分搜索+二分答案)

Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They

zoj 2112 Dynamic Rankings(主席树&amp;动态第k大)

Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They

ZOJ -2112 Dynamic Rankings 主席树 待修改的区间第K大

Dynamic Rankings 带修改的区间第K大其实就是先和静态区间第K大的操作一样.先建立一颗主席树, 然后再在树状数组的每一个节点开线段树(其实也是主席树,共用节点), 每次修改的时候都按照树状数组的方式去修改,并且修改那些地方.查询的时候就是查询原主席树+树状数组的值. 代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt","r&quo

zoj 2112 Dynamic Rankings 动态第k大 线段树套Treap

Dynamic Rankings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 Description The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query l

ZOJ 2112 Dynamic Rankings(主席树套树状数组+静态主席树)

题意:给定一个区间,求这个区间第k大的数,支持单点修改. 思路:主席树真是个神奇的东西.........速度很快但是也有一个问题就是占用内存的很大,一般来说支持单点修改的主席树套树状数组空间复杂度为O(n*logn*logn), 如果查询较少的话,可以初始的时候用一颗静态主席树,这样空间复杂度可以降为O(n*logn+q*logn*logn),勉强可以过zoj这道题. 这道题看了好久好久才懂...网上题解一般就几句话.......下面是自己的一些理解. 首先静态主席树这个东西其实比较好懂,就是对

zoj 2112 Dynamic Rankings

bzoj1901 1 #include<bits/stdc++.h> 2 #define N 200005 3 #define LL long long 4 #define inf 0x3f3f3f3f 5 using namespace std; 6 inline int ra() 7 { 8 int x=0,f=1; char ch=getchar(); 9 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}

zoj 2112 块状链表求动态第k大

块内排序方便二分,然后进行类似于冒泡排序的更新即可. 1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 using namespace std; 7 8 const int INF = 1000000000; 9 const int N = 50000; 10 const int M

●ZOJ 2112 Dynamic Rankings

●赘述题目 对于一个长为n(n<50000)的序列(序列中的数小于1000000000),现有如下两种指令: Q a b c:询问区间[a,b]中第c小的数. C p b:将序列中的从左往右数第p个数改成b. ●题解 (整体二分应该可以做吧...但写不来了) 主席树+树状数组套线段树维护. 本题和POJ 2104 K-th Number相比,多了一个修改操作,但真的做得我心累. 看看POJ 2104 的查询函数: 查询区间到底是往左还是往右,这决于tmp与k大小关系.但本题因为有修改操作,导致上