当前位置:网站首页>Trample --- discretization + tree array + difference
Trample --- discretization + tree array + difference
2022-07-29 02:48:00 【Wawa source】
Topic link
Interval modification and interval query are transformed into Single point modification and query
#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';
}
}
}
}
边栏推荐
- Wechat applet - Advanced chapter Lin UI component library source code analysis button component (II)
- 架构师进阶,微服务设计与治理的 16 条常用原则
- Library management system
- Others' happiness
- 10.书写规则-文件搜寻
- owt-server源码剖析(三)--video模块分析之Mixer In
- ECCV 2022 | airdet: a small sample target detection method without fine tuning
- Three expiration strategies
- 一款好看的iapp捐赠榜单源码
- Deliver temperature with science and technology, vivo appears at the digital China Construction Summit
猜你喜欢

QT compilation of IOT management platform 48 characteristic function design

PHP lucky draw system with background source code

Teach you how to install vscode by hand (with illustrated steps)

h. 264 code stream explanation

Deliver temperature with science and technology, vivo appears at the digital China Construction Summit

家庭亲戚关系计算器微信小程序源码

(作业)C语言:atoi和strncpy、strncat、strncmp的模拟实现

Multiple inheritance and derived class member identification

ASEMI整流桥S25VB100,S25VB100参数,S25VB100应用

C语言:小乐乐与欧几里得
随机推荐
[untitled]
DHCP协议详细解析
Driverless obstacle avoidance technology
网络基础概论
12.书写规则-静态模式
h. 264 code stream explanation
Polygon zkevm - Introduction to HERMEZ 2.0
DHCP protocol detailed analysis
MySQL basic operation and comprehensive instance project based on MySQL basic operation
C language: Little Lele and Euclid
C语言:小乐乐与进制转换
What are the TCP retransmission mechanisms?
Family relationship calculator wechat applet source code
MPEG音频编码三十年
R语言ERROR: compilation failed for package ‘****‘
Stm32c8t6 encoder motor speed measurement and Arduino photoelectric module speed measurement
以科技传递温度,vivo亮相数字中国建设峰会
C语言:判断字母
C language to achieve the three chess game
QT屏幕自适应自动布局,拖动窗口自动变大变小(一)