阶段总结

最近因发现基础不好而狂刷板子,导致bzoj的计划放了一段时间。现在板子搞得差不多了,还剩一个90分的负环和70分的树刨搞不出来,就很慌,希望大家帮我调一下,都是T了。

接下来重心放到bzoj去,按照hzwer的顺序吧。

下面放代码:

90分负环:

// luogu-judger-enable-o2
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <iostream> 
using namespace std ; 

const int maxn = 200011,maxm = 200011,inf = 1e9 ; 
struct node{
    int to,val,pre ; 
}e[2*maxm];
int T,n,m,x,y,val,cnt ; 
int head[maxn],dist[maxn] ; 
bool flag ; 
bool vis[maxn] ; 

inline void addedge(int x,int y,int v) 
{
    e[++cnt] = (node){ y,v,head[x] } ; 
    head[ x ] = cnt ; 
}

inline int read() 
{
    char ch = getchar() ; 
    int x = 0 , f = 1 ; 
    while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar() ;  } 
    while(ch>='0'&&ch<='9') { x = x*10+ch-48 ; ch = getchar() ; } 
    return x*f ; 
}

inline void SPFA(int u) 
{
    int v ; 
    vis[ u ] = 1 ; 
    for(int i=head[u];i;i = e[ i ].pre ) 
    {
        v = e[ i ].to ; 
        if( dist[ u ] + e[ i ].val < dist[ v ] ) 
        {
            dist[ v ] = dist[ u ] + e[ i ].val ; 
            if(vis[ v ]||flag) 
            {
                flag = 1 ;  
                break ; 
            }
            SPFA( v ) ; 
         }
    }    
    vis[ u ] = 0 ; 
}

int main()  
{
    T = read() ;  
    while(T--) 
    {
        flag = 0 ; 
        cnt = 0 ; 
        n = read() ; m = read() ;  
        for(int i=1;i<=n;i++) dist[ i ] = 0,vis[ i ] = 0,head[ i ] = 0 ;    //0
        for(int i=1;i<=m;i++) 
        {
            scanf("%d%d%d",&x,&y,&val) ; 
            addedge(x,y,val) ; 
            if(val>=0) addedge(y,x,val) ;  
        }
        for(int i=1;i<=n;i++) 
        {
            SPFA( i ) ; 
            if(flag) break ; 
        } 
        if(flag) 
            printf("YE5\n") ;  
        else 
            printf("N0\n") ; 
            
    }
    return 0 ; 
}

70分树刨:

// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = 1 << 30;
const int N = 200005;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
struct edge
{
    int nxt,r;
} e[4 * N];
int n,m,r,cnt;
int a[N],lst[N],p;
int f[N],d[N],siz[N],son[N],rk[N];
int top[N],id[N],tree[4 * N];
int lazy[4 * N],len = 0;
//id新编号dfs序
void add(int x,int y)
{
    e[++len].nxt = lst[x];
    e[len].r = y;
    lst[x] = len;
}

void dfs1(int u,int fa,int depth)
{
    f[u] = fa;
    d[u] = depth;
    siz[u] = 1;
    for(int k = lst[u];k;k = e[k].nxt)
    {
        int y = e[k].r;
        if(y == fa)
            continue;
        dfs1(y,u,depth + 1);
        siz[u] += siz[y];
        if(siz[y] > siz[son[u]] || !son[u])
        {
            son[u] = y;
        }
    }
}

void dfs2(int u,int t)
{
    top[u] = t;
    id[u] = ++cnt;
    rk[cnt] = u;
    if(!son[u])
        return;
    dfs2(son[u],t);
    for(int k = lst[u];k;k = e[k].nxt)
    {
        int y = e[k].r;
        if(y != son[u] && y != f[u])
            dfs2(y,y);
    }
}

void push_down(int o,int l,int r)
{
    if(lazy[o])
    {
        lazy[o << 1] += lazy[o];
        lazy[o << 1] %= p;
        lazy[o << 1 | 1] += lazy[o];
        lazy[o << 1 | 1] %= p;
        int len = (r - l + 1);
        tree[o << 1] += lazy[o] * (len - (len >> 1));
        tree[o << 1 | 1] += lazy[o] * (len >> 1);
        tree[o << 1] %= p;
        tree[o << 1 | 1] %= p;
        lazy[o] = 0;
    }
}

void build(int o,int l,int r)
{
    if(l == r)
    {
        tree[o] = a[rk[l]];
        tree[o] %= p;
        return;
    }
    int mid = (l + r) >> 1;
    build(o << 1,l,mid);
    build(o << 1 | 1,mid + 1,r);
    tree[o] = tree[o << 1] + tree[o << 1 | 1];
    tree[o] %= p;
}

void up_num(int o,int l,int r,int x,int y,int w)
{
    if(l == x && r == y)
    {
        tree[o] += w * (l - r + 1);
        tree[o] %= p;
        lazy[o] += w;
        lazy[o] %= p;
        return;
    }
    push_down(o,l,r);
    int mid = (l + r) >> 1;
    if(mid < x)
        up_num(o << 1 | 1,mid + 1,r,x,y,w);
    else if(mid >= y)
        up_num(o << 1,l,mid,x,y,w);
    else
    {
        up_num(o << 1,l,mid,x,mid,w);
        up_num(o << 1 | 1,mid + 1,r,mid + 1,y,w);
    }
    tree[o] = tree[o << 1] + tree[o << 1 | 1];
    tree[o] %= p;
}

int query(int o,int l,int r,int x,int y)
{
    if(l == r && x == y)
    {
        return tree[o];
    }
    push_down(o,l,r);
    int mid = (l + r) >> 1;
    if(mid >= y)
        return query(o << 1,l,mid,x,y);
    else if(mid < x)
        return query(o << 1 | 1,mid + 1,r,x,y);
    else
    {
        return (query(o << 1,l,mid,x,mid) + query(o << 1 | 1,mid + 1,r,mid + 1,y)) % p;
    }
}

int pathquery(int x,int y)
{
    int ans = 0;
    while(top[x] != top[y])
    {
        if(d[top[x]] < d[top[y]])
            swap(x,y);
        ans += query(1,1,n,id[top[x]],id[x]);
        ans %= p;
        x = f[top[x]];
    }
    if(d[x] > d[y])
    swap(x,y);
    ans += query(1,1,n,id[x],id[y]);
    ans %= p;
    return ans;
}

void pathupdate(int x,int y,int c)
{
    // int fx = top[x],fy = top[y];
    while(top[x] != top[y])
    {
        if(d[top[x]] < d[top[y]])
        swap(x,y);
        up_num(1,1,n,id[top[x]],id[x],c);
        x = f[top[x]];
        // update(id[x])
    }
    if(d[x] > d[y])
    swap(x,y);
    up_num(1,1,n,id[x],id[y],c);
}
int main()
{
    read(n);read(m);read(r);read(p);
    duke(i,1,n)
    read(a[i]);
    duke(i,1,n - 1)
    {
        int x,y;
        read(x);read(y);
        add(x,y);
        add(y,x);
    }
    cnt = 0;
    dfs1(r,0,1);
    dfs2(r,r);
    cnt = 0;
    build(1,1,n);
    duke(i,1,m)
    {
        int op,x,y,z;
        read(op);
        if(op == 1)
        {
            read(x);read(y);read(z);
            pathupdate(x,y,z);
        }
        else if(op == 2)
        {
            read(x);read(y);
            printf("%d\n",pathquery(x,y));
        }
        else if(op == 3)
        {
            read(x);read(z);
//            cout<<x<<endl;
            up_num(1,1,n,id[x],id[x] + siz[x] - 1,z);
        }
        else
        {
            read(x);
            printf("%d\n",query(1,1,n,id[x],id[x] + siz[x] - 1));
        }
    }
    return 0;
}
/*
5 5 2 24
7 3 7 8 0
1 2
1 5
3 1
4 1
3 4 2
3 2 2
4 5
1 5 1 3
2 1 3
*/

有人帮我debug吗?

posted @ 2018-09-23 21:11  DukeLv  阅读(159)  评论(4编辑  收藏  举报