Can you answer these queries?(线段树)

本文介绍了一个关于连续区间内数值更新与查询的问题,通过使用线段树优化数据结构,实现对一连串数值进行平方根操作及求和查询,旨在解决战场上的武器攻击效果评估。

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66989#problem/H

这一题要注意剪枝,所有不必访问的点都不需要再访问了,注意最小的值不是0而是1;

H - Can you answer these queries?
Time Limit:2000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u

Description

A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help. 
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line. 

Notice that the square root operation should be rounded down to integer.
 

Input

The input contains several test cases, terminated by EOF. 
  For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000) 
  The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2  63
  The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000) 
  For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive. 
 

Output

For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
 

Sample Input

       
10 1 2 3 4 5 6 7 8 9 10 5 0 1 10 1 1 10 1 1 5 0 5 8 1 4 8
 

Sample Output

       
Case #1: 19 7 6
 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <string>
#define SI(T)int T;scanf("%d",&T)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
using namespace std;
const int SIZE=1e5+10;
const int maxn=1<<30;
__int64 sum[SIZE<<2];
void pushup(int rt){
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt){
    if(l==r){
        scanf("%I64d",&sum[rt]);
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int L,int R,int l,int r,int rt){
    if(sum[rt]==r-l+1)return;
    if(l==r){
        sum[rt]=(__int64)sqrt(sum[rt]);
        return;
    }
    int m=(l+r)>>1;
    if(L<=m)update(L,R,lson);
    if(R>m)update(L,R,rson);
    pushup(rt);
}
__int64 query(int L,int R,int l,int r,int rt){
    if(L<=l&&r<=R){
        return sum[rt];
    }
    if(sum[rt]==1)return 1;
    int m=(l+r)>>1;
    __int64 ans=0;
    if(L<=m)ans+=query(L,R,lson);
    if(R>m)ans+=query(L,R,rson);
    return ans;
}
int main()
{
    int n,m,t,x,y,cas=1;
    while(scanf("%d",&n)!=EOF){
         printf("Case #%d:\n",cas++);
        build(1,n,1);
        scanf("%d",&m);
        while(m--){
            scanf("%d%d%d",&t,&x,&y);
            if(x>y)swap(x,y);
            if(t==0){
                update(x,y,1,n,1);
            }
            else printf("%I64d\n",query(x,y,1,n,1));
        }
        printf("\n");
    }
    return 0;
}


### 批量抓取知乎答帖 answer-id 的方法及工具 批量抓取知乎答帖的 `answer-id` 是一个常见的需求,可以通过以下方法实现。以下是具体的技术细节和实现方式: #### 1. 使用 Python 爬虫技术 Python 提供了强大的爬虫库,如 `requests` 和 `BeautifulSoup`,可以用于抓取网页内容并解析出目标数据。对于知乎这样的网站,通常需要模拟登录或处理 API 请求以获取数据。 - **API 请求**:知乎提供了公开的 API 接口,可以通过访问问题页面的 API 来获取答案列表。每个答案的 `answer-id` 可以通过解析 JSON 数据提取。 - **分页处理**:知乎的答案通常是分页显示的,因此需要递归地请求下一页的数据,直到没有更多数据为止[^2]。 ```python import requests import json def fetch_answer_ids(question_id, headers): url = f"https://www.zhihu.com/api/v4/questions/{question_id}/answers" params = { "include": "data[*].is_normal,admin_closed_comment,reward_info", "limit": 5, # 每次请求返回的答案数量 "offset": 0, "platform": "desktop", "sort_by": "default" } answer_ids = [] while True: response = requests.get(url, headers=headers, params=params) if response.status_code != 200: print(f"Error: {response.status_code}") break data = json.loads(response.text) for answer in data["data"]: answer_ids.append(answer["id"]) # 提取 answer-id if not data["paging"]["is_end"]: params["offset"] += params["limit"] else: break return answer_ids ``` #### 2. 处理匿名用户和缺失字段 在某些情况下,知乎的回答可能由匿名用户发布,或者某些字段为空。在这种情况下,需要对数据进行预处理,确保不会因缺失值导致程序崩溃[^4]。 ```python def process_answer_data(answer): answer_item = {} answer_item["zhihu_id"] = answer.get("id", None) answer_item["author_id"] = answer["author"].get("id", None) if "author" in answer else None answer_item["content"] = answer.get("content", None) answer_item["praise_num"] = answer.get("voteup_count", 0) answer_item["comments_num"] = answer.get("comment_count", 0) return answer_item ``` #### 3. 数据存储与分析 抓取到的 `answer-id` 可以存储到文件或数据库中,以便后续分析。常用的存储工具包括 CSV 文件、SQLite 数据库或 MongoDB 等[^2]。 ```python import csv def save_to_csv(answer_ids, filename="answer_ids.csv"): with open(filename, mode="w", newline="", encoding="utf-8") as file: writer = csv.writer(file) writer.writerow(["Answer ID"]) for id in answer_ids: writer.writerow([id]) ``` #### 4. 注意事项 - **反爬机制**:知乎有严格的反爬机制,频繁请求可能会导致 IP 被封禁。建议使用代理池或设置合理的请求间隔[^3]。 - **法律合规**:抓取数据时需遵守相关法律法规和平台政策,避免侵犯隐私或违反服务条款[^1]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值