# 莫隊

(通常是)靜態的區間查詢

## 核心代碼

struct question{int l,r,i;}; // 第i個詢問 [l,r]
int n,q,k; vector<int> arr;
void init(int n){}
void rem(int i){}
int get_ans(){}
signed main(){
vector<question> querys;
cin>>n>>q;
arr.resize(n);
querys.resize(q);
k=n/sqrt(q);
for(int i=0;i<n;i++) cin>>arr[i];
for(int i=0;i<q;i++)
cin>>querys[i].l>>querys[i].r,querys[i].i=i;
sort(querys.begin(),querys.end(),
[](question a,question b){
return a.l/k<b.l/k||(a.l/k==b.l/k&&a.r<.r);
});
init(n);
int l=0,r=-1; vector<int> ans(q);
for(int i=0;i<q;i++){
while(querys[i].r<r) rem(r--);
while(l<querys[i].l) rem(l++);
ans[querys[i].i]=get_ans();
}
return 0;
}

## ZJ b417

https://zerojudge.tw/ShowProblem?problemid=b417

"眾數的個數"(出現最多次的人出現幾次)

$$N\leq 10^5, M\leq 10^6$$

# 整體二分搜

## TIOJ 1840

https://tioj.ck.tp.edu.tw/problems/1840

## TIOJ 1840

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
#define F first
#define S second
const int INF=pow(2,31)-1;
struct act{int tp,l,r,k,i;};
struct BIT{
vector<int> data; vector<pii> rec; int n;
void init(int len){n=len; data.clear(); data.resize(n+1);}
int lowbit(int x){return x&(-x);}
void modify(int pos,int val,bool record=true){
if(record) rec.push_back({pos,val});
while(pos<=n){
data[pos]+=val;
pos+=lowbit(pos);
}
}
int query(int pos){
int val=0;
while(pos>0){
val+=data[pos];
pos-=lowbit(pos);
}
return val;
}
int query(int l,int r){
return query(r)-query(l-1);
}
void undo(){
while(!rec.empty()){
modify(rec.back().F,-rec.back().S,false);
rec.pop_back();
}
}
} bit;
vector<act> action; vector<int> ans,arr,comp;
void total_binary_search(vector<int> &v,int l,int r){
if(l+1==r||v.size()==0){
for(auto &i:v){
if(action[i].tp==1) ans[action[i].i]=l;
}
return void();
}
int mid=(l+r)/2;
vector<int> L,R;
for(auto &i:v){
if(action[i].tp==1){
int k=bit.query(action[i].l,action[i].r);
if(k>=action[i].k){
L.push_back(i);
}
else{
action[i].k-=k;
R.push_back(i);
}
}
else if(action[i].tp==2){
if(action[i].i>0){
if(action[i].k<mid){
bit.modify(action[i].i,1);
L.push_back(i);
}
else{
R.push_back(i);
}
}
else{
if(action[i].k<mid){
bit.modify(-action[i].i,-1);
L.push_back(i);
}
else{
R.push_back(i);
}
}
}
}
bit.undo();
total_binary_search(L,l,mid);
total_binary_search(R,mid,r);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
int n,q;
cin>>n>>q;
bit.init(n); action.clear(); ans.clear(); arr.clear(); arr.resize(n+1); comp.clear();
for(int i=1;i<=n;i++){
cin>>arr[i];
comp.push_back(arr[i]);
}
for(int i=1;i<=n;i++){
action.push_back(act());
action.back().tp=2;
action.back().i=i;
action.back().k=arr[i];
}
for(int i=0;i<q;i++){
int k;
cin>>k;
if(k==1){
action.push_back(act());
action.back().tp=1;
action.back().i=ans.size();
cin>>action.back().l>>action.back().r>>action.back().k;
ans.push_back(0);
}
else if(k==2){
action.push_back(act());
action.back().tp=2;
cin>>k; action.back().i=-k; action.back().k=arr[k];
action.push_back(act());
action.back().tp=2;
cin>>arr[k]; action.back().i=k; action.back().k=arr[k]; comp.push_back(arr[k]);
}
else if(k==3){cin>>k>>k; ans.push_back(-1);}
}
sort(comp.begin(),comp.end());
comp.erase(unique(comp.begin(),comp.end()),comp.end());
for(act &i:action){
if(i.tp==2) i.k=lower_bound(comp.begin(),comp.end(),i.k)-comp.begin();
}
vector<int> v; for(int i=0;i<action.size();i++) v.push_back(i);
total_binary_search(v,0,comp.size());
for(auto &i:ans){
if(i!=-1) cout<<comp[i]<<endl;
else cout<<7122<<endl;
}
}
return 0;
}


# 操作分治

## 逆序數對

https://tioj.ck.tp.edu.tw/problems/1080

merge sort

## No Judge

$$1,x,y,w$$: 將$$(x,y)$$加上權重$$w$$

$$2,x,y$$: 詢問$$(x,y)$$左下角的權重總和

By jass921026

• 707