随笔 - 531  文章 - 0  评论 - 3  阅读 - 10215 

在区间上种树

1. 区间 [l,r] 全部设为一种树木(每次都种新的品种)

 2. 问 [l,r] 内有多少种树木

且操作不会出现覆盖的情况

 

把区间当作括号 () [ ] ,

询问时答案为 r 左边 ( 的个数  -  l左边 ) 的个数 ,求前缀和,以及更新

 

复制代码
#include <iostream>
#include <cmath>
#include<algorithm>
using namespace std;
 const int M=1e5;
 
 int n,c1[M],c2[M];
 int lowbit(int x){
     return x&-x;
 }
 int q1(int x){
     int s=0;
     for(;x;x-=lowbit(x)) s+=c1[x];
     return s;
 }
 void up1(int x,int v){
     for(;x<=n;x+=lowbit(x)) c1[x]+=v;
 }
 int q2(int x){
     int s=0;
     for(;x;x-=lowbit(x)) s+=c2[x];
     return s;
 }
 void up2(int x,int v){
     for(;x<=n;x+=lowbit(x)) c2[x]+=v;
 }
 int main(){
     int tes;
     scanf("%d%d",&n,&tes);
     int x,y,k;
     while(tes--){
         scanf("%d%d%d",&k,&x,&y);
         if(k==1){
             up1(x,1); up2(y,1);
         }
         else printf("%d\n",q1(y)-q2(x-1));
     }
 } 
复制代码

 

posted on   towboat  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示