寒假训练4线段树

单点修改

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 50010;

struct segmentTree {
  int l, r, sum;
} node[N << 2];

int a[N], kase;

void pushUp(int i) {
  node[i].sum = node[i<<1].sum + node[i<<1|1].sum;
}

void build(int l, int r, int i) {
  node[i] = {l, r};
  if (l == r) {
   node[i].sum = a[l];
   return;
  }
  int mid = l + r >> 1;
  build(l, mid, i << 1);
  build(mid + 1, r, i << 1 | 1);
  pushUp(i);
}

void modify(int p, int w, int i) {
  if (node[i].l == p && node[i].r == p) {
    node[i].sum += w;
    return;
  }
  int mid = node[i].l + node[i].r >> 1;
  if (p <= mid) modify(p, w, i << 1);
  else modify(p, w, i << 1 | 1);
  pushUp(i);
}

int query(int l, int r, int i) {
  if (l <= node[i].l && r >= node[i].r) {
    return node[i].sum;
  }
  int mid = node[i].l + node[i].r >> 1;
  int res = 0;
  if (l <= mid) res += query(l, r, i << 1);
  if (r > mid) res += query(l, r, i << 1 | 1);
  return res;
}

int main() {
  int T;
  scanf("%d",&T);
  while (T--) {
    printf("Case %d:\n",++kase);
    int n;
    scanf("%d",&n);
    for (int i = 1; i <= n; i++)
      scanf("%d",&a[i]);
    build(1, n, 1);
    char op[10];
    int x, y;
    while (scanf("%s",op)) {
      if (!strcmp(op, "End")) break;
      if (!strcmp(op, "Add")) {
        scanf("%d%d",&x,&y);
        modify(x, y, 1);
      } else if (!strcmp(op, "Sub")) {
        scanf("%d%d",&x,&y);
        modify(x, -y, 1);
      } else {
        scanf("%d%d",&x,&y);
        printf("%d\n",query(x, y, 1));
      }
    }
  }
  return 0;
}

区间修改

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
typedef long long LL;
using namespace std;
const int maxn=100000;
int num[maxn];
LL sum[maxn<<2],add[maxn<<2];
int N,Q;
void pushup(int rs)
{
    sum[rs]=sum[rs<<1]+sum[rs<<1|1];
}
void pushdown(int rs,int l)
{
    if(add[rs])
    {
        add[rs<<1]+=add[rs];
        add[rs<<1|1]+=add[rs];
        sum[rs<<1]+=add[rs]*(l-(l>>1));
        sum[rs<<1|1]+=add[rs]*(l>>1);
        add[rs]=0;
    }
}
void build(int rs,int l,int r)
{
     if(l==r)
     {
         sum[rs]=num[l];
         return ;
     }
     int mid=(l+r)>>1;
     build(rs<<1,l,mid);
     build(rs<<1|1,mid+1,r);
     pushup(rs);
}
void update(int c,int x,int y,int l,int r,int rs)
{
     if(l>=x&&r<=y)
     {
         add[rs]+=c;
         sum[rs]+=(LL)c*(r-l+1);
         return ;
     }
     pushdown(rs,r-l+1);
     int mid=(l+r)>>1;
     if(x<=mid)   update(c,x,y,l,mid,rs<<1);
     if(y>mid)    update(c,x,y,mid+1,r,rs<<1|1);
     pushup(rs);
}
LL query(int x,int y,int l,int r,int rs)
{
    if(l>=x&&r<=y)
        return  sum[rs];
    pushdown(rs,r-l+1);
    int mid=(l+r)>>1;
    LL ans=0;
    if(x<=mid)   ans+=query(x,y,l,mid,rs<<1);
    if(y>mid)    ans+=query(x,y,mid+1,r,rs<<1|1);
    return ans;
}
int main()
{
     int x,y,z;
     while(scanf("%d%d",&N,&Q)!=EOF)
     {
         memset(add,0,sizeof add);
         memset(sum,0,sizeof sum);
         for(int i=1;i<=N;i++){
         	scanf("%d",&num[i]);
		 }
         build(1,1,N);
         char str[2];
         while(Q--)
         {
             scanf("%s",str);
             if(str[0]=='C')
             {
                 scanf("%d%d%d",&x,&y,&z);
                 update(z,x,y,1,N,1);
             }
             else
             {
                 scanf("%d%d",&x,&y);
                 printf("%lld\n",query(x,y,1,N,1));
             }
         }
     }
     return 0;
}

二分


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
 
int h,w,n;
int ans;
 
struct node
{
    int l,r,n;
} a[1000000];
 
void init(int l,int r,int i,int w)
{
    a[i].l=l;
    a[i].r=r;
    a[i].n=w;
    if(l!=r)
    {
        int mid=(l+r)/2;
        init(l,mid,2*i,w);
        init(mid+1,r,2*i+1,w);
    }
}
 
void insert(int i,int x)
{
    if(a[i].l == a[i].r)
    {
        a[i].n-=x;
        ans = a[i].l;
        return ;
    }
    if(x<=a[2*i].n)
        insert(2*i,x);
    else
        insert(2*i+1,x);
    a[i].n = max(a[2*i].n,a[2*i+1].n);
}
 
int main()
{
    int i,k;
    while(scanf("%d%d%d",&h,&w,&n)!=EOF)
    {
       if(h>n)
            h = n;
        init(1,h,1,w);
        ans = -1;
        for(i = 1; i<=n; i++)
        {
            scanf("%d",&k);
            if(a[1].n>=k)
                insert(1,k);
            printf("%d\n",ans);
            ans = -1;
        }
    }
 
    return 0;
}

权值线段树

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>

using namespace std;

const int N = 8010;
int a[N], ans[N];

struct seg {
  int l, r, sum;
} node[N << 2];

void build(int l, int r, int i) {
  node[i] = {l, r, r - l + 1};
  if (l == r) return;
  int mid = l + r >> 1;
  build(l, mid, i << 1);
  build(mid + 1, r, i << 1 | 1);  
}

int query(int nums, int i) {
  node[i].sum--;
  if (node[i].l == node[i].r) return node[i].l;
  if (node[i<<1].sum >= nums) return query(nums, i << 1);
  else return query(nums - node[i<<1].sum, i << 1 | 1);
}

int main() {
  int n;
  scanf("%d",&n);
  for (int i = 2; i <= n; i++)
    scanf("%d",&a[i]);
  build(1, n, 1);
  for (int i = n; i >= 1; i--)
    ans[i] = query(a[i] + 1, 1);
  for (int i = 1; i <= n; i++)
    printf("%d\n",ans[i]);
  return 0;
}

树状数组

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxx=100005;
#define lowbit(x) x&(-x)
int c[maxx];
void update(int x,int v)
{
    for(int i=x;i<=maxx;i+=lowbit(i))
        c[i]+=v;
}
int gestsum(int x)
{
    int num=0;
    for(int i=x;i;i-=lowbit(i))
        num+=c[i];
    return num;
}
int main()
{
    int n,s,add;
    while(~scanf("%d",&n))
    {
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)
        {
           scanf("%d",&s);
           if(s==0)
           {
               scanf("%d",&add);
               update(add,1);
           }
           else if(s==1)
           {
               scanf("%d",&add);
               if(gestsum(add)-gestsum(add-1)==0)
                printf("No Elment!\n");
               else
                update(add,-1);
           }
           else
           {
               int a,k;
               scanf("%d%d",&a,&k);
               if(gestsum(maxx)-gestsum(a)<k)
                printf("Not Find!\n");
               else
               {
                   int l=a+1,r=maxx,mid;
                   while(l<r)
                   {
                       mid=(l+r)/2;
                       if(gestsum(mid)-gestsum(a)<k)
                       {
                           l=mid+1;
                       }
                       else
                       {
                           r=mid;
                       }
                   }
                   printf("%d\n",r);
               }
           }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值