目录
树状数组
1.
#include<bits/stdc++.h>
using namespace std;
#define MAX 500005
int s[MAX];
int n,m;
int lowbit(int x){//提取x的低位2次幂
return x&-x;
}
void change(int x,int k){//后修
while(x<=n)
{
s[x]+=k;
x+=lowbit(x);
}
}
int query(int x){//向前查,前缀和
int t=0;
while(x){
t+=s[x];
x-=lowbit(x);
}
return t;
}
void solve(){
cin>>n>>m;
int num;
for(int i=1;i<=n;i++){
cin>>num;
change(i,num);
}
while(m--){
int t;
cin>>t;
if(t==1){
int x,k;
cin>>x>>k;
change(x,k);
}
else if(t==2){
int x,y;
cin>>x>>y;
cout<<query(y)-query(x-1)<<endl;
}
}
}
int main(){
int T=1;
//cin>>T;
while(T--){
solve();
}
return 0;
}
2.
#include<bits/stdc++.h>
using namespace std;
#define MAX 500005
int num[MAX],nums[MAX];
int n,m,x,y,k;
int lowbit(int x){
return x&-x;
}
void change(int x,int k){
while(x<=n){
nums[x]+=k;
x+=lowbit(x);
}
}
int query(int x){
int sum=0;
while(x){
sum+=nums[x];
x-=lowbit(x);
}
return sum;
}
void solve(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>num[i];
}
while(m--){
int op;
cin>>op;
if(op==1){
cin>>x>>y>>k;
change(x,k);
change(y+1,-k);//差分
}
else{
cin>>x;
cout<<num[x]+query(x)<<endl;
}
}
}
int main(){
int T=1;
//cin>>T;
while(T--){
solve();
}
return 0;
}
最短路径
1.Dijkstra
//Dijkstra(朴素算法O(n^2))
#include<bits/stdc++.h>
using namespace std;
#define MAX 101
#define edgeType int
#define inf 0x3f3f3f3f
edgeType graph[MAX][MAX];
void initEdges(int n){//初始化
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
graph[i][j]=inf;
}
}
}
void addEdge(int u,int v,edgeType w){//加边
if(graph[u][v]==inf||w<graph[u][v]){
graph[u][v]=w;
}
}
void dijkstra(int n,int s,edgeType dist[MAX]){//n为顶点个数,s为起点,dist为返回出去的数组
bool visited[MAX];
for(int i=0;i<n;i++){//初始化
visited[i]=false;
dist[i]=inf;
}
dist[s]=0;
while(1){
edgeType minDist=inf;
int minIndex;
for(int i=0;i<n;i++){
if(visited[i]){//访问过
continue;
}
if(minDist==inf||dist[i]<minDist){
minDist=dist[i];
minIndex=i;
}
}
if(minDist==inf){
break;
}
visited[minIndex]=true;
for(int i=0;i<n;i++){
if(visited[i]==true){
continue;
}
int dis=graph[minIndex][i];
if(dis==inf){
continue;
}
if(dist[i]==inf||dist[minIndex]+dis<dist[i]){
dist[i]=dist[minIndex]+dis;
}
}
}
}
void solve(){
/*
1.先初始化
2.构边
3.套dijkstra;
4.根据题目写
*/
}
int main(){
int T=1;
//cin>>T;
while(T--){
solve();
}
return 0;
}
2.BellmanFord
#include <iostream>
using namespace std;
#define MAX 20005
#define inf 0x3f3f3f3f
#define edgeType int
struct edgeTu{
int u,v;
edgeType w;
};
int pdRelax(int m,edgeTu* edges,edgeType* dist){
int isRelax=0;
for(int i=0;i<m;i++){
int u=edges[i].u;
int v=edges[i].v;
edgeType w=edges[i].w;
if(dist[u]+w<dist[v]){//松弛操作
dist[v]=dist[u]+w;
isRelax=1;
}
}
return isRelax;
}
int bellman(int n,int m,edgeTu* edges,int s,edgeType* dist){
for(int i=0;i<n;i++){
dist[i]=(i==s?0:inf);
}
for(int i=0;i<n-1;i++){
if(!pdRelax(m,edges,dist)){
return 0;
}
}
return pdRelax(m,edges,dist);
}
int n,e,m;
edgeTu edges[MAX];
edgeType dist[MAX];
int c[MAX];
int main()
{
cin>>n>>e;
for(int i=0;i<n;i++){
cin>>c[i];
}
c[n-1]=0;
m=0;
for(int i=0;i<e;i++){//有向图
int u,v,w;
cin>>u>>v>>w;
edges[m].u=u-1;
edges[m].v=v-1;
edges[m].w=w+c[v-1];
m++;
edges[m].u=v-1;
edges[m].v=u-1;
edges[m].w=w+c[u-1];
m++;
}
bellman(n,m,edges,0,dist);
cout<<dist[n-1]<<endl;
return 0;
}