D - Cutting Woods
D题的话很简单,只需要维护区间的左右边长即可。我们使用set来存储被切割的点,每切一刀就把点加入集合。搜寻长度就upper_bound一下set,取当前和前一个数的差即可(set默认自动排序)
#include<bits/stdc++.h>
using namespace std;
//=================================
set<int> S;
int l,m;
//=================================
int main(){
cin >> l >> m;
S.insert(0);
S.insert(l);
for(int i=1;i<=m;i++){
int op,x;
cin >> op >> x;
if(op==1){
S.insert(x);
}
else{
auto pos = S.upper_bound(x);
cout << *pos - *(--pos) << endl;
}
}
return 0;
}
E - Sorting Queries
E的话突然想到一个很妙的方法,用小根堆+手写队列维护;
如果是1插入操作,只需要先插入队列即可
如果是2弹出打印操作,如果小根堆为空就打印队列的开头,并弹出队列。否则就打印小根堆的开头并弹出
三操作只需要把队列的所有元素加进小根堆即可
目前在提交的里面还没看到比我快的36ms
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
using namespace std;
//================================
#define debug(a) cout << #a": " << a << endl;
#define N 200010
//================================
typedef pair<int,int> pii;
#define x first
#define y second
typedef long long LL; typedef unsigned long long ULL; typedef long double LD;
inline LL read() { LL s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void print(LL x, int op = 10) { if (!x) { putchar('0'); if (op) putchar(op); return; } char F[40]; LL tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); if (op) putchar(op); }
//=================================
int q;
int op,x;
priority_queue<int,vector<int>,greater<int>> heap;
int a[2*N],hh=1,tt=0;
//=================================
int main(){
q=read();
while(q--){
op=read();
if(op==1){
x=read();
a[++tt]=x;
}
else if(op==2){
if(heap.empty()){
print(a[hh++]);
}
else{
print(heap.top());
heap.pop();
}
}
else{
while(hh<=tt) heap.push(a[hh++]);
}
}
return 0;
}