树状数组
C++ 复制 int tr[N];
int lowbit (int x ){return x&- x;}
void add (int p ,int v ){for (;p<= n;p+= lowbit (p))tr[p]+= v;}
int ask (int p ){int s= 0 ;for (;p;p-= lowbit (p))s+= tr[p];return s;}
int query (int l ,int r ){return ask (r)- ask (l- 1 );}
高精度
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
struct BigInt {
vector<int> num;
bool neg;
BigInt (){num= {0 };neg= 0 ;}
BigInt (string s ){
neg= 0 ;
if (s[0 ]== '-' )neg= 1 ,s= s.substr (1 );
for (int i= s.size ()- 1 ;i>= 0 ;i-- )num.push_back (s[i]- '0' );
trim ();
}
BigInt (long long x ){
neg= 0 ;
if (x< 0 )neg= 1 ,x=- x;
while (x)num.push_back (x% 10 ),x/= 10 ;
if (num.empty ())num= {0 };
}
void trim (){
while (num.size ()> 1 && num.back ()== 0 )num.pop_back ();
if (num.size ()== 1 && num[0 ]== 0 )neg= 0 ;
}
bool isZero ()const {return num.size ()== 1 && num[0 ]== 0 ;}
int cmpAbs (const BigInt & b )const {
if (num.size ()!= b.num.size ())return num.size ()< b.num.size ()?- 1 : 1 ;
for (int i= num.size ()- 1 ;i>= 0 ;i-- )if (num[i]!= b.num[i])return num[i]< b.num[i]?- 1 : 1 ;
return 0 ;
}
bool operator < (const BigInt & b )const {
if (neg!= b.neg)return neg;
return neg? cmpAbs (b)> 0 : cmpAbs (b)< 0 ;
}
bool operator > (const BigInt & b )const {return b<* this ;}
bool operator <= (const BigInt & b )const {return ! (* this > b);}
bool operator >= (const BigInt & b )const {return ! (* this < b);}
bool operator == (const BigInt & b )const {return ! (* this < b)&&! (* this > b);}
bool operator != (const BigInt & b )const {return ! (* this == b);}
BigInt abs ()const {BigInt res=* this ;res.neg= 0 ;return res;}
BigInt operator - ()const {BigInt res=* this ;res.neg=! neg;res.trim ();return res;}
BigInt operator + (const BigInt & b )const {
if (neg)return - (- (* this )+ (- b));
if (b.neg)return * this - (- b);
BigInt res;
int n= max (num.size (),b.num.size ()),carry= 0 ;
res.num.assign (n,0 );
for (int i= 0 ;i< n;i++ ){
if (i< num.size ())carry+= num[i];
if (i< b.num.size ())carry+= b.num[i];
res.num[i]= carry% 10 ;
carry/= 10 ;
}
if (carry)res.num.push_back (carry);
return res;
}
BigInt operator - (const BigInt & b )const {
if (neg)return - (- (* this )- (- b));
if (b.neg)return * this + (- b);
if (* this < b)return - (b-* this );
BigInt res;
int n= num.size (),borrow= 0 ;
res.num.assign (n,0 );
for (int i= 0 ;i< n;i++ ){
borrow+= num[i];
if (i< b.num.size ())borrow-= b.num[i];
res.num[i]= (borrow+ 10 )% 10 ;
borrow= borrow< 0 ?- 1 : 0 ;
}
res.trim ();
return res;
}
BigInt operator * (const BigInt & b )const {
BigInt res;
res.neg= neg^ b.neg;
res.num.assign (num.size ()+ b.num.size (),0 );
for (int i= 0 ;i< num.size ();i++ ){
int carry= 0 ;
for (int j= 0 ;j< b.num.size ()|| carry;j++ ){
long long tmp= res.num[i+ j]+ 1 LL* num[i]* (j< b.num.size ()? b.num[j]: 0 )+ carry;
res.num[i+ j]= tmp% 10 ;
carry= tmp/ 10 ;
}
}
res.trim ();
return res;
}
BigInt operator / (const BigInt & b )const {
if (b.isZero ())throw runtime_error ("Division by zero" );
BigInt res,cur;
res.neg= neg^ b.neg;
res.num.assign (num.size (),0 );
BigInt absB= b.abs ();
for (int i= num.size ()- 1 ;i>= 0 ;i-- ){
cur.num.insert (cur.num.begin (),num[i]);
cur.trim ();
int l= 0 ,r= 9 ,ans= 0 ;
while (l<= r){
int mid= (l+ r)/ 2 ;
if (absB* BigInt (mid)<= cur)ans= mid,l= mid+ 1 ;
else r= mid- 1 ;
}
res.num[i]= ans;
cur= cur- absB* BigInt (ans);
}
res.trim ();
return res;
}
BigInt operator % (const BigInt & b )const {
return * this - (* this / b)* b;
}
friend istream &operator >> (istream & is ,BigInt & b ){
string s;is>> s;b= BigInt (s);return is;
}
friend ostream &operator << (ostream & os ,const BigInt & b ){
if (b.neg)os<< '-' ;
for (int i= b.num.size ()- 1 ;i>= 0 ;i-- )os<< b.num[i];
return os;
}
};