社区讨论
莫队套了个线段树T一片啊...
P3709大爷的字符串题参与者 5已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @mi6obcn2
- 此快照首次捕获于
- 2025/11/20 08:08 4 个月前
- 此快照最后确认于
- 2025/11/20 08:08 4 个月前
数据结构学傻了,以为log可以过 qwq
CPP#include<bits/stdc++.h>
#define maxn 200005
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define dd(i) (i-1)/blo
#define debug cout<<"*"<<endl;
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=getchar();
return x*f;
}
//--//--//--A1封装--//--//--//
struct ro{
int x,y,id,ans;
} d[maxn];
int a[maxn],t[maxn],ds[maxn],tmp,n,m,blo;
int cnt[maxn],ans;
map<int,int> mp;
//--//--//--变量--//--//--//
#define mid ((zuo+you)>>1)
#define c1 (o<<1)
#define c2 (o<<1|1)
int tree[maxn<<2];
void update(int o,int zuo,int you,int x,int k)
{
if(zuo==you){
tree[o]+=k;
// cout<<tree[o]<<" "<<o<<" "<<k<<" tree[o]********"<<endl;
return;
}
if(x<=mid) update(c1,zuo,mid,x,k);
else update(c2,mid+1,you,x,k);
tree[o]=max(tree[c1],tree[c2]);
}
//--//--//--线段树--//--//--//
void sc_lisan()
{
n=read(),m=read();blo=sqrt(n);
fo(i,1,n) ds[i]=dd(i);
fo(i,1,n) a[i]=read();
fo(i,1,n) t[i]=a[i];
sort(t+1,t+1+n);
tmp=unique(t+1,t+1+n)-t-1;
fo(i,1,tmp) mp[t[i]]=i;
fo(i,1,n) a[i]=mp[a[i]];
}
bool cmp(ro s1,ro s2){
if(ds[s1.x]==ds[s2.x]) return s1.y<s2.y;
return s1.x<s2.x;
}
void sc_query()
{
fo(i,1,m) d[i].x=read(),d[i].y=read(),d[i].id=i;
sort(d+1,d+1+m,cmp);
}
//--//--//--离散封装--//--//--//
bool vis[maxn];
void Mo_update(int u)
{
// cout<<u<<" u"<<endl;
// cout<<vis[u]<<" vis[u]"<<endl;;
if(vis[u]){
update(1,1,n,a[u],-1);
} else{
update(1,1,n,a[u],1);
}
vis[u]=!vis[u];
}
void Mo_captain()
{
int l=1,r=0;
fo(i,1,m){
while(l<d[i].x) Mo_update(l++);
while(l>d[i].x) Mo_update(--l);
while(r<d[i].y) Mo_update(++r);
while(r>d[i].y) Mo_update(r--);
// cout<<endl;
// cout<<tree[1]<<" tree1"<<endl;
d[i].ans=tree[1];
}
}
bool cmp2(ro s1,ro s2){
return s1.id<s2.id;
}
void pr()
{
sort(d+1,d+1+n,cmp2);
fo(i,1,n) printf("-%d\n",d[i].ans);
}
int main()
{
sc_lisan();
sc_query();
Mo_captain();
// debug;
pr();
return 0;
}
回复
共 5 条回复,欢迎继续交流。
正在加载回复...