跳到正文

个人记录

模板

StelChord约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月13日
更新
2025年12月13日
篇幅
约 11 分钟

版本

  1. v1当前

高精度

C++
struct bint{vector<int>digits;bool negative=false;bint(){}bint(int n){if(n==0)digits.push_back(0);else{if(n<0){negative=true;n=-n;}while(n>0){digits.push_back(n%10);n/=10;}}}bint(const string&s){int start=0;if(s[0]=='-'){negative=true;start=1;}for(int i=s.length()-1;i>=start;i--){digits.push_back(s[i]-'0');}removeLeadingZeros();}void removeLeadingZeros(){while(digits.size()>1&&digits.back()==0)digits.pop_back();if(digits.size()==1&&digits[0]==0)negative=false;}string toString()const{string s;if(negative&&!(digits.size()==1&&digits[0]==0))s+='-';for(int i=digits.size()-1;i>=0;i--)s+=char(digits[i]+'0');return s;}bool absLess(const bint&other)const{if(digits.size()!=other.digits.size())return digits.size()<other.digits.size();for(int i=digits.size()-1;i>=0;i--)if(digits[i]!=other.digits[i])return digits[i]<other.digits[i];return false;}bool operator<(const bint&other)const{if(negative!=other.negative)return negative;if(negative)return absLess(other)?false:true;return absLess(other);}bool operator>(const bint&other)const{return other<*this;}bool operator<=(const bint&other)const{return!(other<*this);}bool operator>=(const bint&other)const{return!(*this<other);}bool operator==(const bint&other)const{return digits==other.digits&&negative==other.negative;}bool operator!=(const bint&other)const{return!(*this==other);}bint operator-()const{bint result=*this;if(!(digits.size()==1&&digits[0]==0))result.negative=!negative;return result;}bint operator+(const bint&other)const{if(negative==other.negative){bint result=addAbsolute(*this,other);result.negative=negative;return result;}else{if(absLess(other)){bint result=subtractAbsolute(other,*this);result.negative=other.negative;return result;}else{bint result=subtractAbsolute(*this,other);result.negative=negative;return result;}}}bint operator-(const bint&other)const{return*this+(-other);}bint operator*(const bint&other)const{bint result=multiplyAbsolute(*this,other);result.negative=(negative!=other.negative);result.removeLeadingZeros();return result;}static bint addAbsolute(const bint&a,const bint&b){bint result;int carry=0;int maxSize=max(a.digits.size(),b.digits.size());for(int i=0;i<maxSize||carry;i++){int sum=carry;if(i<a.digits.size())sum+=a.digits[i];if(i<b.digits.size())sum+=b.digits[i];result.digits.push_back(sum%10);carry=sum/10;}return result;}static bint subtractAbsolute(const bint&a,const bint&b){bint result;int borrow=0;for(int i=0;i<a.digits.size();i++){int diff=a.digits[i]-borrow;if(i<b.digits.size())diff-=b.digits[i];if(diff<0){diff+=10;borrow=1;}else borrow=0;result.digits.push_back(diff);}result.removeLeadingZeros();return result;}static bint multiplyAbsolute(const bint&a,const bint&b){bint result;result.digits.resize(a.digits.size()+b.digits.size(),0);for(int i=0;i<a.digits.size();i++){int carry=0;for(int j=0;j<b.digits.size()||carry;j++){long long product=result.digits[i+j]+carry;if(j<b.digits.size())product+=a.digits[i]*(long long)b.digits[j];result.digits[i+j]=product%10;carry=product/10;}}result.removeLeadingZeros();return result;}};ostream&operator<<(ostream&os,const bint&bi){os<<bi.toString();return os;}istream&operator>>(istream&is,bint&bi){string s;is>>s;bi=bint(s);return is;}bint max(const bint&a,const bint&b){return(a<b)?b:a;}bint min(const bint&a,const bint&b){return(a<b)?a:b;}bint abs(const bint&a){bint result=a;result.negative=false;return result;}

带修莫队

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int maxn=1000005;
int n,m,k,a[maxn],b[maxn];
int ans[maxn],cnt;
struct node{
	int l,r,d,tim;
}md[maxn];
struct chg{
	int pos,val;
}c[maxn];
void add(int val){
	if(++b[val]==1) cnt++;
}
void del(int val){
	if(--b[val]==0) cnt--;
}
void ch(int tim,int i){
	if(c[tim].pos>=md[i].l&&c[tim].pos<=md[i].r){
		b[a[c[tim].pos]]--;
		b[c[tim].val]++;
		if(b[a[c[tim].pos]]==0) cnt--;
		if(b[c[tim].val]==1) cnt++;
	}
	swap(c[tim].val,a[c[tim].pos]);
}
int fk;
bool cmp(const node &a,const node &b){
	//return a.l/fk==b.l/fk?a.r/fk==b.r/fk?a.tim<b.tim:a.r<b.r:a.l<b.l;
	if(a.l/fk==b.l/fk){
		if(a.r/fk==b.r/fk) return a.tim<b.tim;
		return a.r<b.r;
	}
	return a.l<b.l;
}
int qm,cm;
char opt;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=m;i++){
		cin>>opt;
		if(opt=='Q'){
			++qm;
			cin>>md[qm].l>>md[qm].r;
			md[qm].d=qm;md[qm].tim=cm;
		}else{
			++cm;
			cin>>c[cm].pos>>c[cm].val;
		}
	}
	fk=pow(n,0.666);
	//fk=sqrt(n);
	sort(md+1,md+qm+1,cmp);
	int sl=1,sr=0,l,r,d,ti,now=0;
	for(int i=1;i<=qm;i++){
		l=md[i].l;r=md[i].r;d=md[i].d;ti=md[i].tim;
		while(sl>l) add(a[--sl]);
		while(sr<r) add(a[++sr]);
		while(sl<l) del(a[sl++]);
		while(sr>r) del(a[sr--]);
		while(ti>now) ch(++now,i);
		while(ti<now) ch(now--,i);
		ans[d]=cnt;
	}
	for(int i=1;i<=qm;i++) cout<<ans[i]<<endl;
	return 0;
}

普通莫队

C++
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int maxn=50005;
int n,m,k,a[maxn],b[maxn];
int ans[maxn],cnt;
struct node{
	int l,r,d;
}md[maxn];
bool cmp(node a1,node a2){
	int fk=sqrt(n);
	if(a1.l/fk==a2.l/fk) return a1.r<a2.r;
	return a1.l/fk<a2.l/fk;
}
void add(int x){
	cnt+=2*b[x]+1;
	b[x]++;
}
void del(int x){
	cnt-=2*b[x]-1;
	b[x]--;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=m;i++) cin>>md[i].l>>md[i].r,md[i].d=i;
	sort(md+1,md+m+1,cmp);
	int sl=1,sr=0,l,r;
	for(int i=1;i<=m;i++){
		l=md[i].l;r=md[i].r;
		while(sl>l) add(a[--sl]);
		while(sr<r) add(a[++sr]);
		while(sl<l) del(a[sl++]);
		while(sr>r) del(a[sr--]);
		ans[md[i].d]=cnt;
	}
	for(int i=1;i<=m;i++) cout<<ans[i]<<endl;
	return 0;
}
原文