【ZJOI2013 DAY1】K大数查询 --树套树水题
Description 时限:2s? Input 第一行两个数n,m。意义如题目描述。? Output 对于每个询问回答k 大数是多少。 Sample Input
2 5
1 1 2 1
1 1 2 2
2 1 1 2
2 1 1 1
2 1 2 3
Sample Output
1
2
1
Hint 【样例说明】? 【分析】 树套树 外层权值树,内层区间树。 这样外层单点修改,内层区间修改比较方便好写。 注意点要动态构建,然后就很水了~ 【代码】
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <queue> using namespace std; #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define rrep(i,b,a) for(int i=(b);i>=(a);--i) #define INF (~0U>>3) #define sf scanf #define pf printf #define ls (id<<1) #define rs ((id<<1)|1) #define Mid ((Left+Right)>>1) #define MAXN 50010 int N,M; int p,q; struct SegNode{ #define node SegNode node* lc; int sz; int lazy; node* rc; node(bool mark){ lc=rc=NULL; sz=lazy=0; } node(){} }; node* null=new node(true); node Pool[MAXN*400]; node* New=Pool; node* root[MAXN<<2]; inline node* Get(){ New->lc=New->rc=null; New->sz=New->lazy=0; return New++; } inline void Read(int& x,bool mark=0){ char tt=getchar(); for(;tt<'0'||'9'<tt;tt=getchar()) if(tt=='-') mark=1; for(x=0;'0'<=tt&&tt<='9';x=(x<<1)+(x<<3)+tt-'0',tt=getchar()); x=mark?-x:x; } inline void Init(){ Read(N); Read(M); } inline void Insert(node* T,int Left,int Right){ if(p<=Left&&Right<=q){ T->sz+=(Right+1-Left); T->lazy++; return; } T->sz+=min(Right,q)+1-max(Left,p); if(T->lc==null) T->lc=Get(); if(T->rc==null) T->rc=Get(); if(!(q<Left||Mid<p)) Insert(T->lc,Left,Mid); if(!(q<Mid+1||Right<p)) Insert(T->rc,Mid+1,Right); } inline void Insert(int id,int Right,int loc){ if(!root[id]) root[id]=Get(); Insert(root[id],1,N); if(Left==Right) return; else if(loc<=Mid) Insert(ls,Mid,loc); else Insert(rs,Right,loc); } inline int Getsum(node* T,int Right){ if(T==null) return 0; if(p<=Left&&Right<=q) return T->sz; int rt=(min(Right,p))*T->lazy; if(!(q<Left||Mid<p)) rt+=Getsum(T->lc,Mid); if(!(q<Mid+1||Right<p)) rt+=Getsum(T->rc,Right); return rt; } inline int Select(int id,int k){ while(Left!=Right){ int t=0; if(root[rs]) t=Getsum(root[rs],N); if(k<=t){ id=rs;Left=Mid+1; } else{ id=ls;Right=Mid; k-=t; } } return Left; } inline void Solve(){ int T,c; while(M--){ Read(T); Read(p); Read(q); Read(c); if(T==1) Insert(1,N,c); else pf("%dn",Select(1,c)); } } int main(){ null->lc=null; null->rc=null; Init(); Solve(); return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- delphi – 我需要将“继承”行添加到记录构造函数中吗?
- spring – 在jsp中迭代PagedListHolder
- Delphi 字符串与 Windows PChar字符串
- Delphi 10 Seattle,C++ Builder 10 Seattle,RAD Studio 10
- SoapUI + Groovy 接口自动化
- 【poj 2429】GCD & LCM Inverse (Miller-Rabin素数测试
- java – 继承类的Spring MVC验证
- 在支持接口的Delphi 3中有什么相同之处?
- Delphi XE2 64位在字符串例程中运行时性能非常慢
- VB.NET复制删除文件