跳到正文

个人记录

小技巧

luckyyunji约 17 分钟
原文
文章信息与版本

文章信息

发布
2026年3月22日
收录
2026年8月16日
更新
2026年11月11日
篇幅
约 17 分钟
获赞
1

版本

  1. v1当前
C++
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //关联容器,必加
#include <ext/pb_ds/hash_policy.hpp>     //哈希表
#include <ext/pb_ds/tree_policy.hpp>     //平衡树
#include <ext/pb_ds/trie_policy.hpp>     //字典树
#include <ext/pb_ds/priority_queue.hpp>  //优先队列
#include <bits/extc++.h>
using namespace __gnu_cxx;
using namespace __gnu_pbds;
const int dx[4]={1,1,-1,-1},dy[4]={1,-1,1,-1};
const db PI=acos(-1.0);
namespace myVector{
};using namespace myVector;
#define int long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
#define db double
#define eps 1e-3
#undef Rof
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;
    db ans=1ll<<10;
    if(ans=x>1e3) cout<<x+ans<<endl;
    cout<<setprecision(2)<<ans<<'\n';
    vector<int> vi;
    vi.reserve(4);vi.emplace_back(1);
    vi.push_back(2);
    stable_sort(vi.begin(),vi.end())
    for(auto i:vi) cout<<i<<' ';
    return (void)puts("");
}
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
#define db double
#define eps 1e-13
#define PPP pair<Point,Point>
#define Eq(a,b) (fabs(a-b)<=eps)
const double PI=acos(-1);
namespace myVector{
	struct Point{db x,y;};
	struct Line{Point s,e;};
	struct Circle{Point p;db r;};
	// Point
	Point operator+(Point a,Point b){return {a.x+b.x,a.y+b.y};}
	Point operator-(Point a,Point b){return {a.x-b.x,a.y-b.y};}
	Point operator*(Point a,db b){return {a.x*b,a.y*b};}
	Point operator/(Point a,db b){return {a.x/b,a.y/b};}
	bool operator==(Point a,Point b){return fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps;}
	db operator*(Point a,Point b){return a.x*b.y-a.y*b.x;}
	db cross(Point a,Point b,Point c){return (b-a)*(c-a);}
	db operator&(Point a,Point b){return a.x*b.x+a.y*b.y;}
	db dot(Point a,Point b,Point c){return (b-a)&(c-a);}
	db len(Point a){return sqrt(a.x*a.x+a.y*a.y);}
	db dis(Point a,Point b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
	db angle(Point a,Point b){return acos((a&b)/len(a)/len(b));}
	Point cross(Point a,Point u,Point b,Point v){db t=(a-b)*v/(v*u);return a+u*t;}
	bool cmp1(Point a,Point b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
	Point rotate(Point a,db b){return {a.x*cos(b)-a.y*sin(b),a.x*sin(b)+a.y*cos(b)};}
	db getS(Point a,Point b,Point c){return fabs((a*b+b*c+c*a))/2.0;}
	bool intersect(Point a,Point b,Point c,Point d){
		if(cross(a,b,c)*cross(a,b,d)>0) return 0;
		if(cross(c,d,a)*cross(c,d,b)>0) return 0;
		return 1;
	}
	// Line
	Point cross(Line a,Line b){return cross(a.s,a.e-a.s,b.s,b.e-b.s);}
	PPP midperp(Point a,Point b){return {(a+b)/2,rotate(b-a,PI/2)};}
	db angle(Line a){return atan2(a.e.y-a.s.y,a.e.x-a.s.x);}
	bool cmp2(Line a,Line b){
		db A=angle(a),B=angle(b);
		return fabs(A-B)>eps?A<B:(a.e-a.s)*(b.e-a.s)<0;
	}
	bool right(Line a,Line b,Line c){
		Point p=cross(b,c);
		return (a.e-a.s)*(p-a.s)<0;
	}
	// Circle
	Circle cover(Point a,Point b){return {(a+b)/2,dis(a,b)/2};}
	Circle cover(Point a,Point b,Point c){
		auto u=midperp(a,b),v=midperp(a,c);
		auto p=cross(u.fi,u.se,v.fi,v.se);
		return {p,dis(p,a)};
	}
};using namespace myVector;
C++
#include <iostream>
#include <string>
#include <ext/rope>                       // rope
#include <ext/pb_ds/assoc_container.hpp> // tree
#include <ext/pb_ds/tree_policy.hpp>     // tree_order_statistics_node_update

using namespace std;
using namespace __gnu_cxx;   // for rope
using namespace __gnu_pbds;  // for tree

/* ========================= 1. rope(块状字符串) =========================
   rope<T>  - 高效支持随机插入、删除、拼接、子串等操作(内部使用平衡树块)
   常用方法:
     构造: rope<T>(), rope<T>(n, val), rope<T>(str), rope<T>(it1, it2)
     添加/插入: push_back(val), append(str), insert(pos, str)
     删除: erase(pos, len)
     替换: replace(pos, len, str)
     访问: at(pos), operator[](pos)  -- 返回 const 引用,不可修改
     子串: substr(pos, len)          -- 返回一个新的 rope
     拷贝到 C 数组: copy(pos, len, dest)
     连接: operator+
     大小: size(), length(), empty()
     清空: clear()
     输出: 可通过 ostream_iterator 或逐个字符输出
   性能:O(log N) 的插入、删除、拼接,适用于大字符串频繁修改
*/
void demo_rope() {
    // 创建
    rope<char> r("Hello world");
    cout << "Initial: ";
    for (char c : r) cout << c;
    cout << " | size = " << r.size() << endl;

    // push_back
    r.push_back('!');
    cout << "After push_back('!'): ";
    for (char c : r) cout << c;
    cout << endl;

    // append(字符串)
    r.append(" How are you?");
    cout << "After append: ";
    for (char c : r) cout << c;
    cout << endl;

    // insert(位置,字符串)
    r.insert(6, "beautiful ");
    cout << "After insert at 6: ";
    for (char c : r) cout << c;
    cout << endl;

    // erase(位置, 长度)
    r.erase(6, 10); // 删除 "beautiful "
    cout << "After erase 10 chars from 6: ";
    for (char c : r) cout << c;
    cout << endl;

    // replace(位置, 长度, 新字符串)
    r.replace(6, 5, "there");
    cout << "After replace: ";
    for (char c : r) cout << c;
    cout << endl;

    // substr 返回新的 rope
    rope<char> sub = r.substr(6, 5); // "there"
    cout << "Substr(6,5): ";
    for (char c : sub) cout << c;
    cout << endl;

    // at / operator[]
    cout << "r[0] = " << r[0] << ", r.at(1) = " << r.at(1) << endl;

    // copy 到 char 数组
    char buf[20] = {0};
    r.copy(0, 5, buf);
    cout << "First 5 chars copied: " << buf << endl;

    // 连接用 + ,返回新 rope
    rope<char> r2 = r + rope<char>(" Nice to meet you.");
    cout << "Concatenated: ";
    for (char c : r2) cout << c;
    cout << endl;

    // 与 string 互转(需要遍历或 copy)
    string s;
    for (char c : r) s.push_back(c);
    cout << "As std::string: " << s << endl;

    // 删除全部
    r.clear();
    cout << "After clear, size = " << r.size() << endl << endl;
}

/* ========================= 2. tree(红黑树 + 顺序统计) =========================
   tree<Key, Mapped, Cmp_Fn, Tag, Node_Update>
   - 若 Mapped = null_type,为集合 (set),否则为映射 (map)。
   - Tag 常用 rb_tree_tag, splay_tree_tag, ov_tree_tag。
   - Node_Update 使用 tree_order_statistics_node_update 才能获得顺序统计操作。
   常用方法(集合视角):
      insert(x), erase(x), find(x)
      lower_bound(x), upper_bound(x)
      order_of_key(x)   -> 严格小于 x 的元素个数(排名)
      find_by_order(k)  -> 第 k 小(0-based)元素的迭代器
      join(other)       -> 合并另一棵树,要求本树最大值 < 另一树最小值
      split(v, other)   -> 将键 > v 的元素分割到 other 中
      size(), empty(), clear()
   示例演示以映射形式存储,并展示更多操作。
*/
void demo_tree() {
    // 定义:key = int, value = string, 升序, 红黑树, 顺序统计
    tree<int, string, less<int>, rb_tree_tag, tree_order_statistics_node_update> t;

    // 插入
    t.insert({5, "five"});
    t.insert({1, "one"});
    t.insert({3, "three"});
    t.insert({8, "eight"});
    t.insert({6, "six"});
    cout << "Tree size: " << t.size() << endl;
    cout << "All elements: ";
    for (auto &p : t) cout << "(" << p.first << "," << p.second << ") ";
    cout << endl;

    // 查找
    auto it = t.find(3);
    if (it != t.end()) cout << "Found key 3 -> " << it->second << endl;

    // lower_bound / upper_bound
    cout << "lower_bound(4): " << t.lower_bound(4)->first << endl; // 5
    cout << "upper_bound(5): " << t.upper_bound(5)->first << endl; // 6

    // 顺序统计:order_of_key 返回比 key 小的元素个数
    cout << "order_of_key(5): " << t.order_of_key(5) << endl;      // 2 (1,3)
    // find_by_order(k) 返回第 k 小(0-based)的迭代器
    auto kth = t.find_by_order(2);
    cout << "2nd index (0-based) element: " << kth->first << " -> " << kth->second << endl; // 5

    // 修改映射的值(通过 find 返回的迭代器可以修改 value)
    it = t.find(1);
    if (it != t.end()) it->second = "one-modified";
    cout << "After modify key 1: " << t.find(1)->second << endl;

    // 删除
    t.erase(3);
    cout << "After erase 3, size = " << t.size() << endl;

    // 分割:split(v, other) 将键 > v 的元素移到 other 中
    tree<int, string, less<int>, rb_tree_tag, tree_order_statistics_node_update> t2;
    t.split(5, t2); // 将 >5 的元素(即 6,8)移到 t2
    cout << "After split at 5:\n t: ";
    for (auto &p : t) cout << p.first << " ";
    cout << "\n t2: ";
    for (auto &p : t2) cout << p.first << " ";
    cout << endl;

    // 合并:将 t2 合并回 t(要求 t 最大值 < t2 最小值)
    t.join(t2);
    cout << "After join: ";
    for (auto &p : t) cout << p.first << " ";
    cout << endl;

    // 获取小于某个值的所有元素?可利用 lower_bound 遍历
    cout << "Keys < 6: ";
    for (auto it = t.begin(); it != t.lower_bound(6); ++it)
        cout << it->first << " ";
    cout << endl;

    // 清空
    t.clear();
    cout << "Cleared, empty = " << t.empty() << endl << endl;
}

int main() {
    cout << "=== Rope 演示 ===" << endl;
    demo_rope();

    cout << "=== Tree (顺序统计红黑树) 演示 ===" << endl;
    demo_tree();

    return 0;
}
原文