当前位置:网站首页>践踏---离散化+树状数组+差分
践踏---离散化+树状数组+差分
2022-07-29 02:17:00 【WAWA源】
题目链接
区间修改和区间查询分别通过差分转变为 单点修改和单点查询
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
const int N = 300010;
int n,k;
int tr[N];
vector<int>vec;
struct node
{
int op,l,r,x;
}a[N];
int lowbit(int x)
{
return x&-x;
}
void add(int x,int v)
{
for(int i=x;i<N;i+=lowbit(i))tr[i]+=v;
}
int query(int x)
{
int res=0;
for(int i=x;i>=1;i-=lowbit(i))res+=tr[i];
return res;
}
int get_id(int x)
{
return lower_bound(vec.begin(),vec.end(),x)-vec.begin()+1;
}
void modify(int l,int r,int v)
{
if(r-l+1>=k)
{
add(1,v);
add(k+1,-v);
}
else
{
l=(l%k)+1,r=(r%k)+1;
if(l<=r)
{
add(l,v);
add(r+1,-v);
}
else
{
add(l,v);
add(k+1,-v);
add(1,v);
add(r+1,-v);
}
}
}
signed main()
{
cin>>n>>k;
if(n==0)
{
cout<<"fafa\n";
return 0;
}
int op,l,r,x;
for(int i=1;i<=n;i++)
{
cin>>op;
if(op==1||op==2)
{
cin>>l>>r;
a[i]={
op,l,r};
vec.push_back(l);
vec.push_back(r);
}
else
{
cin>>x;
a[i].op=3,a[i].x=x;
vec.push_back(x);
}
}
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
if(k==0)
{
for(int i=1;i<=n;i++)
{
if(a[i].op==1||a[i].op==2)
{
int l=get_id(a[i].l);
int r=get_id(a[i].r);
if(a[i].op==1)add(l,1),add(r+1,-1);
else add(l,-1),add(r+1,1);
}
else
{
int x=get_id(a[i].x);
cout<<query(x)<<'\n';
}
}
}
else
{
for(int i=1;i<=n;i++)
{
if(a[i].op==1)modify(a[i].l,a[i].r,1);
else if(a[i].op==2)modify(a[i].l,a[i].r,-1);
else
{
int x=(a[i].x%k)+1;
cout<<query(x)<<'\n';
}
}
}
}
边栏推荐
- 这个博主,qt归类比较全,有空去学习总结,记录一下。
- 自动分账系统哪家好?
- QT screen adaptive automatic layout, drag the window to automatically grow larger and smaller (I)
- XSS range (II) xss.haozi
- How to migrate thinkphp5 projects to Alibaba cloud function computing to cope with traffic peaks?
- Asemi rectifier bridge s25vb100, s25vb100 parameters, s25vb100 application
- C language to achieve the three chess game
- Quickly master nodejs installation and getting started
- PHP幸运抽奖系统带后台源码
- 第八天笔记
猜你喜欢

Multimodal unsupervised image to image translation

物联网组件

6年测试经验,教大家测试~如何把控项目

用于校园流浪猫信息记录和分享的小程序源码/微信云开发中大猫谱小程序源码

无线振弦采集系统工作流程

XSS range (II) xss.haozi

CUDA details GPU architecture

Qt编写物联网管理平台48-特色功能设计

Workflow of wireless vibrating wire acquisition system

How does the Devops team defend against API attacks?
随机推荐
双for循环
自动分账系统哪家好?
ES6 detailed quick start!
Teach you how to install vscode by hand (with illustrated steps)
Redis master-slave mode, sentinel cluster, fragment cluster
MQTT例程
网络基础概论
代码实现 —— 多项式的最大公因式(线性代数)
When synchronized encounters this thing, there is a big hole, so be careful
QT qstringlist usage
Redis队列实现秒杀
Ffmpeg+sdl+qt is a simple video player
ECCV 2022 | AirDet:无需微调的小样本目标检测方法
线上3d数字展厅制作方案及优点
Flink生产环境经典问题汇总
云开发打工人必备上班摸鱼划水微信小程序源码
Cuda-npp image and video processing
Explanation of engineering economics terms
STM32C8T6编码器电机测速与arduino光电模块测速
MySQL basic operation and comprehensive instance project based on MySQL basic operation