(/2和>>1不一样!!)
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector> #if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#define INF 1000000007
#define MAXN 200010
using namespace std;
struct Node {
int x,y,a,b,c;
}q[MAXN],a[MAXN],b[MAXN];
int t[MAXN],ans[MAXN],n,m,x,y,z,cnt,k;
inline int read() {
char ch;
bool f=false;
int res=0;
while (((ch=getchar())<'0'||ch>'9')&&ch!='-');
if (ch=='-')
f=true;
else
res=ch-'0';
while ((ch=getchar())>='0'&&ch<='9')
res=(res<<3)+(res<<1)+ch-'0';
return f?~res+1:res;
}
inline int lowbit(int x) {
return x&(-x);
}
inline void add(int x,int y) {
while (x<=n) {
t[x]+=y,x+=lowbit(x);
}
}
inline int sum(int x) {
int summ=0;
while (x>0) {
summ+=t[x],x-=lowbit(x);
}
return summ;
}
inline void Build(int x,int i){
cnt++;
q[cnt].x=x,q[cnt].b=1,q[cnt].c=i;
}
inline void Build1(int x,int y,int k,int i){
cnt++;
q[cnt].x=x,q[cnt].y=y,q[cnt].a=k,q[cnt].b=2,q[cnt].c=i;
}
void sc(int t,int w,int l,int r) {
if (t>=w)
return;
if (l==r) {
for (int i=t;i<=w;++i)
if (q[i].b==2)
ans[q[i].c]=l;
return;
}
int mid=(l+r)>>1,t1=0,w1=0;
for (int i=t;i<=w;++i)
if (q[i].b==1) {
if (q[i].x<=mid)
add(q[i].c,1),a[++t1]=q[i];
else
b[++w1]=q[i];
}
else {
int tw=sum(q[i].y)-sum(q[i].x-1);
if (tw>=q[i].a)
a[++t1]=q[i];
else {
q[i].a=q[i].a-tw;
b[++w1]=q[i];
}
}
for (int i=1;i<=t1;++i)
if (a[i].b==1)
add(a[i].c,-1);
for (int i=1;i<=t1;++i)
q[t+i-1]=a[i];
for (int i=1;i<=w1;++i)
q[t+t1+i-1]=b[i];
sc(t,t+t1-1,l,mid);
sc(t+t1,w,mid+1,r);
}
int main() {
n=read(),m=read();
for (int i=1;i<=n;++i) {
x=read();
Build(x,i);
}
for (int i=1;i<=m;++i) {
x=read(),y=read(),k=read();
Build1(x,y,k,i);
}
sc(1,cnt,-INF,INF);
for (int i=1;i<=m;++i)
printf("%d\n",ans[i]);
return 0;
}
ZOJ 2112 Dynamic Rankings
#include <algorithm>
#include <bitset>
#include <complex>
#include<cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector> #if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#define INF 1000000007
#define MAXN 2000010
using namespace std;
struct Node {
int x,y,a,b,c;
}q[MAXN],a[MAXN],b[MAXN];
int t[MAXN],ans[MAXN],n,m,x,y,z,cnt,k;
int aa[MAXN];
char ch;
int ansn;
inline int read() {
char ch;
bool f=false;
int res=0;
while (((ch=getchar())<'0'||ch>'9')&&ch!='-');
if (ch=='-')
f=true;
else
res=ch-'0';
while ((ch=getchar())>='0'&&ch<='9')
res=(res<<3)+(res<<1)+ch-'0';
return f?~res+1:res;
}
inline int lowbit(int x) {
return x&(-x);
}
inline void add(int x,int y) {
while (x<=n) {
t[x]+=y,x+=lowbit(x);
}
}
inline int sum(int x) {
int summ=0;
while (x>0) {
summ+=t[x],x-=lowbit(x);
}
return summ;
}
inline void Build(int x,int i) {
cnt++;
q[cnt].x=x,q[cnt].b=1,q[cnt].c=i;
}
inline void Build1(int x,int y,int k,int i) {
cnt++,ansn++;
q[cnt].x=x,q[cnt].y=y,q[cnt].a=k,q[cnt].b=0,q[cnt].c=ansn;
}
inline void Build2(int x,int y,int i) {
cnt++;
q[cnt].x=aa[x],q[cnt].b=-1,q[cnt].c=x;
}
inline void Build3(int x,int y,int i) {
cnt++;
q[cnt].x=aa[x],q[cnt].b=1,q[cnt].c=x;
}
void sc(int t,int w,int l,int r) {
if (t>w)
return;
if (l==r) {
for (int i=t;i<=w;++i)
if (q[i].b==0)
ans[q[i].c]=l;
return;
}
int mid=(l+r)>>1,t1=0,w1=0;
for (int i=t;i<=w;++i)
if (q[i].b) {
if (q[i].x<=mid)
/*add(q[i].c,1),*/add(q[i].c,q[i].b),a[++t1]=q[i];
else
b[++w1]=q[i];
}
else {
int tw=sum(q[i].y)-sum(q[i].x-1);
if (tw>=q[i].a)
a[++t1]=q[i];
else {
q[i].a=q[i].a-tw;
b[++w1]=q[i];
}
}
for (int i=1;i<=t1;++i)
if (a[i].b)
add(a[i].c,-a[i].b);
for (int i=1;i<=t1;++i)
q[t+i-1]=a[i];
/*for (int i=1;i<=t1;++i)
printf("%d %d %d %d %d ",a[i].x,a[i].y,a[i].a,a[i].b,a[i].c);
printf("\n");*/
for (int i=1;i<=w1;++i)
q[t+t1+i-1]=b[i];
sc(t,t+t1-1,l,mid);
sc(t+t1,w,mid+1,r);
}
int main() {
int T=read();
while (T--){
memset(q,0,sizeof q);
memset(a,0,sizeof a);
memset(b,0,sizeof b);
memset(ans,0,sizeof ans);
n=read(),m=read(),cnt=0,ansn=0;
for (int i=1;i<=n;++i) {
aa[i]=read();
Build(aa[i],i);
}
for (int i=1;i<=m;++i) {
/*scanf("%c",&ch);*/cin>>ch;x=read(),y=read();
if (ch=='Q'){
k=read();
Build1(x,y,k,i);
}
else {
Build2(x,y,i);
aa[x]=y;
Build3(x,y,i);
}
}
//for (int i=1;i<=cnt;++i)
// printf("%d %d %d %d %d\n",q[i].x,q[i].y,q[i].a,q[i].b,q[i].c);
sc(1,cnt,-INF,INF);
for (int i=1;i<=ansn;++i)
printf("%d\n",ans[i]);
}
return 0;
}