#include<bits/stdc++.h>
#define pn putchar('\n')
#define ps putchar(' ')
#define rec(x) scanf(" %c",&(x))
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define In(x) freopen(x".in","r",stdin)
#define Out(x) freopen(x".ans","w",stdout)
#define File(x) (In(x),Out(x))
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?++x:--x))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/trie_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/rope>
using namespace __gnu_cxx;
using namespace __gnu_pbds;
*/
using namespace std;
typedef long long ll;
template <typename T> void re(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t=((t<<3)+(t<<1)))+=(ch^48); ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
inline void wr(ll x){
if(x<0) putchar('-'),x=-x;
if(x>9) wr(x/10);
putchar(x%10+'0');
}
int main(){
return 0;
}
// rp++ Chtholly bless me!
// If happiness has a color, it must be the blue dyed by the last red.
// What do you do at the end tf the world? Are you busy? Will you save us?
// May you grant me good fortune and brighter days.
日用
#include<bits/stdc++.h>
#define pn putchar('\n')
#define ps putchar(' ')
#define rec(x) scanf(" %c",&(x))
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define In(x) freopen(x".in","r",stdin)
#define Out(x) freopen(x".ans","w",stdout)
#define File(x) (In(x),Out(x))
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/trie_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/rope>
using namespace __gnu_cxx;
using namespace __gnu_pbds;
*/
using namespace std;
typedef long long ll;
template <typename T> void re(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t=((t<<3)+(t<<1)))+=(ch^48); ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
inline void wr(ll x){
if(x<0) putchar('-'),x=-x;
if(x>9) wr(x/10);
putchar(x%10+'0');
}
int main(){
return 0;
}
// rp++ Chtholly bless me!
// If happiness has a color, it must be the blue dyed by the last red.
// What do you do at the end tf the world? Are you busy? Will you save us?
// May you grant me good fortune and brighter days.
赛用
#include<bits/stdc++.h>
#define pn putchar('\n')
#define ps putchar(' ')
#define rec(x) scanf(" %c",&(x))
using namespace std;
typedef long long ll;
template <typename T> void re(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t=((t<<3)+(t<<1)))+=(ch^48); ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
inline void wr(ll x){
if(x<0) putchar('-'),x=-x;
if(x>9) wr(x/10);
putchar(x%10+'0');
}
int main(){
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef __uint128_t L;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull reduce(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
};
FastMod F(2);
int main() {
int M = 1000000007; F = FastMod(M);
ull x = 10ULL*M+3;
cout << x << " " << F.reduce(x) << "\n";
}
树链剖分求LCA
#include<bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/trie_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/rope>
using namespace __gnu_cxx;
using namespace __gnu_pbds;
*/
#define pn putchar('\n')
#define ps putchar(' ')
using namespace std;
typedef long long ll;
template <typename T> void re(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t=((t<<3)+(t<<1)))+=(ch^48); ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
inline void wr(ll x){
if(x<0) putchar('-'),x=-x;
if(x>9) wr(x/10);
putchar(x%10+'0');
}
struct node{
int to,next;
}eg[5000010];
int cnt;
int hd[500005];
void add(int x,int y){
eg[++cnt].next=hd[x];
eg[cnt].to=y;
hd[x]=cnt;
}
int n,m,s;
int dep[500005],fa[500005],siz[500005],so[500005],tp[500005];
void dfs1(int u){
dep[u]=dep[fa[u]]+1;
siz[u]=1;
so[u]=-1;
for(int j=hd[u];j;j=eg[j].next){
int i=eg[j].to;
if(i==fa[u]) continue;
fa[i]=u;
dfs1(i);
siz[u]+=siz[i];
if(siz[i]>siz[so[u]] || so[u]==-1) so[u]=i;
}
}
void dfs2(int u,int p){
tp[u]=p;
if(so[u]==-1) return;
dfs2(so[u],p);
for(int j=hd[u];j;j=eg[j].next){
int i=eg[j].to;
if(i==fa[u] || i==so[u]) continue;
dfs2(i,i);
}
}
int lca(int x,int y){
while(tp[x]!=tp[y]){
if(dep[tp[y]]>dep[tp[x]]) y=fa[tp[y]];
else x=fa[tp[x]];
}
return dep[x]<dep[y] ? x :y;
}
int main(){
//memset(hd,-1,sizeof hd);
re(n),re(m),re(s);
for(int i=1;i<n;i++){
int x,y;
re(x),re(y);
add(x,y);
add(y,x);
}
dfs1(s);
dfs2(s,s);
for(int i=1;i<=m;i++){
int x,y;
re(x),re(y);
wr(lca(x,y)),pn;
}
return 0;
}
// rp++ Chtholly bless me!
// If happiness has a color, it must be the blue dyed by the last red.
// What do you do at the end of the world? Are you busy? Will you save us?
// May you grant me good fortune and brighter days.
#include<iostream>
using namespace std;
long long a,b,p;
long long quick_power(long long x,long long y){
long long ans=1,base=x;
while(y>0){
if(y&1){
ans*=base;
ans%=p;
}
base*=base;
base%=p;
y>>=1;
}
return ans;
}
int main(){
cin>>a>>b>>p;
long long s=quick_power(a,b);
cout<<a<<"^"<<b<<" mod "<<p<<"="<<s<<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=100005,INF=1e9;
int n;
struct Node{
int s[2];
int p;
int v;
int cnt;
int siz;
void init(int p1,int v1){
p=p1, v=v1;
cnt=siz=1;
}
};
struct Splay_tree{
Node tr[N];
int root;
int idx;
int ls(int x){
return tr[x].s[0];
}
int rs(int x){
return tr[x].s[1];
}
void pushup(int x){
tr[x].siz=tr[ls(x)].siz+tr[rs(x)].siz+tr[x].cnt;
}
void rotate(int x){
int y=tr[x].p,z=tr[y].p;
int k=(tr[y].s[1]==x);
tr[z].s[tr[z].s[1]==y]=x;
tr[x].p=z;
tr[y].s[k]=tr[x].s[k^1];
tr[tr[x].s[k^1]].p=y;
tr[x].s[k^1]=y;
tr[y].p=x;
pushup(y);
pushup(x);
}
void splay(int x, int k){
while(tr[x].p!=k){
int y=tr[x].p,z=tr[y].p;
if(z!=k){
(ls(y)==x)^(ls(z)==y)?rotate(x):rotate(y);
}
rotate(x);
}
if(k==0){
root=x;
}
}
void insert(int v){
int x=root,p=0;
while(x && tr[x].v!=v){
p=x;
x=tr[x].s[v>tr[x].v];
}
if(x){
tr[x].cnt++;
}
else{
x=++idx;
tr[p].s[v>tr[p].v]=x;
tr[x].init(p,v);
}
splay(x,0);
}
void find(int v){
int x=root;
while(tr[x].s[v>tr[x].v]&&v!=tr[x].v){
x=tr[x].s[v>tr[x].v];
}
splay(x,0);
}
int getpre(int v){
find(v);
int x=root;
if(tr[x].v<v){
return x;
}
x=ls(x);
while(rs(x)){
x=rs(x);
}
return x;
}
int getnxt(int v){
find(v);
int x=root;
if(tr[x].v>v){
return x;
}
x=rs(x);
while(ls(x)){
x=ls(x);
}
return x;
}
void del(int v){
int pre=getpre(v);
int suc=getnxt(v);
splay(pre,0);
splay(suc,pre);
int del=tr[suc].s[0];
if(tr[del].cnt>1){
tr[del].cnt--, splay(del,0);
}
else{
tr[suc].s[0]=0,splay(suc,0);
}
}
int getrank(int v){
find(v);
return tr[tr[root].s[0]].siz;
}
int getval(int k){
int x=root;
while(1){
int y=ls(x);
if(tr[y].siz+tr[x].cnt<k){
k-=tr[y].siz+tr[x].cnt;
x=rs(x);
}
else if(tr[y].siz>=k){
x=y;
}
else{
break;
}
}
splay(x,0);
return tr[x].v;
}
};
Splay_tree s;
int main(){
s.insert(-INF);
s.insert(INF);
scanf("%d",&n);
while(n--){
int op,x;
scanf("%d%d",&op,&x);
if(op==1) s.insert(x);
if(op==2) s.del(x);
if(op==3) printf("%d\n",s.getrank(x));
if(op==4) printf("%d\n",s.getval(x+1));
if(op==5) printf("%d\n",s.tr[s.getpre(x)].v);
if(op==6) printf("%d\n",s.tr[s.getnxt(x)].v);
}
return 0;
}
扫描线
#include<bits/stdc++.h>
#define N 1000006
using namespace std;
struct Point{
int v,num,tag;
};
struct Event{
int x1,x2,v;
};
struct Rect{
int x1,y1,x2,y2;
};
long long n,x[N],y[N],xcnt,ycnt,ans;
Point tr[4*N];
vector<Event> ev[N];
Rect g[N];
void pushup(int x){
if(tr[x<<1].v == tr[x<<1|1].v){
tr[x].v=tr[x<<1].v;
tr[x].num=tr[x<<1].num+tr[x<<1|1].num;
}
else{
int p=(tr[x<<1].v<tr[x<<1|1].v)?(x<<1):(x<<1|1);
tr[x].v=tr[p].v;
tr[x].num=tr[p].num;
}
}
void pushdown(int l,int r,int x){
int mid=(l+r)>>1;
if(tr[x].tag!=0){
tr[x<<1].tag+=tr[x].tag;
tr[x<<1|1].tag+=tr[x].tag;
tr[x<<1].v+=tr[x].tag;
tr[x<<1|1].v+=tr[x].tag;
tr[x].tag=0;
}
}
void build(int l,int r,int x_){
if(l==r){
tr[x_].num=x[l]-x[l-1];
return;
}
int mid=(l+r)>>1;
build(l,mid,x_<<1);
build(mid+1,r,x_<<1|1);
pushup(x_);
}
void change(int l,int r,int x,int cl,int cr,int va){
if(cl<=l && r<=cr){
tr[x].v+=va;
tr[x].tag+=va;
return;
}
int mid=(l+r)>>1;
pushdown(l,r,x);
if(cl<=mid){
change(l,mid,x<<1,cl,cr,va);
}
if(cr>mid){
change(mid+1,r,x<<1|1,cl,cr,va);
}
pushup(x);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>g[i].x1>>g[i].y1>>g[i].x2>>g[i].y2;
x[++xcnt]=g[i].x1;
x[++xcnt]=g[i].x2;
y[++ycnt]=g[i].y1;
y[++ycnt]=g[i].y2;
}
sort(x+1,x+xcnt+1);
sort(y+1,y+ycnt+1);
xcnt=unique(x+1,x+xcnt+1)-x-1;
ycnt=unique(y+1,y+ycnt+1)-y-1;
for(int i=1;i<=n;i++){
int x1,y1,x2,y2;
x1=lower_bound(x+1,x+xcnt+1,g[i].x1)-x;
y1=lower_bound(y+1,y+ycnt+1,g[i].y1)-y;
x2=lower_bound(x+1,x+xcnt+1,g[i].x2)-x;
y2=lower_bound(y+1,y+ycnt+1,g[i].y2)-y;
ev[y1].push_back((Event){x1+1,x2,1});
ev[y2].push_back((Event){x1+1,x2,-1});
}
x[0]=x[1]; y[0]=y[1];
build(1,xcnt,1);
for(int i=1;i<=ycnt;i++){
int t=tr[1].num;
if(tr[1].v>0){
t=0;
}
ans+=(y[i]-y[i-1])*(x[xcnt]-x[1]-t);
for(int j=0;j<(int)(ev[i].size());j++){
change(1,xcnt,1,ev[i][j].x1,ev[i][j].x2,ev[i][j].v);
}
}
cout<<ans<<endl;
return 0;
}
线段树
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
struct Point{
int v,tag;
};
struct Tree{
Point tr[4*N];
void pushup(int x){
tr[x].v=tr[x<<1].v+tr[x<<1|1].v;
}
void pushdown(int l,int r,int x){
int mid=(l+r)>>1;
if(tr[x].tag!=0){
tr[x<<1].tag+=tr[x].tag;
tr[x<<1|1].tag+=tr[x].tag;
tr[x<<1].v+=(mid+1-l)*tr[x].tag;
tr[x<<1|1].v+=(r-mid)*tr[x].tag;
tr[x].tag=0;
}
}
void build(int l,int r,int x){
if(l==r){
tr[x].v=a[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,x<<1);
build(mid+1,r,x<<1|1);
pushup(x);
}
int query(int l,int r,int x,int ql,int qr){
if(ql<=l && r<=qr){
return tr[x].v;
}
int mid=(l+r)>>1;
int ans=0;
pushdown(l,r,x);
if(ql<=mid){
ans+=query(l,mid,x<<1,ql,qr);
}
if(qr>mid){
ans+=query(mid+1,r,x<<1|1,ql,qr);
}
return ans;
}
void change(int l,int r,int x,int cl,int cr,int va){
if(cl<=l && r<=cr){
tr[x].v+=(r-l+1)*va;
tr[x].tag+=va;
return;
}
int mid=(l+r)>>1;
pushdown(l,r,x);
if(cl<=mid){
change(l,mid,x<<1,cl,cr,va);
}
if(cr>mid){
change(mid+1,r,x<<1|1,cl,cr,va);
}
pushup(x);
}
};
int main(){
int op,l,r,x;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,n,1);
for(;q--;){
cin>>op;
switch(op){
case 1:
cin>>l>>r>>x;
change(1,n,1,l,r,x);
break;
case 2:
cin>>l>>r;
cout<<query(1,n,1,l,r)<<endl;
break;
}
}
return 0;
}
线段树(指针)
#include<bits/stdc++.h>
#define pn putchar('\n')
#define ps putchar(' ')
#define rec(x) scanf(" %c",&(x))
#define ls(i) (i>>1)
#define rs(i) (i>>1|1)
#define In(x) freopen(x".in","r",stdin)
#define Out(x) freopen(x".ans","w",stdout)
#define File(x) (In(x),Out(x))
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
#define pub push_back
#define pob pop_back
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?++x:--x))
using namespace std;
typedef long long ll;
template <typename T> void re(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t=((t<<3)+(t<<1)))+=(ch^48); ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
inline void wr(ll x){
if(x<0) putchar('-'),x=-x;
if(x>9) wr(x/10);
putchar(x%10+'0');
}
ll a[100005],n,m;
struct seg{
int s,e,m;
ll val=0,lazy=0;
seg *l,*r;
seg (int _s,int _e){
s=_s,e=_e,m=s+e>>1;
if (s!=e){
l=new seg(s,m);
r=new seg(m+1,e);
val=l->val+r->val;
}
else
val=a[s];
}
void propo(){
if (lazy){
val+=lazy*(e-s+1);
if (s!=e){
l->lazy+=lazy;
r->lazy+=lazy;
}
lazy=0;
}
}
void update(int i,int j,ll k){
if (s==i && e==j) lazy+=k,propo();
else{
if (j<=m) l->update(i,j,k);
else if (m<i) r->update(i,j,k);
else l->update(i,m,k),r->update(m+1,j,k);
l->propo(),r->propo();
val=l->val+r->val;
}
}
ll query(int i,int j){
propo();
if (s==i && e==j) return val;
else if (j<=m) return l->query(i,j);
else if (m<i) return r->query(i,j);
else return l->query(i,m)+r->query(m+1,j);
}
} *rt;
int main(){
re(n),re(m);
rep(i,1,n+1) re(a[i]);
rt=new seg(1,n);
rep(i,0,m){
int o,x,y;
ll k;
re(o),re(x),re(y);
if(o-1){
wr(rt->query(x,y)),pn;
}
else{
re(k);
rt->update(x,y,k);
}
}
return 0;
}
// rp++ Chtholly bless me!
// If happiness has a color, it must be the blue dyed by the last red.
// What do you do at the end tf the world? Are you busy? Will you save us?
// May you grant me good fortune and brighter days.
线性筛
#include<bits/stdc++.h>
#define N 100005
using namespace std;
int n,cnt,prim[N];
bool vis[N];
int main(){
cin>>n;
for(int i=2;i<=n;i++){
if(!vis[i]){
prim[++cnt]=i;
}
for(int j=1;j<=cnt && i*prim[j]<=n;j++){
int m=i*prim[j];
vis[m]=1;
if(i%prim[j]==0){
break;
}
}
}
cout<<cnt<<endl;
for(int i=1;i<=cnt;i++){
cout<<prim[i]<<" ";
}
cout<<endl;
return 0;
}
线性筛求约数个数
#include<bits/stdc++.h>
#define N 100005
using namespace std;
int n,cnt,prim[N],prims[N],snum[N]={0,1};
bool vis[N];
int main(){
cin>>n;
for(int i=2;i<=n;i++){
if(!vis[i]){
prim[++cnt]=i;
prims[i]=1;
snum[i]=2;
}
for(int j=1;j<=cnt && i*prim[j]<=n;j++){
int m=i*prim[j];
vis[m]=1;
if(i%prim[j]==0){
prims[m]=prims[i]+1;
snum[m]=snum[i]/(prims[i]+1)*(prims[i]+2);
break;
}
snum[m]=snum[i]*snum[prim[j]];
prims[m]=1;
}
}
for(int i=1;i<=n;i++){
cout<<snum[i]<<" ";
}
cout<<endl;
return 0;
}
匈牙利算法 二分图匹配
#include<bits/stdc++.h>
using namespace std;
const int N=502;
int n,m,e,ans;
int tim[N],mch[N];
vector<int>G[N];
bool dfs(int u,int t){
if(tim[u]==t){
return false;
}
tim[u]=t;
for(auto v:G[u]){
if(!mch[v] || dfs(mch[v],t)){
mch[v]=u;
return true;
}
}
return false;
}
int main(){
int u,v;
cin>>n>>m>>e;
while(e--){
cin>>u>>v;
G[u].push_back(v);
}
for(int i=1;i<=n;i++){
if(dfs(i,i)){
ans++;
}
}
cout<<ans<<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int k[500005],s[500005],len,bl[500005],n,m,num,lll[500005],rr[500005];
struct node {
int l, r, id;
friend bool operator < (node x,node y){
if (x.l/len!=y.l/len) return x.l<y.l;
if ((x.l/len) & 1) return x.r<y.r;
return x.r>y.r;
}
}a[500005];
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
inline void write(int x){
if(x<0) x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
void add(int x,int kk){
k[x]+=kk;
s[bl[x]]+=kk;
}
void ask(int l,int r){
int x=bl[l],y=bl[r];
ll ans=0;
if(x==y){
for(int i=l;i<=r;i++){
ans+=k[i];
}
}
else{
for(int i=l;i<=rr[x];i++){
ans+=k[i];
}
for(int i=x+1;i<=y-1;i++){
ans+=s[i];
}
for(int i=lll[y];i<=r;i++){
ans+=k[i];
}
}
write(ans);
putchar('\n');
return;
}
int main(){
read(n);
read(m);
len=sqrt(n);
num=n/len;if (n%len) num++;
for(int i=1;i<=num;i++){
lll[i]=(i-1)*len+1;rr[i]=i*len;
}
rr[num]=n;
for(int i=1;i<=n;i++){
read(k[i]);
bl[i]=(i-1)/len+1;
s[bl[i]]+=k[i];
}
for(int i=1;i<=m;i++){
int a,b,c;
read(a);read(b);read(c);
if(a==1){
add(b,c);
}
else{
ask(b,c);
}
}
return 0;
}
分块暴力(区间修改,单点值)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int k[500005],s[500005],len,bl[500005],n,m,num,lll[500005],rr[500005];
struct node {
int l, r, id;
friend bool operator < (node x,node y){
if (x.l/len!=y.l/len) return x.l<y.l;
if ((x.l/len) & 1) return x.r<y.r;
return x.r>y.r;
}
}a[500005];
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
inline void write(int x){
if(x<0) x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
void ask(int x){
cout<<k[x]+s[bl[x]]<<"\n";
}
void add(int l,int r,int kk){
int x=bl[l],y=bl[r];
ll ans=0;
if(x==y){
for(int i=l;i<=r;i++){
k[i]+=kk;
}
}
else{
for(int i=l;i<=rr[x];i++){
k[i]+=kk;
}
for(int i=x+1;i<=y-1;i++){
s[i]+=kk;
}
for(int i=lll[y];i<=r;i++){
k[i]+=kk;
}
}
}
int main(){
read(n);
read(m);
len=sqrt(n);
num=n/len;if (n%len) num++;
for(int i=1;i<=num;i++){
lll[i]=(i-1)*len+1;rr[i]=i*len;
}
rr[num]=n;
for(int i=1;i<=n;i++){
read(k[i]);
bl[i]=(i-1)/len+1;
//s[bl[i]]+=k[i];
}
for(int i=1;i<=m;i++){
int a,b,c,d;
read(a);read(b);
if(a==2){
ask(b);
}
else{
read(c);read(d);
add(b,c,d);
}
}
return 0;
}
O(n) RMQ
#include<bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/trie_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/rope>
using namespace __gnu_cxx;
using namespace __gnu_pbds;
*/
#define pn putchar('\n')
#define ps putchar(' ')
using namespace std;
typedef long long ll;
template <typename T> void re(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t=((t<<3)+(t<<1)))+=(ch^48); ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
inline void wr(auto x){
if(x<0) putchar('-'),x=-x;
if(x>9) wr(x/10);
putchar(x%10+'0');
}
int k;
inline int po(int x){
return (x+k-1)/k;
}
int n,m;
int lg[100005],a[100005];
int s[10005][25];
int pm[100005],nm[100005];
int main(){
re(n),re(m);
lg[1]=0;
for(int i=2;i<=n;i++) lg[i]=lg[i>>1]+1;
k=lg[n];
for(int i=1;i<=n;i++){
re(a[i]);
s[po(i)][0]=max(s[po(i)][0],a[i]);
}
int p=po(n);
for(int j=1;j<=lg[p];j++){
for(int i=1;i<=p-(1<<j)+1;i++){
s[i][j]=max(s[i][j-1],s[i+(1<<j-1)][j-1]);
}
}
for(int i=1;i<=n;i++){
if(i%k!=1) pm[i]=max(pm[i-1],a[i]);
else pm[i]=a[i];
}
for(int i=n;i>=1;i--){
if(i%k) nm[i]=max(nm[i+1],a[i]);
else nm[i]=a[i];
}
for(int i=1;i<=m;i++){
int l,r;
re(l),re(r);
int kk=lg[po(r)-po(l)-1];
if(po(l)==po(r)){
int ma=0;
for(int i=l;i<=r;i++){
ma=max(a[i],ma);
}
wr(ma),pn;
}
else if(po(r)-po(l)==1) wr(max(pm[r],nm[l])),pn;
else wr(max(max(s[po(l)+1][kk],s[po(r)-(1<<kk)][kk]),max(pm[r],nm[l]))),pn;
}
return 0;
}
// rp++ Chtholly bless me!
// If happiness has a color, it must be the blue dyed by the last red.
// What do you do at the end of the world? Are you busy? Will you save us?
// May you grant me good fortune and brighter days.