noip前打板子 qwq

在某咕上打了一晚上的模板

感觉还好。。。

#include<bits/stdc++.h>
#define LL long long
using namespace std;
inline int read()
{
    int x = 0,f = 1;char ch = getchar();
    for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
    for(;isdigit(ch);ch = getchar())x = 10 * x + ch - '0';
    return x * f;
}
int n;
priority_queue<int> q;
int main()
{
    n = read();
    while(n--)
    {
        int opt = read();
        if(opt == 1)q.push(-read());
        else if(opt == 2)printf("%d\n",-q.top());
        else q.pop();
    }
}
小根堆(滑稽)
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long
using namespace std;
inline int read()
{
    int x = 0,f = 1;char ch = getchar();
    for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
    for(;isdigit(ch);ch = getchar())x = 10 * x + ch - '0';
    return x * f;
}
const int maxn = 15010;
int n,m;
int first[maxn],to[maxn],nx[maxn],val[maxn],cnt;
int inq[maxn],dis[maxn],cq[maxn],vis[maxn];
int spfa(int s)
{
    memset(inq,0,sizeof(inq));
    memset(cq,0,sizeof(cq));
    memset(dis,127,sizeof(dis));
    queue<int> q;
    dis[s] = 0;q.push(s);
    cq[s] = 1;
    while(!q.empty())
    {
        int now = q.front();q.pop();
        inq[now] = 0;
        for(int i=first[now];i;i=nx[i])
        {
            if(dis[to[i]] > dis[now] + val[i])
            {
                dis[to[i]] = dis[now] + val[i];
                cq[to[i]] = cq[now] + 1;
                if(cq[to[i]] > n)return 1;
                if(!inq[to[i]])
                {
                    inq[to[i]] = 1;
                    q.push(to[i]);
                }
            }
        }
    }return 0;
}
int main()
{
    //freopen("testdata.in","r",stdin);
//    freopen("testdata.ans","w",stdout);
    int T = read();
    while(T--)
    {
        n = read(),m = read();
        cnt = 0;memset(first,0,sizeof(first));
        for(int i=1;i<=m;i++)
        {
            int u = read(),v = read(),w = read();
            to[++cnt] = v,nx[cnt] = first[u],first[u] = cnt,val[cnt] = w;
            if(w >= 0)to[++cnt] = u,nx[cnt] = first[v],first[v] = cnt,val[cnt] = w;
        }
        if(spfa(1))cout<<"YE5\n";
        else cout<<"N0\n";
    }
}
spfa判断负环,判断一个点是否松弛超过n次,比判入队n次快
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long
using namespace std;
inline int read()
{
    int x = 0,f = 1;char ch = getchar();
    for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
    for(;isdigit(ch);ch = getchar())x = 10 * x + ch - '0';
    return x * f;
}
const int maxn = 100010;
int n,m;
int f[maxn][23],lg[maxn];
int main()
{
    n = read(),m = read();
    lg[0] = -1;
    for(int i=1;i<=n;i++)f[i][0] = read(),lg[i] = lg[i >> 1] + 1;
    for(int j=1;j<=22;j++)
        for(int i=1;i+(1 << j) - 1 <= n;i++)
            f[i][j] = max(f[i][j - 1],f[i + (1 << (j - 1))][j - 1]);
    while(m--)
    {
        int l = read(),r = read();
        printf("%d\n",max(f[l][lg[r - l + 1]],f[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]));
    }
}
ST表
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 500010,mod = 998244353,M = 499122177,G = 3;
int n,L,num,R[maxn],a[maxn],b[maxn],c[maxn],d[maxn];
int poly[maxn],inv[maxn];
inline int read()
{
    int x = 0,f = 1;char ch = getchar();
    for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
    for(;isdigit(ch);ch = getchar())x = 10 * x + ch - '0';
    return x * f;
}
inline int ksm(int x,int t)
{
    int res = 1;
    while(t)
    {
        if(t & 1) res = 1LL * res * x % mod;
        x = 1LL * x * x % mod;
        t >>= 1;
    }
    return res;
}
inline void NTT(int *a,int f,int n,int L)
{
    for(int i=0;i<n;i++) R[i] = (R[i>>1] >> 1) | ((i & 1) << (L - 1));
    for(int i=0;i<n;i++)if(i < R[i])swap(a[i],a[R[i]]);
    for(int i=1;i<n;i<<=1)
    {
        int wn = ksm(G,(mod - 1) / (i << 1));
        if(f == -1)wn = ksm(wn,mod - 2);
        for(int j=0;j<n;j+=(i<<1))
        {
            int w = 1;
            for(int k=0;k<i;k++,w=1LL * w * wn % mod)
            {
                int x = a[j + k], y = 1LL * w * a[j + k + i ] % mod;
                a[j + k] = ((x + y) % mod + mod) % mod;
                a[j + k + i] = ((x - y) % mod + mod) % mod;
            }
        }
    }
    if(f == -1)
    {
        int inv = ksm(n,mod - 2);
        for(int i=0;i<n;i++)a[i] = 1LL * a[i] * inv % mod;
    }
}
inline void inverse(int *a,int *b,int n,int L)
{
    if(n == 1){b[0] = ksm(a[0],mod - 2);return;}
    inverse(a,b,n>>1,L-1);
    memcpy(c,a,n*sizeof(int));memset(c+n,0,n*sizeof(int));
    NTT(c,1,n<<1,L+1);NTT(b,1,n<<1,L+1);
    for(int i=0;i<(n<<1);i++) b[i] = 1LL * b[i] * ((2 - 1LL * c[i] * b[i] % mod + mod) % mod) % mod;
    NTT(b,-1,n<<1,L+1);memset(b+n,0,n*sizeof(int));
}
inline void sqrt(int *a,int *b,int n,int L)
{
    if(n == 1){b[0] = 1;return;}
    sqrt(a,b,n>>1,L-1);memset(d,0,n*2*sizeof(int));inverse(b,d,n,L);
    memcpy(c,a,n*sizeof(int));memset(c+n,0,n*sizeof(int));
    NTT(c,1,n<<1,L+1);NTT(b,1,n<<1,L+1);NTT(d,1,n<<1,L+1);
    for(int i=0;i<n<<1;i++) b[i] = (1LL * c[i] * d[i] % mod + b[i]) %mod * M % mod;
    NTT(b,-1,n<<1,L+1);memset(b+n,0,n*sizeof(int));
}
signed main()
{
    n = read();
    for(int i=1;i<n;i++)poly[i] = read();
    int m;
    for(m=n,n=1;n<=m;n<<=1) L++;
    for(int i=1;i<n;i++)poly[i] = mod - poly[i];
    (poly[0] += 1) %= mod;
    inverse(poly,inv,n,L);
    for(int i=0;i<m;i++)printf("%lld ",inv[i]);
}
用多项式求逆搞的分治FFT板子
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
    int x = 0,f = 1;char ch = getchar();
    for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
    for(;isdigit(ch);ch = getchar())x = 10 * x + ch - '0';
    return x * f;
}
const int maxn = 3e6 + 10;
int n,p;
int inv[maxn];
inline int inverse(int x)
{
    int res = 1,t = p - 2;
    while(t)
    {
        if(t & 1)res = res * x % p;
        x = x * x % p;
        t = t >> 1;
    }
    return res;
}
signed main()
{
    n = read(),p = read();puts("1");inv[1] = 1;
    for(int i=2;i<=n;i++)
    {
        printf("%d\n",(inv[i] = ((p - p / i) * inv[p % i]) % p));
        //printf("%d\n",inverse(i));
    }
}
逆元
// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#define LL long long
using namespace std;namespace IO {
    const int iL = 1<<18;
    char _buf[iL],*S,*T;
    #define gc (S==T?(T=(S=_buf)+fread(_buf,1,iL,stdin),S==T?0:*S++):*S++)
    template<class _Tp> inline void gi(_Tp&x){
        x=0;int f=1;char ch=gc;while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc;}
        while(ch>='0'&&ch<='9')x=x*10+(ch^48),ch=gc;x*=f;
    }
    char _o[25],*__o;
    template<class _Tp> inline void oi(_Tp x,char _y=0){
        if(x==0){putchar('0');if(_y)putchar(_y);}
        else {
            if(x<0) putchar('-'), x=-x;
            __o = _o+24; if(_y) *--__o = _y;
            while(x) *--__o = x%10+48, x/=10;
            fwrite(__o,1,_o+24-__o,stdout);
        }
    }
    
    inline char getc(){char ch;while((ch=gc)!='A'&&ch!='C');return ch;}
} ;
const int maxn = 1e6 + 10;
int n,k;
char s[maxn];
namespace Suffix_Array
{
    #define equ(x) (y[sa[i] + x] == y[sa[i - 1] + x])
    int rnk[maxn],tmp[maxn],sa[maxn],hei[maxn];
    int *x,*y;
    int wa[maxn],wb[maxn],wc[maxn];
    void radix_sort(int m)
    {
        memset(wc+1,0,sizeof(int)*m);
        for(int i=1;i<=n;++i)++wc[x[y[i]]];
        for(int i=1;i<=m;++i)wc[i] += wc[i - 1];
        for(int i=n;i>=1;--i)sa[wc[x[y[i]]]--] = y[i];
    }
    void makesa(char *s,int n,int m)
    {
        x = wa,y = wb;
        for(int i=1;i<=n;++i)x[i] = s[i],y[i] = i;radix_sort(m);
        for(int j=1,p=0;j<=n;j<<=1,m = p,p = 0)
        {
            for(int i=n-j+1;i<=n;++i)y[++p] = i;
            for(int i=1;i<=n;++i)
                if(sa[i] > j)y[++p] = sa[i] - j;
            radix_sort(m);swap(x,y);x[sa[p = 1]] = 1;
            for(int i=2;i<=n;++i)x[sa[i]] = equ(0) && equ(j) ? p : ++p;
            if(p == n)break;
        }
    }
}
using namespace Suffix_Array;
int main()
{
    fgets(s + 1,1 << 20,stdin);
    n = strlen(s + 1);
    makesa(s,n,122);
    for(int i=1;i<=n;++i)IO::oi(sa[i],' ');
}
后缀排序
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
    int x = 0,f = 1;char ch = getchar();
    for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
    for(;isdigit(ch);ch = getchar())x = 10 * x + ch - '0';
    return x * f;
}
const int maxn = 100010;
int n,m,mod;
int a[maxn];
#define ls (x << 1)
#define rs ((x << 1) | 1)
int seg[maxn << 2],mutag[maxn << 2],adtag[maxn << 2];
inline void pushup(int x){seg[x] = (seg[ls] + seg[rs]) % mod;}
inline void build(int x,int l,int r)
{
    mutag[x] = 1,adtag[x] = 0;
    if(l == r)
    {
        seg[x] = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(ls,l,mid);build(rs,mid + 1,r);
    pushup(x);
}
inline void pushdown(int x,int l,int r)
{
    if(mutag[x] != 1)
    {
        (mutag[ls] *= mutag[x]) %= mod;(mutag[rs] *= mutag[x]) %= mod;
        (adtag[ls] *= mutag[x]) %= mod;(adtag[rs] *= mutag[x]) %= mod;
        (seg[ls] *= mutag[x]) %= mod;(seg[rs] *= mutag[x]) %= mod;
        mutag[x] = 1;
    }
    if(adtag[x])
    {
        int mid = (l + r) >> 1;
        (adtag[ls] += adtag[x]) %= mod;(adtag[rs] += adtag[x]) %= mod;
        (seg[ls] += (mid - l + 1) * adtag[x]) %= mod;
        (seg[rs] += (r - mid) * adtag[x]) %= mod;
        adtag[x] = 0;
    }
}
inline void modify(int x,int l,int r,int L,int R,int v,int type)
{
    if(L <= l && r <= R)
    {
        if(type == 2)
        {
            (seg[x] += (r - l + 1) * v) %= mod;
            (adtag[x] += v) %= mod;
        }
        if(type == 1)
        {
            (seg[x] *= v) %= mod;
            (adtag[x] *= v) %= mod;
            (mutag[x] *= v) %= mod;
        }
        return;
    }
    int mid = (l + r) >> 1;
    pushdown(x,l,r);
    if(L <= mid)modify(ls,l,mid,L,R,v,type);
    if(R > mid)modify(rs,mid + 1,r,L,R,v,type);
    pushup(x);
}
inline int query(int x,int l,int r,int L,int R)
{
    if(L <= l && r <= R)return seg[x];
    int mid = (l + r) >> 1,ans = 0;
    pushdown(x,l,r);
    if(L <= mid)(ans += query(ls,l,mid,L,R)) %= mod;
    if(R > mid)(ans += query(rs,mid + 1,r,L,R)) %= mod;
    return ans;
}
signed main()
{
    n = read(),m = read(),mod = read();
    for(int i=1;i<=n;i++)a[i] = read();
    build(1,1,n);
    while(m--)
    {
        int op = read(),l = read(),r = read();
        if(op == 1)modify(1,1,n,l,r,read(),op);
        else if(op == 2)modify(1,1,n,l,r,read(),op);
        else printf("%lld\n",query(1,1,n,l,r));
    }
}
线段树区间加,区间乘
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
    int x = 0,f = 1;char ch = getchar();
    for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
    for(;isdigit(ch);ch = getchar())x = 10 * x + ch - '0';
    return x * f;
}
const int maxn = 100010;
int n,m;
int a[maxn];
struct Graph
{
    int first[maxn],to[maxn << 1],nx[maxn << 1],cnt;
    inline void add(int u,int v)
    {
        to[++cnt] = v;
        nx[cnt] = first[u];
        first[u] = cnt;
    }
}G1,G2;
int dfn[maxn],low[maxn],stk[maxn],top,vis[maxn],bl[maxn],scc,size[maxn],_tim;
int ind[maxn],sum[maxn];
inline void Tarjan(int x)
{
    dfn[x] = low[x] = ++_tim;stk[++top] = x;vis[x] = 1;
    for(int i=G1.first[x];i;i=G1.nx[i])
    {
        int v = G1.to[i];
        if(!dfn[v])
        {
            Tarjan(v);
            low[x] = min(low[x],low[v]);
        }
        else if(vis[v])low[x] = min(low[x],dfn[v]);
    }
    if(dfn[x] == low[x])
    {
        int now = 0;scc++;
        while(now != x)
        {
            now = stk[top--];
            vis[now] = 0;
            bl[now] = scc;
            size[scc]++;
            sum[scc] += a[now];
        }
    }
}
inline void rebuild()
{
    for(int j=1;j<=n;j++)
        for(int i=G1.first[j];i;i=G1.nx[i])
            if(bl[j] != bl[G1.to[i]])
            {
                ind[bl[G1.to[i]]]++;
                G2.add(bl[j],bl[G1.to[i]]);
            }
}
int dp[maxn];
inline int dfs()
{
    queue<int> q;
    for(int i=1;i<=scc;i++)
        if(ind[i] == 0)
        {
            q.push(i);
            dp[i] = sum[i];
        }
    while(!q.empty())
    {
        int now = q.front();q.pop();
        for(int i=G2.first[now];i;i=G2.nx[i])
        {
            int v = G2.to[i];
            dp[v] = max(dp[v],dp[now] + sum[v]);
            ind[v]--;
            if(ind[v] == 0)q.push(v);
        }
    }
    int ans = 0;
    for(int i=1;i<=scc;i++)ans = max(ans,dp[i]);
    return ans;
}
signed main()
{
    n = read(),m = read();
    for(int i=1;i<=n;i++)a[i] = read();
    for(int i=1;i<=m;i++)
    {
        int u = read(),v = read();
        G1.add(u,v);
    }
    for(int i=1;i<=n;i++)
        if(!dfn[i])Tarjan(i);
    rebuild();
    cout<<dfs();
}
Tarjan强连通分量
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long
using namespace std;
inline int read()
{
    int x = 0,f = 1;char ch = getchar();
    for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
    for(;isdigit(ch);ch = getchar())x = 10 * x + ch - '0';
    return x * f;
}
const int maxn = 100010;
int n,m;
int first[maxn],to[maxn << 1],nx[maxn << 1],cnt;
inline void add(int u,int v)
{
    to[++cnt] = v;
    nx[cnt] = first[u];
    first[u] = cnt;
}
int dfn[maxn],low[maxn],_tim;
int isc[maxn];
inline void Tarjan(int x,int fa)
{
    dfn[x] = low[x] = ++_tim;
    int ch = 0;
    for(int i=first[x];i;i=nx[i])
    {
        int v = to[i];
        if(!dfn[v])
        {
            Tarjan(v,x);
            low[x] = min(low[x],low[v]);
            if(low[v] >= dfn[x] && x != fa)isc[x] = 1;
            if(x == fa)++ch;
        }
        else low[x] = min(low[x],dfn[v]);
    }
    if(x == fa && ch >= 2)isc[x] = 1;
}
signed main()
{
    n = read(),m = read();
    for(int i=1;i<=m;i++)
    {
        int u = read(),v = read();
        add(u,v);add(v,u);
    }
    for(int i=1;i<=n;i++)
        if(!dfn[i])Tarjan(i,i);
    int ToT = 0;
    for(int i=1;i<=n;i++)if(isc[i])ToT++;
    cout<<ToT<<endl;
    for(int i=1;i<=n;i++)if(isc[i])cout<<i<<" ";
}
割点
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 500010,mod = 998244353,M = 499122177,G = 3;
int n,L,num,R[maxn],a[maxn],b[maxn],c[maxn],d[maxn];
int poly[maxn],inv[maxn];
inline int read()
{
    int x = 0,f = 1;char ch = getchar();
    for(;!isdigit(ch);ch = getchar())if(ch == '-')f = -f;
    for(;isdigit(ch);ch = getchar())x = 10 * x + ch - '0';
    return x * f;
}
inline int ksm(int x,int t)
{
    int res = 1;
    while(t)
    {
        if(t & 1) res = 1LL * res * x % mod;
        x = 1LL * x * x % mod;
        t >>= 1;
    }
    return res;
}
inline void NTT(int *a,int f,int n,int L)
{
    for(int i=0;i<n;i++) R[i] = (R[i>>1] >> 1) | ((i & 1) << (L - 1));
    for(int i=0;i<n;i++)if(i < R[i])swap(a[i],a[R[i]]);
    for(int i=1;i<n;i<<=1)
    {
        int wn = ksm(G,(mod - 1) / (i << 1));
        if(f == -1)wn = ksm(wn,mod - 2);
        for(int j=0;j<n;j+=(i<<1))
        {
            int w = 1;
            for(int k=0;k<i;k++,w=1LL * w * wn % mod)
            {
                int x = a[j + k], y = 1LL * w * a[j + k + i ] % mod;
                a[j + k] = ((x + y) % mod + mod) % mod;
                a[j + k + i] = ((x - y) % mod + mod) % mod;
            }
        }
    }
    if(f == -1)
    {
        int inv = ksm(n,mod - 2);
        for(int i=0;i<n;i++)a[i] = 1LL * a[i] * inv % mod;
    }
}
inline void inverse(int *a,int *b,int n,int L)
{
    if(n == 1){b[0] = ksm(a[0],mod - 2);return;}
    inverse(a,b,n>>1,L-1);
    memcpy(c,a,n*sizeof(int));memset(c+n,0,n*sizeof(int));
    NTT(c,1,n<<1,L+1);NTT(b,1,n<<1,L+1);
    for(int i=0;i<(n<<1);i++) b[i] = 1LL * b[i] * ((2 - 1LL * c[i] * b[i] % mod + mod) % mod) % mod;
    NTT(b,-1,n<<1,L+1);memset(b+n,0,n*sizeof(int));
}
inline void sqrt(int *a,int *b,int n,int L)
{
    if(n == 1){b[0] = 1;return;}
    sqrt(a,b,n>>1,L-1);memset(d,0,n*2*sizeof(int));inverse(b,d,n,L);
    memcpy(c,a,n*sizeof(int));memset(c+n,0,n*sizeof(int));
    NTT(c,1,n<<1,L+1);NTT(b,1,n<<1,L+1);NTT(d,1,n<<1,L+1);
    for(int i=0;i<n<<1;i++) b[i] = (1LL * c[i] * d[i] % mod + b[i]) %mod * M % mod;
    NTT(b,-1,n<<1,L+1);memset(b+n,0,n*sizeof(int));
}
signed main()
{
    n = read();
    for(int i=0;i<n;i++)poly[i] = read();
    int m;
    for(m=n,n=1;n<=m;n<<=1) L++;
    inverse(poly,inv,n,L);
    for(int i=0;i<m;i++)printf("%lld ",inv[i]);
}
多项式求逆

 

posted @ 2018-11-05 20:36  探险家Mr.H  阅读(208)  评论(1编辑  收藏  举报