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

这题也算比较容易的了。

如果哪个区间已经没有黑色的话,就不用update了,就是因为这个原因WA了2发,唉……

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define sc(a,b) scanf("%d%d",&a,&b)
#define pri(a) printf("%d\n",a)
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define MM 2000004
#define MN 1008
#define INF 2000000000
#define eps 1e-8
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
ll sum[MM];
int val[MM];
void pushUp(int i)
{
    sum[i]=sum[i<<1]+sum[i<<1|1];
}
void pushDown(int i)
{
    if(val[i])
    {
        sum[i]=sum[i<<1]=sum[i<<1|1]=0;
        val[i]=0;
    }
}
void build(int i,int l,int r)
{
    if(l==r)
    {
        sum[i]=1;
        return ;
    }
    int mid=(l+r)>>1;
    build(lson),build(rson);
    pushUp(i);
}
void update(int i,int l,int r,int L,int R)
{
    if(L<=l&&r<=R)
    {
        val[i]=1;
        sum[i]=0;
        return ;
    }
    int mid=(l+r)>>1;
    pushDown(i);
    if(sum[i]) //如果这个i的区间已经为0就不用再进行下去了
    {
        if(L>mid) update(rson,L,R);
        else if(R<=mid) update(lson,L,R);
        else
        {
            update(lson,L,mid);
            update(rson,mid+1,R);
        }
    }
    pushUp(i);
}
int main()
{
    int n,m,l,r;
    sc(n,m);
    build(1,1,n);
    while(m--)
    {
        sc(l,r);
        update(1,1,n,l,r);
        printf("%lld\n",sum[1]);
    }
    return 0;
}

Wikilo 1191线段树区间修改单点查询,布布扣,bubuko.com

时间: 2024-10-15 14:59:41

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

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

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

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; str