线段树 区间修改,单点查询

https://www.luogu.com.cn/problem/P3368

上代码,对比一下可以发现,区间修改单点查询   和    单点修改区间查询   的add函数和search函数刚好是反的

#include<bits/stdc++.h>
#define N 500005
#define endl ‘\n‘
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
struct Node{
    int l,r,num;
}T[N*4];
int a[N]; ll res;
void build(int i,int l,int r){//µÝ¹é½¨Ê÷
    T[i].l=l;T[i].r=r;
    if(l==r){//Èç¹ûÕâ¸ö½ÚµãÊÇÒ¶×Ó½Úµã
        T[i].num=a[r];
        return ;
    }
    int mid=(l+r)>>1;
    build(i*2,l,mid);//·Ö±ð¹¹Ôì×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷
    build(i*2+1,mid+1,r);
    T[i].num= 0;//¸Õ²ÅÎÒÃÇ·¢ÏÖµÄÐÔÖÊreturn ;
}
void add(int i,int l,int r,int k){
    if(T[i].l>=l && T[i].r<=r){//Èç¹ûÕâ¸öÇø¼ä±»ÍêÈ«°üÀ¨ÔÚÄ¿±êÇø¼äÀïÃ棬½²Õâ¸öÇø¼ä±ê¼Çk
        T[i].num+=k;
        return ;
    }
    if(T[i*2].r>=l)
        add(i*2,l,r,k);
    if(T[i*2+1].l<=r)
        add(i*2+1,l,r,k);
}
void search(int i,int dis){
    res+=T[i].num;//һ·¼ÓÆðÀ´
    if(T[i].l==T[i].r)
        return ;
    if(dis<=T[i*2].r)
        search(i*2,dis);
    else
        search(i*2+1,dis);
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n,m;
    cin>>n>>m;
    _for(i,1,n+1) cin>>a[i];
    build(1,1,n);
    while(m--){
        int k; cin>>k;
        if(k==1){//
            int l,r,ad; cin>>l>>r>>ad;
            add( 1,l,r,ad );
        }
        else {
            int pos; cin>>pos; res=0;
            search(1,pos);
            cout<<res<<endl;
        }
    }
    return 0;
} 

原文地址:https://www.cnblogs.com/SunChuangYu/p/12343770.html

时间: 2024-10-18 16:37:14

线段树 区间修改,单点查询的相关文章

Wikilo 1191线段树区间修改单点查询

这题也算比较容易的了. 如果哪个区间已经没有黑色的话,就不用update了,就是因为这个原因WA了2发,唉-- #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #incl

HDU 5861 Road(线段树 区间修改 单点查询)

Road Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1132    Accepted Submission(s): 309 Problem Description There are n villages along a high way, and divided the high way into n-1 segments. E

线段树区间修改与查询

单点修改与查询 //单点修改,区间询问最小值 #include <iostream> #include <cstdio> #define maxn 101 #define INF 0x7fffffff using namespace std; int a[maxn],n,m; int mi[maxn]; void build(int k,int l,int r)//k是当前节点编号,l,r为当前节点代表区间 { if(l==r) { mi[k]=a[l]; return; } in

Light OJ 1080 - Binary Simulation - (线段树区间更新 单点查询)

Description Given a binary number, we are about to do some operations on the number. Two types of operations can be here. 'I i j'    which means invert the bit from i to j (inclusive) 'Q i'    answer whether the ith bit is 0 or 1 The MSB (most signif

ZOJ 1610 Count the Colors【题意+线段树区间更新&amp;&amp;单点查询】

任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent

POJ 3468 A Simple Problem with Integers(线段树区间修改及查询)

Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. In

uva 1401 Fast Matrix Operations 快速矩阵操作 (线段树 区间修改和查询)

题意:给一个r行c列的全0矩阵,支持以下三种操作: 1 x1 y1 x2 y2 v 子矩阵(x1 y1 x2 y2)的所有元素增加v 2 x1 y1 x2 y2 v 子矩阵(x1 y1 x2 y2)的所有元素设为v 3 x1 y1 x2 y2   查询子矩阵(x1 y1 x2 y2)的元素和.最小值.最大值. 子矩阵(x1 y1 x2 y2)是指满足 x1 <= x <= x2, y1 <= y <= y2的所有元素(x,y). 矩阵不超过20行,矩阵总元素可多达10^6个. 思路

ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)

Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can s

hdu1698 Just a Hook(线段树+区间修改+区间查询+模板)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 54923    Accepted Submission(s): 25566 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of