令 ,
因为
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e7;
// n log n
int n,m;
int lc[maxn],rc[maxn], l[maxn], r[maxn], siz[maxn], cnt, rt[maxn];
void insert(int& nn, int mir, int ll, int rr, int x){
if (nn == 0){
nn = ++cnt;
l[nn] = ll;
r[nn] = rr;
}
siz[nn] = siz[mir] + 1;
if(ll == rr) return;
int mid = (ll+rr)>>1;
if (x <= mid) rc[nn] = rc[mir],insert(lc[nn], lc[mir], ll, mid, x);
else lc[nn] = lc[mir],insert(rc[nn], rc[mir], mid+1, rr, x);
}
int query(int nn1, int nn2, int k){
// cout << nn1 << " " << nn2 << endl;
if (l[nn2] == r[nn2]) return l[nn2];
int tmp = siz[lc[nn2]] - siz[lc[nn1]];
// cout << lc[nn2] << " " << rc[nn1] << endl;
// cout << siz[lc[nn2]] << " " << siz[lc[nn1]] << endl;
if (k <= tmp) return query(lc[nn1], lc[nn2], k);
else return query(rc[nn1], rc[nn2], k - tmp);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i = 1; i <= n; i++){
int x;
cin>>x;
insert(rt[i], rt[i-1], 0, 1e9, x);
}
for(int i = 1; i <= m; i++){
int l,r,k;
cin>>l>>r>>k;
cout<<query(rt[l-1],rt[r],k)<<endl;
}
} #include <bits/stdc++.h>
using namespace std;
int n,k; // n : length k : swap chance
const int N = 105;
int f[N][N][N* 2]; // f[i][j][k] the prefix of i , j swap, k of 1 at edge
const int offset = 105;
bitset<N> a,b;
void check(int &x , int y){
x = max(x, y);
}
int abs(int x){
if(x>0) return x;
else return -x;
}
int main(){
cin>>n>>k;
cin>>a>>b;
a<<=1;
b<<=1;
for(int i=0;i<=n;i++){
for(int j=0;j<=k;j++){
for(int x=offset-n;x<=offset+n;x++){
f[i][j][x] = -101;
}
}
}
f[0][0][0+offset] = 0;
for(int i=0;i<=n;i++){
for(int j=0;j<=k;j++)
for(int x=i-n+offset;x<=i+offset;x++){
if(f[i][j][x] < 0 ) continue;
// cout<<i<<" "<<j<<" "<<x-offset<<": "<<f[i][j][x]<<endl;
if(i==n) continue;
int rx = abs(x-offset);
if(a[i+1] == b[i+1]){
if(j+rx <= k ) check( f[i+1][j+rx][x], f[i][j][x] + a[i+1]);
} else {
if(b[i+1] == 1){
if(j+abs(x+1-offset) <= k ) check(f[i+1][j+abs(x+1-offset)][x+1], f[i][j][x]);
} else {
if(j+abs(x-1-offset) <= k ) check(f[i+1][j+abs(x-1-offset)][x-1], f[i][j][x] + 1);
}
if( j+abs(x-offset) <= k ) check(f[i+1][j+abs(x-offset)][x], f[i][j][x]);
}
}
}
int ans = 0;
for(int i=0;i<=k;i++){
ans = max(ans, f[n][i][offset]);
}
cout<<ans<<endl;
}#include <bits/stdc++.h>
using namespace std;
int n,k; // n : length k : swap chance
const int N = 1e6 + 10;
bitset<N> a,b;
typedef pair<int,pair<int,int> > node; // first cost , second first left , second second right
set<int> ss;
priority_queue<node, vector<node>, greater<node> > pq;
int ans = 0;
int main(){
cin>>n>>k;
cin>>a>>b;
a<<=1;
b<<=1;
for(int i=1;i<=n;i++){
if(a[i]!=b[i]){
ss.insert(i);
} else if(b[i] == 1) ans++;
}
int last = -1;
for(auto i:ss){
if(last != -1 && b[last] != b[i]){
pq.push(make_pair(i - last, make_pair(last, i)));
}
last = i;
}
for(;!pq.empty();){
auto x = pq.top();
int len = x.first, left = x.second.first, right = x.second.second;
pq.pop();
if(ss.find(left) == ss.end() || ss.find(right) == ss.end()) continue;
if(len > k) break;
ans++;
k -= len;
if(ss.find(left) == ss.begin()) continue;
auto pr = prev(ss.find(left)), nxt = next(ss.find(right));
if(nxt == ss.end()) continue;
if(b[*pr] != b[*nxt]){
if(b[*pr] == b[left]){
pq.push(make_pair(*nxt - *pr, make_pair(*pr, *nxt)));
} else {
pq.push(make_pair(*nxt - *pr - len * 2, make_pair(*pr, *nxt)));
}
}
ss.erase(left);
ss.erase(right);
}
cout<< ans << endl;
}
/*
10 8
1000010000
0000100001
*/int merge(int x, int y) {
if (!x || !y) return x | y;
if (val[x] > val[y]) swap(x, y);
const int d = rand() % 2;
son[x][d] = merge(son[x][d], y);
return x;
}