模板题目:ZJOI2013 K大数查询原题是带插入区间第K大
注释足够看→_←
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 50010
#define MAXINT 0x7fffffff
#define lchild rt<<1,l,mid
#define rchild rt<<1|1,mid+1,r
#define ln rt<<1
#define rn rt<<1|1
using namespace std;
int op,a,b,c;
int n,m;
struct query
{
int x;
int op,a,b,c;
int fin;//操作是否得到解决
bool operator < (const query& a)const{
if (fin!=a.fin) return fin<a.fin;
return x<a.x;
}
}ques[MAXN];//询问序列
int ans[MAXN];//答案序列,如果对应询问是插入,ans为-MAXINT
struct seg
{
int l,r;
int mem;//清空标记
int flag;//操作标记
int add;//加量标记
int sum;
}tree[MAXN<<4];
bool comp(query a,query b)
{
if (a.fin<b.fin) return 1;
if (a.fin==b.fin) a.x<b.x;
return 0;
}
void build(int rt=1,int l=1,int r=n)
{
int mid=(l+r)>>1;
tree[rt].l=l;tree[rt].r=r;
if (l==r) return;
build(lchild);build(rchild);
}
void push_up(int rt)
{
tree[rt].sum=tree[ln].sum+tree[rn].sum;
}
void push_down(int rt)
{
if (tree[rt].mem)
{
tree[ln].add=tree[rn].add=tree[ln].sum=tree[rn].sum=0;
tree[ln].mem=tree[rn].mem=1;
tree[rt].mem=0;
}
if (tree[rt].add)
{
tree[ln].add+=tree[rt].add;tree[rn].add+=tree[rt].add;
tree[ln].sum+=(tree[ln].r-tree[ln].l+1)*tree[rt].add;
tree[rn].sum+=(tree[rn].r-tree[rn].l+1)*tree[rt].add;
tree[rt].add=0;
}
}
void modify(int rt,int l,int r)
{
push_down(rt);
int L=tree[rt].l,R=tree[rt].r;
if (L==l&&R==r)
{
tree[rt].add++;
tree[rt].sum+=(R-L+1);
return;
}
int mid=(L+R)>>1;
if (r<=mid) modify(ln,l,r);
else
if (l>mid) modify(rn,l,r);
else modify(ln,l,mid),modify(rn,mid+1,r);
push_up(rt);
}
int query(int rt,int l,int r)
{
push_down(rt);
int L=tree[rt].l,R=tree[rt].r;
if (L==l&&R==r) return tree[rt].sum;
int mid=(L+R)>>1;
if (r<=mid) return query(ln,l,r);
else
if (l>mid) return query(rn,l,r);
else return query(ln,l,mid)+query(rn,mid+1,r);
}
void solve(int L,int R,int l,int r)//L,R是数据序列,l,r是询问序列
{
if (l>r) return;
if (L==R)//答案get
{
for (int i=l;i<=r;i++)
if (ques[i].op==2) ans[ques[i].x]=L;
return;
}
tree[1].add=tree[1].sum=0;tree[1].mem=1;
int Mid=l-1;//用于确定下一次划分询问区间的端点
int mid=(L+R)>>1;
for (int i=l;i<=r;i++)
{
if (ques[i].op==1)
{
if (ques[i].c>mid)
modify(1,ques[i].a,ques[i].b),ques[i].fin=1;
else
ques[i].fin=0,Mid++;
}
else
{
int temp=query(1,ques[i].a,ques[i].b);
if (ques[i].c<=temp) ques[i].fin=1;
else ques[i].fin=0,Mid++,ques[i].c-=temp;
}
}
sort(ques+l,ques+r+1);
solve(L,mid,l,Mid);
solve(mid+1,R,Mid+1,r);
}
int main()
{
scanf("%d%d",&n,&m);
build();
for (int i=1;i<=MAXN;i++) ans[i]=-MAXINT;
for (int i=1;i<=m;i++)
{
scanf("%d%d%d%d",&op,&a,&b,&c);
ques[i].x=i;ques[i].op=op;ques[i].a=a;ques[i].b=b;ques[i].c=c;
}
solve(0,n,1,m);
for (int i=1;i<=m;i++)
if (ans[i]!=-MAXINT) printf("%d\n",ans[i]);
}
时间: 2024-10-18 09:04:26