bzoj4765 普通计算姬

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4765

【题解】

我们可以分块来做。

首先我们预处理每块的答案,并且处理出来f[x,i]表示x改动了,i块有多少个要改动。

修改用DFS序+BIT来块中暴力,整块用上面的方法处理即可。

询问块中BIT暴力,整块直接查答案即可。

# include <stdio.h>
# include <string.h>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 2e5 + 10, BLOCK = 310, N = 1e5 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, d[M], rt;
int head[M], nxt[M], to[M], tot=0;
inline void add(int u, int v) {
    ++tot; nxt[tot] = head[u]; head[u] = tot; to[tot] = v;
}
inline void adde(int u, int v) {
    add(u, v), add(v, u);
}

namespace BIT {
    const int M = 4e5 + 10;
    ull c[M];
    int n;
    # define lb(x) (x&(-x)) 
    inline void set(int _n) {
        n = _n; memset(c, 0, sizeof c);
    }
    inline void edt(int x, int d) {
        for (; x<=n; x+=lb(x)) c[x] += d;
    }
    inline ull ask(int x) {
        ull ret = 0;
        for (; x; x-=lb(x)) ret += c[x];
        return ret;
    }
    # undef lb 
}

ull bans[M]; 
int bl[M]; 
int f[N][BLOCK + 100], sz; 
int in[M], out[M], DFN=0, t[BLOCK + 100]; 
inline void dfs(int x, int fa) {
    t[bl[x]] ++;
    for (int i=1; i<=sz; ++i) f[x][i] = t[i]; 
    in[x] = ++DFN;
    for (int i=head[x]; i; i=nxt[i]) {
        if(to[i] == fa) continue;
        dfs(to[i], x);
    }
    out[x] = DFN;
    t[bl[x]] --; 
}

int main() {
    int q; scanf("%d%d", &n, &q);
    BIT::set(n); 
    for (int i=1; i<=n; ++i) scanf("%d", d+i); 
    for (int i=1, u, v; i<=n; ++i) {
        scanf("%d%d", &u, &v); 
        if(u == 0) rt = v;
        else adde(u, v);
        bl[i] = (i-1)/BLOCK+1;
    }
    sz = bl[n]; 
    dfs(rt, 0); 
    for (int i=1; i<=n; ++i) BIT::edt(in[i], d[i]); 
    for (int i=1; i<=n; ++i) bans[bl[i]] += BIT::ask(out[i]) - BIT::ask(in[i]-1); 
    
    int op, x, y;
    while(q--) {
         scanf("%d%d%d", &op, &x, &y);
        if(op == 1) {
             BIT::edt(in[x], y-d[x]);
            for (int i=1; i<=sz; ++i) bans[i] += (ull)f[x][i]*(y-d[x]);
            d[x] = y; 
        } else {
            ull ans = 0;
            if(bl[x] == bl[y]) {
                for (int i=x; i<=y; ++i) ans += BIT::ask(out[i]) - BIT::ask(in[i]-1); 
                printf("%llu\n", ans); 
                continue;
            }
            for (int i=bl[x]+1; i<=bl[y]-1; ++i) ans += bans[i];
            for (int i=x; i<=n && bl[i]==bl[x]; ++i) ans += BIT::ask(out[i]) - BIT::ask(in[i]-1);
            for (int i=y; i && bl[i]==bl[y]; --i) ans += BIT::ask(out[i]) - BIT::ask(in[i]-1); 
            printf("%llu\n", ans); 
        }
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/galaxies/p/bzoj4765.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BZOJ 2908 题目是一个数据下载任务。这个任务要求下载指定的数据文件,并统计文件中小于等于给定整数的数字个数。 为了完成这个任务,首先需要选择一个合适的网址来下载文件。我们可以使用一个网络爬虫库,如Python中的Requests库,来帮助我们完成文件下载的操作。 首先,我们需要使用Requests库中的get()方法来访问目标网址,并将目标文件下载到我们的本地计算机中。可以使用以下代码实现文件下载: ```python import requests url = '目标文件的网址' response = requests.get(url) with open('本地保存文件的路径', 'wb') as file: file.write(response.content) ``` 下载完成后,我们可以使用Python内置的open()函数打开已下载的文件,并按行读取文件内容。可以使用以下代码实现文件内容读取: ```python count = 0 with open('本地保存文件的路径', 'r') as file: for line in file: # 在这里实现对每一行数据的判断 # 如果小于等于给定整数,count 加 1 # 否则,不进行任何操作 ``` 在每一行的处理过程中,我们可以使用split()方法将一行数据分割成多个字符串,并使用int()函数将其转换为整数。然后,我们可以将该整数与给定整数进行比较,以判断是否小于等于给定整数。 最后,我们可以将统计结果打印出来,以满足题目的要求。 综上所述,以上是关于解决 BZOJ 2908 数据下载任务的简要步骤和代码实现。 希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值