跳到正文

个人记录

实用头文件(第四版)

The_Que约 31 分钟
原文

这是 2025年12月6日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年1月28日
篇幅
约 31 分钟

版本

  1. v2当前
  2. v1
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
namespace tree_array{ void upd(int k,long long x,long long MXN,long long tr[]){ while(k<=MXN){ tr[k]+=x; k+=(k&-k); } } long long quy(int x,long long tr[]){ long long ans=0; while(x){ ans+=tr[x]; x-=(x&-x); } return ans; } }
namespace better_function { void discretization(int n, int (&arr)[]) { vector<int> disarr; disarr.push_back(0); for (int i = 1; i <= n; i++) disarr.push_back(arr[i]); sort(disarr.begin() + 1, disarr.end()); disarr.erase(unique(disarr.begin() + 1, disarr.end()), disarr.end()); for (int i = 1; i <= n; i++) arr[i] = lower_bound(disarr.begin() + 1, disarr.end(), arr[i]) - disarr.begin(); } inline ll read() { ll x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-')f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return x * f; } void print(ll x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) { print(x / 10); putchar(x % 10 + '0'); } else putchar(x + '0'); return; } }
class highsum{private:std::string value;bool isNegative;void normalize(){size_t nonZeroIndex=0;while(nonZeroIndex<value.size()-1&&value[nonZeroIndex]=='0'){nonZeroIndex++;}value=value.substr(nonZeroIndex);if(value=="0"){isNegative=false;}}static bool unsignedLess(const std::string& a,const std::string& b){if(a.length()!=b.length()){return a.length()<b.length();}return a<b;}static std::string unsignedAdd(const std::string& a,const std::string& b){std::string result;int carry=0;int i=a.length()-1;int j=b.length()-1;while(i>=0||j>=0||carry>0){int digit1=(i>=0)?(a[i--]-'0'):0;int digit2=(j>=0)?(b[j--]-'0'):0;int sum=digit1+digit2+carry;carry=sum/10;result.push_back((sum%10)+'0');}std::reverse(result.begin(),result.end());return result;}static std::string unsignedSubtract(std::string a,const std::string& b){if(unsignedLess(a,b)){return"0";}int borrow=0;int i=a.length()-1;int j=b.length()-1;std::string result;while(i>=0||j>=0){int digit1=(i>=0)?(a[i--]-'0'):0;int digit2=(j>=0)?(b[j--]-'0'):0;digit1-=borrow;borrow=0;if(digit1<digit2){digit1+=10;borrow=1;}result.push_back((digit1-digit2)+'0');}std::reverse(result.begin(),result.end());size_t nonZero=0;while(nonZero<result.size()-1&&result[nonZero]=='0'){nonZero++;}return result.substr(nonZero);}std::string toBinaryString()const{if(value=="0")return"0";highsum temp=*this;std::string binary;if(!isNegative){while(temp>highsum(0)){highsum remainder=temp%2;binary=(remainder==highsum(1)?"1":"0")+binary;temp=temp/2;}}else{highsum absVal=-temp;std::string absBinary;while(absVal>highsum(0)){highsum remainder=absVal%2;absBinary=(remainder==highsum(1)?"1":"0")+absBinary;absVal=absVal/2;}for(char&c:absBinary){c=(c=='1')?'0':'1';}binary=absBinary;int carry=1;for(int i=binary.length()-1;i>=0&&carry>0;i--){if(binary[i]=='0'){binary[i]='1';carry=0;}else{binary[i]='0';}}if(carry){binary="1"+binary;}}return binary.empty()?"0":binary;}static highsum fromBinaryString(const std::string& binary){if(binary.empty()||binary=="0")return highsum(0);if(binary[0]=='1'){std::string temp=binary;int borrow=1;for(int i=temp.length()-1;i>=0&&borrow>0;i--){if(temp[i]=='1'){temp[i]='0';borrow=0;}else{temp[i]='1';}}for(char&c:temp){c=(c=='1')?'0':'1';}highsum result(0);highsum power(1);for(int i=temp.length()-1;i>=0;i--){if(temp[i]=='1'){result=result+power;}power=power*2;}return-result;}else{highsum result(0);highsum power(1);for(int i=binary.length()-1;i>=0;i--){if(binary[i]=='1'){result=result+power;}power=power*2;}return result;}}static std::string bitwiseOperation(const std::string& bin1,const std::string& bin2,char(*op)(char,char)){size_t maxLen=std::max(bin1.length(),bin2.length());std::string a=bin1;std::string b=bin2;if(!a.empty()){char signBit=a[0];a=std::string(maxLen-a.length(),signBit)+a;}if(!b.empty()){char signBit=b[0];b=std::string(maxLen-b.length(),signBit)+b;}std::string result;for(size_t i=0;i<maxLen;i++){result.push_back(op(a[i],b[i]));}return result;}static char andOp(char a,char b){return(a=='1'&&b=='1')?'1':'0';}static char orOp(char a,char b){return(a=='1'||b=='1')?'1':'0';}static char xorOp(char a,char b){return(a!=b)?'1':'0';}public:highsum():value("0"),isNegative(false){}highsum(const std::string& num){if(num.empty()){value="0";isNegative=false;}else{isNegative=(num[0]=='-');value=isNegative?num.substr(1):num;if(value.empty()||!std::all_of(value.begin(),value.end(),::isdigit)){value="0";isNegative=false;}normalize();}}highsum(int num){isNegative=num<0;value=std::to_string(isNegative?-num:num);normalize();}highsum(long long num){isNegative=num<0;value=std::to_string(isNegative?-num:num);normalize();}highsum(const highsum& other):value(other.value),isNegative(other.isNegative){}highsum&operator=(const highsum& other){if(this!=&other){value=other.value;isNegative=other.isNegative;}return*this;}highsum operator-()const{highsum result(*this);if(result.value!="0"){result.isNegative=!result.isNegative;}return result;}highsum operator+(const highsum& other)const{if(isNegative!=other.isNegative){if(isNegative){return other-(-(*this));}else{return*this-(-other);}}std::string result=unsignedAdd(value,other.value);highsum sumResult(result);sumResult.isNegative=isNegative;return sumResult;}highsum operator-(const highsum& other)const{if(isNegative!=other.isNegative){return*this+(-other);}if(isNegative){return(-other)-(-(*this));}if(unsignedLess(value,other.value)){highsum result=unsignedSubtract(other.value,value);result.isNegative=true;return result;}else{highsum result=unsignedSubtract(value,other.value);result.isNegative=false;return result;}}highsum operator*(const highsum& other)const{if(value=="0"||other.value=="0"){return highsum("0");}std::string num1=value;std::string num2=other.value;int len1=num1.size();int len2=num2.size();std::vector<int>result(len1+len2,0);int i_n1=0;int i_n2=0;for(int i=len1-1;i>=0;i--){int digit1=num1[i]-'0';i_n2=0;int carry=0;for(int j=len2-1;j>=0;j--){int digit2=num2[j]-'0';int sum=digit1*digit2+result[i_n1+i_n2]+carry;carry=sum/10;result[i_n1+i_n2]=sum%10;i_n2++;}if(carry>0){result[i_n1+i_n2]+=carry;}i_n1++;}int k=result.size()-1;while(k>=0&&result[k]==0){k--;}std::string product;while(k>=0){product+=std::to_string(result[k--]);}highsum mulResult(product);mulResult.isNegative=isNegative^other.isNegative;return mulResult;}highsum operator/(const highsum& other)const{if(other.value=="0"){throw std::runtime_error("Division by zero");}bool resultNegative=isNegative^other.isNegative;if(value=="0"){return highsum("0");}if(unsignedLess(value,other.value)){return highsum("0");}std::string dividend=value;std::string divisor=other.value;std::string quotient;std::string remainder;for(size_t i=0;i<dividend.size();++i){remainder+=dividend[i];size_t nonZero=0;while(nonZero<remainder.size()-1&&remainder[nonZero]=='0'){nonZero++;}remainder=remainder.substr(nonZero);int digit=0;while(!unsignedLess(remainder,divisor)){remainder=unsignedSubtract(remainder,divisor);digit++;}quotient+=std::to_string(digit);}size_t nonZero=0;while(nonZero<quotient.size()-1&&quotient[nonZero]=='0'){nonZero++;}quotient=quotient.substr(nonZero);highsum result(quotient);result.isNegative=resultNegative;return result;}highsum operator%(const highsum& other)const{if(other.value=="0"){throw std::runtime_error("Division by zero");}if(value=="0"){return highsum("0");}if(unsignedLess(value,other.value)){return*this;}std::string dividend=value;std::string divisor=other.value;std::string remainder;for(size_t i=0;i<dividend.size();++i){remainder+=dividend[i];size_t nonZero=0;while(nonZero<remainder.size()-1&&remainder[nonZero]=='0'){nonZero++;}remainder=remainder.substr(nonZero);while(!unsignedLess(remainder,divisor)){remainder=unsignedSubtract(remainder,divisor);}}highsum result(remainder);result.isNegative=isNegative;return result;}highsum operator+(int num)const{return*this+highsum(num);}highsum operator+(long long num)const{return*this+highsum(num);}highsum operator-(int num)const{return*this-highsum(num);}highsum operator-(long long num)const{return*this-highsum(num);}highsum operator*(int num)const{return*this*highsum(num);}highsum operator*(long long num)const{return*this*highsum(num);}highsum operator/(int num)const{return*this/highsum(num);}highsum operator/(long long num)const{if(num==0){throw std::runtime_error("Division by zero");}bool resultNegative=isNegative^(num<0);unsigned long long absNum=num<0?-num:num;if(value=="0"){return highsum("0");}std::string dividend=value;unsigned long long remainder=0;std::string quotient;for(size_t i=0;i<dividend.size();++i){remainder=remainder*10+(dividend[i]-'0');int digit=remainder/absNum;remainder=remainder%absNum;quotient+=std::to_string(digit);}size_t nonZero=0;while(nonZero<quotient.size()-1&&quotient[nonZero]=='0'){nonZero++;}quotient=quotient.substr(nonZero);highsum result(quotient);result.isNegative=resultNegative;return result;}highsum operator%(int num)const{return*this%highsum(num);}highsum operator%(long long num)const{if(num==0){throw std::runtime_error("Division by zero");}unsigned long long absNum=num<0?-num:num;if(value=="0"){return highsum("0");}std::string dividend=value;unsigned long long remainder=0;for(size_t i=0;i<dividend.size();++i){remainder=remainder*10+(dividend[i]-'0');remainder=remainder%absNum;}highsum result(std::to_string(remainder));result.isNegative=isNegative;return result;}highsum operator<<(const highsum& other)const{if(other.isNegative){throw std::runtime_error("Negative shift count");}highsum result=*this;highsum power(2);highsum count=other;while(count>highsum(0)){result=result*power;count=count-highsum(1);}return result;}highsum operator>>(const highsum& other)const{if(other.isNegative){throw std::runtime_error("Negative shift count");}highsum result=*this;highsum power(2);highsum count=other;while(count>highsum(0)){result=result/power;count=count-highsum(1);}return result;}highsum operator<<(int num)const{return*this<<highsum(num);}highsum operator<<(long long num)const{return*this<<highsum(num);}highsum operator>>(int num)const{return*this>>highsum(num);}highsum operator>>(long long num)const{return*this>>highsum(num);}highsum operator&(const highsum& other)const{std::string bin1=this->toBinaryString();std::string bin2=other.toBinaryString();std::string resultBin=bitwiseOperation(bin1,bin2,andOp);return fromBinaryString(resultBin);}highsum operator|(const highsum& other)const{std::string bin1=this->toBinaryString();std::string bin2=other.toBinaryString();std::string resultBin=bitwiseOperation(bin1,bin2,orOp);return fromBinaryString(resultBin);}highsum operator^(const highsum& other)const{std::string bin1=this->toBinaryString();std::string bin2=other.toBinaryString();std::string resultBin=bitwiseOperation(bin1,bin2,xorOp);return fromBinaryString(resultBin);}highsum operator&(int num)const{return*this&highsum(num);}highsum operator&(long long num)const{return*this&highsum(num);}highsum operator|(int num)const{return*this|highsum(num);}highsum operator|(long long num)const{return*this|highsum(num);}highsum operator^(int num)const{return*this^highsum(num);}highsum operator^(long long num)const{return*this^highsum(num);}highsum&operator+=(const highsum& other){*this=*this+other;return*this;}highsum&operator-=(const highsum& other){*this=*this-other;return*this;}highsum&operator*=(const highsum& other){*this=*this*other;return*this;}highsum&operator/=(const highsum& other){*this=*this/other;return*this;}highsum&operator%=(const highsum& other){*this=*this%other;return*this;}highsum&operator+=(int num){*this=*this+num;return*this;}highsum&operator-=(int num){*this=*this-num;return*this;}highsum&operator*=(int num){*this=*this*num;return*this;}highsum&operator/=(int num){*this=*this/num;return*this;}highsum&operator%=(int num){*this=*this%num;return*this;}highsum&operator+=(long long num){*this=*this+num;return*this;}highsum&operator-=(long long num){*this=*this-num;return*this;}highsum&operator*=(long long num){*this=*this*num;return*this;}highsum&operator/=(long long num){*this=*this/num;return*this;}highsum&operator%=(long long num){*this=*this%num;return*this;}bool operator<(const highsum& other)const{if(isNegative!=other.isNegative){return isNegative;}if(value.length()!=other.value.length()){return(value.length()<other.value.length())^isNegative;}return(value<other.value)^isNegative;}bool operator>(const highsum& other)const{return other<*this;}bool operator<=(const highsum& other)const{return!(*this>other);}bool operator>=(const highsum& other)const{return!(*this<other);}bool operator==(const highsum& other)const{return value==other.value&&isNegative==other.isNegative;}bool operator!=(const highsum& other)const{return!(*this==other);}bool operator<(int num)const{return*this<highsum(num);}bool operator<(long long num)const{return*this<highsum(num);}bool operator>(int num)const{return*this>highsum(num);}bool operator>(long long num)const{return*this>highsum(num);}bool operator<=(int num)const{return*this<=highsum(num);}bool operator<=(long long num)const{return*this<=highsum(num);}bool operator>=(int num)const{return*this>=highsum(num);}bool operator>=(long long num)const{return*this>=highsum(num);}bool operator==(int num)const{return*this==highsum(num);}bool operator==(long long num)const{return*this==highsum(num);}bool operator!=(int num)const{return*this!=highsum(num);}bool operator!=(long long num)const{return*this!=highsum(num);}friend std::ostream&operator<<(std::ostream& os,const highsum& num){if(num.isNegative){os<<'-';}os<<num.value;return os;}friend std::istream&operator>>(std::istream& is,highsum& num){std::string s;is>>s;num=highsum(s);return is;}std::string to_string()const{return(isNegative?"-":"")+value;}};
struct segTree {struct segTree_Node {int sum, tag;int ls = -1, rs = -1;};const segTree_Node nulltree = {0, 0, 0, 0};vector<segTree_Node> tree;int root = -1;segTree_Node merge_tree(segTree_Node m1, segTree_Node m2) {segTree_Node m3;m3.sum = m1.sum + m2.sum;return m3;}segTree_Node merge_tree(int rt1, int rt2) {if (rt1 == -1) return tree[rt2];else if (rt2 == -1) return tree[rt1];else return merge_tree(tree[rt1], tree[rt2]);}void add_tag(int dl, int dr, int &rt, long long k) {if (rt == -1) rt = tree.size(), tree.push_back(nulltree);tree[rt].tag = (tree[rt].tag + k);tree[rt].sum = (tree[rt].sum + (dr - dl + 1) * k);}void down_update(int dl, int dr, int rt) {if (tree[rt].tag != 0) {int mid = (dl + dr) >> 1;add_tag(dl, mid, tree[rt].ls, tree[rt].tag);add_tag(mid + 1, dr, tree[rt].rs, tree[rt].tag);tree[rt].tag = 0;}}void begin_tree(int dl, int dr, int &rt, long long *arr) {if (rt == -1) rt = tree.size(), tree.push_back(nulltree);tree[rt].tag = 0;if (dl == dr) {tree[rt].sum = arr[dl];return;}int mid = (dl + dr) >> 1;begin_tree(dl, mid, tree[rt].ls, arr);begin_tree(mid + 1, dr, tree[rt].rs, arr);tree[rt] = merge_tree(tree[rt].ls, tree[rt].rs);return;}segTree_Node print_tree(int dl, int dr, int rt, int l, int r) {if (rt == -1) return nulltree;if (l <= dl && dr <= r) return tree[rt];down_update(dl, dr, rt);int mid = (dl + dr) >> 1;segTree_Node res = nulltree;if (l <= mid) res = merge_tree(print_tree(dl, mid, tree[rt].ls, l, r), res);if (r > mid) res = merge_tree(res, print_tree(mid + 1, dr, tree[rt].rs, l, r));return res;}};
using namespace better_function;
int main(){
	
	return 0;
}