这才是更一般的二分写法--HDU5412

 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr strcat
#include <string>
#include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
#include <cassert>
#include <iomanip>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
clock_t __START,__END;
double __TOTALTIME;
void _MS(){__START=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);//
const ll INF=(1LL<<);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)4e6+; int ans[N],a[N];
int c[N],maxn;
int lowbit(int n){return n&(-n);}
void add(int i,int t)
{
while(i<=maxn)
{
c[i]+=t;
i+=lowbit(i);
}
}
int Sum(int i)
{
int sum=;
while(i)
{
sum+=c[i];
i-=lowbit(i);
}
return sum;
}
struct node
{
int l,r,k,id,currk,op;
}q[N],temp[N];
void Init()
{
for(int i=;i<=N-;++i)
c[i]=;
} void solve(int l,int r,int ql,int qr)
{
if(ql>qr)return;
int L=ql,R=qr,mid=l+r>>; if(l==r)
{
for(int i=ql;i<=qr;++i)
if(q[i].op==)
ans[q[i].id]=l;//答案
return;
}
for(int i=ql;i<=qr;++i)
{
if(q[i].op!=)temp[q[i].r<=mid?L++:R--]=q[i];//分位置 if(q[i].op==&&q[i].r<=mid)add(q[i].l,);//q.l才是要加的位置,q.r是值
else if(q[i].op==&&q[i].r<=mid)add(q[i].l,-);
else if(q[i].op==)
{
int sum=Sum(q[i].r)-Sum(q[i].l-);//二分是保证按时间顺序来的,直接计算就行
if(q[i].currk+sum>=q[i].k)temp[L++]=q[i];//分位置
else q[i].currk+=sum,temp[R--]=q[i];
}
}
for(int i=ql;i<=qr;++i)//清空-树状数组
{
if(q[i].op==&&q[i].r<=mid)add(q[i].l,-);
else if(q[i].op==&&q[i].r<=mid)add(q[i].l,);
}
//还原
for(int i=ql;i<=L-;++i)
q[i]=temp[i];
for(int i=qr,j=L;j<=qr;--i,++j)
q[j]=temp[i]; //递归下去
if(L>ql)solve(l,mid,ql,L-);
if(R<qr)solve(mid+,r,R+,qr);
} int main()
{
int n,m;
while(~sc("%d",&n))
{
// Init();
maxn=n;
for(int i=;i<=n;++i)
sc("%d",&q[i].r),q[i].l=i,a[i]=q[i].r,q[i].op=;
sc("%d",&m);
int ask=;
for(int i=;i<=m;++i)
{
int J;
sc("%d",&J);
if(J==)
{
int pos,x;
sc("%d%d",&pos,&x);
++n;
q[n].l=pos,q[n].r=a[pos],q[n].op=;//del
++n,a[pos]=x;
q[n].l=pos,q[n].r=a[pos],q[n].op=;//add
}
else
++n,sc("%d%d%d",&q[n].l,&q[n].r,&q[n].k),q[n].currk=,q[n].id=++ask,q[n].op=;//query
}
solve(,inf,,n);
for(int i=;i<=ask;++i)
pr("%d\n",ans[i]);
}
return ;
} /**************************************************************************************/

整体二分(模板二)动态区间第K大的更多相关文章

  1. 整体二分(模板) 求区间第k小

    整体二分,将询问与初值一起放入一个结构体里,然后每次二分判断询问在哪边,树状数组维护,时间复杂度O((n+Q)lognlogMAX_a[i] 代码 #include<iostream> # ...

  2. ZOJ 1112 Dynamic Rankings【动态区间第K大,整体二分】

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112 题意: 求动态区间第K大. 分析: 把修改操作看成删除与增加 ...

  3. 整体二分初探 两类区间第K大问题 poj2104 & hdu5412

    看到好多讲解都把整体二分和$CDQ$分治放到一起讲 不过自己目前还没学会$CDQ$分治 就单独谈谈整体二分好了 先推荐一下$XHR$的 <浅谈数据结构题的几个非经典解法> 整体二分在当中有 ...

  4. hdu5412(动态区间第k大)

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  5. ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)

    题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...

  6. 动态区间第K大

    整体二分. 主要需要注意的一点是,对于每个删除操作,若删除操作被算入贡献,则最开始的插入操作也一定会被算入,所以不必担心删除删错. #include<cstdio> #include< ...

  7. ZOJ2112--Dynamic Rankings (动态区间第k大)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  8. 整体二分求动态区间第k大

    比树状数组套主席树不知道高到哪里去了,solve(l,r,L,R)就是对于L,R的操作区间的答案都在l,r区间里,然后递归下去 复杂度O(nlognlogn),每个操作会执行logn次就是o(nlog ...

  9. Dynamic_Rankings(动态区间第k大)

    ZOJ - 2112 \[ \ \] (那些说这道题是树状数组套主席树的人一定对主席树有误解!) 这里我们用树状数组套线段树来解决来写 首先 , 我们需要有n棵线段树(不是\(n^2\)空间,别慌) ...

随机推荐

  1. 数据结构实验之栈与队列六:下一较大值(二)(SDUT 3333)

    #include <bits/stdc++.h> using namespace std; int a[1000006]; int b[1000006]; int sta[100006]; ...

  2. 数据结构实验之栈与队列三:后缀式求值(SDUT 2133)

    题解:把每一步计算的答案再存在栈里面,直到计算结束. 如果是操作数 那么直接入栈:如果是运算符,那么把栈里面最顶部的两个操作数拿出来进行运算,运算结果再放入到栈里面,计算完所有的(#之前的长度位len ...

  3. 省市县数据(txt)

    下载地址:https://pan.baidu.com/s/1x1uR9KxzlxAbn5bgN6ZfrQ 提取码:ky8z

  4. hbase部署经验与坑总结

    1.本地单机部署hbase,想要使用独立zookeeper,不使用自带的 vim conf/hbase-env.sh export HBASE_MANAGES_ZK=false 设置不使用自带zook ...

  5. 《你不知道的JavaScript(上)》笔记——词法作用域

    词法作用域是一套关于引擎如何寻找变量以及会在何处找到变量的规则. 词法作用域最重要的特征是它的定义过程发生在代码的书写阶段(假设你没有使用eval() 或 with) 欺骗词法:指修改词法作用域, 欺 ...

  6. Hexo博客skapp主题部署填坑指南

    相信大家都很喜欢 hexo skapp 的主题,由于作者采用结巴分词,加上需要依赖各种各样的环境 所以可能大家踩过很多坑,也许每个人踩得坑不一样,这里使用 Docker 容器 centos 来部署, ...

  7. fastjson在将Map<Integer, String>转换成JSON字符串时,出现中文乱码问题

    fastjson在将Map<Integer, String>转换成JSON字符串时,出现中文乱码问题. 先记下这个坑,改天在看看是怎么导致的,暂时通过避免使用Integer作为键(使用St ...

  8. smart_pointer example

    #pragma oncetemplate<typename T>class smart_pointer{private: T* m_pRawPointer;public: smart_po ...

  9. postgreSQL 之 Privilege & grant & revoke(未完待续)

    When an object is created, it is assigned an owner. The owner is normally the role that executed the ...

  10. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_24-页面预览-页面预览测试

    通过nginx转发到预览的地址 重启nginx 添加页面预览按钮 调整下列表的列的宽度