2k2 分鐘

# 双链表 #include <iostream> #include <algorithm> using namespace std; const int N = 1e6 + 10; int e[N], l[N], r[N], idx; int m; void init(){ r[0] = 1, l[1] = 0; idx = 2; //idx是当前用到的哪儿个数组下标 } void add(int k, int x){ e[idx] = x; r[i
1.1k1 分鐘

# 模拟栈 #include <iostream> using namespace std; const int N = 1e5 + 10; int stk[N], tt; int x; void push(int x){ stk[++tt] = x; } void pop(){ tt--; } void query(){ cout << stk[tt] << endl; } void empty(){ if(tt >
8591 分鐘

# 模拟队列 #include <iostream> using namespace std; const int N = 1e5 + 10; int q[N], hh, tt = -1; void push(int x){ q[++tt] = x; } void pop(){ hh++; } void empty(){ if(hh <= tt) cout << "NO" << endl; else
2.5k2 分鐘

最大异或对 #include <iostream> using namespace std; const int N = 1e6 + 10, M = N * 3; int n; int a[N]; int son[M][2], cnt[N], idx; void insert(int x){ int p = 0; for(int i = 30; ~i; i--){ int &s = son[p][x >> i & 1]; if(!s
1.9k2 分鐘

# 滑动窗口 - 单调队列 #include <iostream> using namespace std; const int N = 1e6 + 10; int n, k; int a[N], q[N]; int main(){ scanf("%d%d", &n, &k); for(int i = 0; i < n; i++) scanf("%d", &a[i]); int hh = 0, tt = -1; for(int
1.2k1 分鐘

# 数组元素的目标和 - 双指针 #include <iostream> #include <algorithm> using namespace std; const int N = 1e6 + 10; int n, m, q; int a[N], b[N]; int main(){ scanf("%d %d %d", &n, &m, &q); for(int i = 0; i < n; i++) scanf("%d", &a[i]); for
2.7k2 分鐘

# 区间和 - 离散化 #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef pair<int, int> PII; const int N = 3e6 + 10; int n, m; int a[N], s[N]; vector<int> alls; vector<PII> add, query; int find(int x){ int l = 0, r
1.7k2 分鐘

# 区间合并 - 贪心 #include <iostream> #include <algorithm> #include <vector> using namespace std; typedef pair<int, int>PII; const int N = 1e6 + 10; int n; vector<PII> segs; void merge(vector<PII> &segs){ vector<PII> res; sort(segs.begin(), se
2.6k2 分鐘

食物链 - 并查集 #include <iostream> using namespace std; const int N = 1e6 + 10; int n, m; int p[N], d[N]; int find(int x){ if(p[x] != x){ int t = find(p[x]); d[x] += d[p[x]]; p[x] = t; } return p[x]; } int main(){ scanf