跳到正文

个人记录

zj

AK_NOIMPOS约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年8月10日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 5 分钟

版本

  1. v1当前

清点人数 板子直接秒

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
#define int long long
#define lowbit(x) ((x)&(-x))
int n,m;
int	tr[N];
void fix(int x,int c){
	for(int i=x;i<=n;i+=lowbit(i)){
		tr[i]+=c;
	}
}
int ask(int x){
	int ans=0;
	for(int i=x;i>=1;i-=lowbit(i)){
		ans+=tr[i];
	}
	return ans;
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		char opt;
		cin>>opt;
		if(opt=='A'){
			int x;
			cin>>x;
			cout<<ask(x)<<endl;
		}
		if(opt=='B'){
			int x,c;
			cin>>x>>c;
			fix(x,c);
		}
		if(opt=='C'){
			int x,c;
			cin>>x>>c;
			fix(x,-c);
		}
	}
	return 0;
} 

计算 线段树单点修改,区间查询

C++
v#include<bits/stdc++.h>
using namespace std;
#define lc p<<1
#define rc p<<1|1
const int N=1e5+10;
#define int long long

int n,mo,op;
struct node{
	int l,r,sum;
}tr[N*4];
void build(int p,int l,int r){
	tr[p]={l,r,1ll};
	if(l==r)return;
	int m=(l+r)>>1;
	build(lc,l,m);
	build(rc,m+1,r);
	tr[p].sum=tr[lc].sum*tr[rc].sum;
	tr[p].sum%=mo;
//	cout<<tr[p].sum<<"\n";
}

void update1(int p,int x,int k){
	if(tr[p].l==x&&tr[p].r==x){
		tr[p].sum=k;
		return ;
	}
	int m=tr[p].l+tr[p].r>>1;
	if(x<=m)update1(lc,x,k);
	if(x>m)update1(rc,x,k);
	tr[p].sum=tr[lc].sum*tr[rc].sum;
	tr[p].sum%=mo;
}
int t;
signed main(){
	int x,y,k;
	cin>>t;
	while(t--){
		cin>>n>>mo;
		build(1,1,n);
		for(int i=1;i<=n;i++){
			cin>>op>>k;
			if(op==1){
				update1(1,i,k);
			}else {
				update1(1,k,1);
			}
			cout<<tr[1].sum<<endl;
		}
	}
	
	return 0;
}

Bus 值域树状数组加上动态规划,区间里面维护Dp数值

C++
#include<bits/stdc++.h>
using namespace  std;
#define lowbit(x) ((x)&(-x))
#define int long long 
const int N=1e5+10;
int tr[N];
int n,dp[N];
struct node{
	int x,y,w;
}a[N];
int maxn=0;
bool cmp(node a,node b){
	if(a.x==b.x)return a.y<b.y;
	return a.x<b.x;
}
void update(int x,int y){
	for(int i=x;i<=N-1;i+=lowbit(i)){
		tr[i]=max(tr[i],y);
	}
}
int query(int x){
	int sum=0;
	for(int i=x;i>=1;i-=lowbit(i)){
		sum=max(tr[i],sum);
	}
	return sum;
}
int m;
int k;
int b[N];
signed main(){
	cin>>n>>m>>k;
	for(int i=1;i<=k;i++){
		int x,y,w;
		cin>>x>>y>>w;
		a[i]={x,y,w};
		b[i]=a[i].y;
	}
	sort(b+1,b+1+k); 
	sort(a+1,a+k+1,cmp);
	int num=unique(b+1,b+k+1)-b-1;
	for(int i=1;i<=k;i++){
		a[i].y=lower_bound(b+1,b+num+1,a[i].y)-b;
	}
	for(int i=1;i<=k;i++){
		dp[i]=query(a[i].y)+a[i].w;
		update(a[i].y,dp[i]);
		maxn=max(maxn,dp[i]);
	}
	cout<<maxn;
	return 0;
}

原文