1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| #include<bits/stdc++.h> #define int long long #define ull unsigned long long #define maxn 100005 #define put() putchar('\n') #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> using namespace std; inline void read(int &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; struct yyy{ __int128 sum; int ls,rs,siz,rnd,lazy,val; }a[maxn]; int root,cnt; mt19937 rnd(time(0)); inline int clone(int val) { ++cnt; a[cnt].ls=a[cnt].rs=0;a[cnt].rnd=rnd(); a[cnt].siz=1;a[cnt].lazy=0; a[cnt].val=a[cnt].sum=val; return cnt; } inline void pushup(int k) { a[k].sum=a[a[k].ls].sum+a[a[k].rs].sum+a[k].val; a[k].siz=a[a[k].ls].siz+a[a[k].rs].siz+1; } inline void cover(int x,int k) { a[x].lazy+=k; a[x].sum+=k*a[x].siz; a[x].val+=k; } inline void pushdown(int k) { if (a[k].lazy) { if (a[k].ls) cover(a[k].ls,a[k].lazy); if (a[k].rs) cover(a[k].rs,a[k].lazy); a[k].lazy=0; } } inline void split(int k,int x,int &tmp1,int &tmp2) { if (!k) return tmp1=tmp2=0,void(); pushdown(k); if (a[a[k].ls].siz+1<x) { split(a[k].rs,x-a[a[k].ls].siz-1,a[k].rs,tmp2); pushup(k);tmp1=k; } else { split(a[k].ls,x,tmp1,a[k].ls); pushup(k);tmp2=k; } } inline int merge(int x,int y) { if (!x||!y) return x+y; if (a[x].rnd<a[y].rnd) { pushdown(x); a[x].rs=merge(a[x].rs,y); pushup(x);return x; } else { pushdown(y); a[y].ls=merge(x,a[y].ls); pushup(y);return y; } } inline int query(int k) { int x=(a[k].sum+1)/2,sum=0; while (k) { pushdown(k); if (x<=a[a[k].ls].sum) k=a[k].ls; else if (x<=a[a[k].ls].sum+a[k].val) return sum+1+a[a[k].ls].siz; else x=x-a[a[k].ls].sum-a[k].val,sum+=a[a[k].ls].siz+1,k=a[k].rs; } return 0; } int n,m; int p[maxn],w[maxn]; inline void print(int k) { if (!k) return ; pushdown(k); assert(a[k].sum==a[a[k].ls].sum+a[a[k].rs].sum+a[k].val); print(a[k].ls); printf("%lld ",a[k].val); print(a[k].rs); } signed main(void){ freopen("dove.in","r",stdin); freopen("dove.out","w",stdout); char ch;int l,r,s,i,sum=0; int tmp1,tmp2,tmp3,tmp4; read(n);read(m); for (i=1;i<=n;i++) read(w[i]),root=merge(root,clone(w[i])),sum+=w[i]; for (i=1;i<=n;i++) read(p[i]); printf("%lld\n",p[query(root)]); while (m--) { ch=getchar();while (ch!='A'&&ch!='B') ch=getchar(); if (ch=='A') { read(l);read(r);read(s); split(root,l,tmp1,tmp2); split(tmp2,r-l+2,tmp2,tmp3); cover(tmp2,s); root=merge(tmp1,merge(tmp2,tmp3)); } else { read(l);read(r); if (l<r) { split(root,l,tmp1,tmp2); split(tmp2,2,tmp2,tmp3); split(tmp3,r-l+1,tmp3,tmp4); root=merge(merge(tmp1,tmp3),merge(tmp2,tmp4)); } else { swap(l,r); split(root,l,tmp1,tmp2); split(tmp2,r-l+1,tmp2,tmp3); split(tmp3,2,tmp3,tmp4); root=merge(merge(tmp1,tmp3),merge(tmp2,tmp4)); } } printf("%lld\n",p[query(root)]); } return 0; }
|