跳到正文

个人记录

关心一下自己的学习

一座城·守一人约 5 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

P2068 统计和

C++
#include<bits/stdc++.h>
using namespace std;
int n,w,a[100005];
int lowbit(int a){
	return a&-a;
}
void add(int p,int x){
	while(p<=n){
		a[p]+=x;
		p+=lowbit(p);
	}
}
int ask(int p){
	int ans=0;
	while(p>0){
		ans+=a[p];
		p-=lowbit(p);
	}
	return ans;
}
int main(){
	cin>>n>>w;
	for(register int i=1;i<=w;i++){
		char q;
		int y,z;
		cin>>q>>y>>z;
		if(q=='x')add(y,z);
		else cout<<ask(z)-ask(y-1)<<endl;
	}
	return 0;
}

P1083 借教室

C++
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,i,j,mid,l,r;
int p[1000001],d[1000001],s[1000001],t[1000001],alpha[1000001];

bool check(int there){
    memset(alpha,0,sizeof(alpha));
    for(int i=1; i <= there; i++)
    {
        alpha[s[i]] += d[i];
        alpha[t[i]+1] -= d[i];
    }
    register int all=0;
    for(int i=1; i <= n; i++)
    {
        all += alpha[i];
        if(all>p[i]) return false;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1; i <= n; i++)
        cin>>p[i];
    for(int i=1; i <= m; i++)
        cin>>d[i]>>s[i]>>t[i];
    l=0;
    r=m+1;
    mid=(l+r)/2;
    while(l+1 < r){
        if(check(mid)) l=mid;
        else r=mid;
        mid=(l+r)/2;
    }
    if(l == m) cout<<"0";
    else{
        cout<<"-1"<<endl;
        cout<<l+1<<endl;
    }
    return (0^0);
}

P3375 【模板】KMP字符串匹配

C++
#include<bits/stdc++.h>
using namespace std;
char a[1000005],b[1000005];
int p[1000005];
int main() {
    cin>>a+1;
    cin>>b+1;
    int lena=strlen(a+1),lenb=strlen(b+1);
    int j=0;
    p[1]=0;
    for(register int i=2; i<=lenb; i++) {
        while(j>0&&b[i]!=b[j+1])j=p[j];
        if(b[i]==b[j+1])j++;
        p[i]=j;
    }
    j=0;
    for(register int i=1; i<=lena; i++) {
        while(j>0&&a[i]!=b[j+1])j=p[j];
        if(a[i]==b[j+1])j++;
        if(j==lenb)cout<<(i-lenb+1)<<endl,j=p[j];
    }
    for(register int i=1; i<lenb; i++)
        cout<<p[i]<<" ";
    cout<<p[lenb];
    return 0;
}

f(x)={12n2Γ(n2)xn2−1e−x2,x>0;0,x≤0f(x) = \begin{cases} \dfrac{1}{2^{ \frac{n}{2} }\Gamma ( \frac{n}{2}) }x^{\frac{n}{2} -1} e^{-\frac{x}{2}} , x>0; \\0,x \leq 0 \end{cases}

Γ(x)=∫0+∞tx−1e−t dt \Gamma (x) = \int_{0}^{+ \infty} t^{x-1}e^{-t}\, dt

∫abf(x)dx≈(b−a)(f(a)+f(b)+4f(a+b2)6 \int _a ^b f(x) d x \approx \dfrac{(b-a)(f(a)+f(b)+4f( \frac{a+b}{2} )}{6}

xσ=∑i=1nximi∑i=1nmix_{ \sigma} = \dfrac { \sum _{ i=1 } ^ { n } x_i m_i}{ \sum_ {i=1}^{n}m_i}

yσ=∑i=1nyimi∑i=1nmiy_{ \sigma} = \dfrac { \sum _{ i=1 }^{n}y_i m_i}{\sum_{i=1}^{n}m_i}

原文