标签:name pair freopen string algorithm -- include BMI i+1
https://www.luogu.com.cn/problem/P1903#submit
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <map>
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;//R L time count
typedef long long ll;
const int maxn=2e5+10;
int a[maxn],num[maxn*10],kuai,time_,q,l,r,time_now,ans_now;
struct nod{
int L,R,ti,coun;
};
vector<nod> D;
vector<P> ans;
PP t[maxn];
bool cmp(nod n1,nod n2)
{
if(n1.L/kuai==n2.L/kuai)
{
if(n1.R/kuai==n2.R/kuai)
{
return n1.ti<n2.ti;
}
else
return n1.R/kuai<n2.R/kuai;
}
else
return n1.L/kuai<n2.L/kuai;
}
inline void l_l()
{
l--;
if(num[a[l]]==0)
ans_now++;
num[a[l]]++;
}
inline void l_r()
{
num[a[l]]--;
if(num[a[l]]==0)
ans_now--;
l++;
}
inline void r_l()
{
num[a[r]]--;
if(num[a[r]]==0)
ans_now--;
r--;
}
inline void r_r()
{
r++;
if(num[a[r]]==0)
ans_now++;
num[a[r]]++;
}
inline void t_l()
{
int loc=t[time_now].first,ago=t[time_now].second.second;
if(l<=loc&&loc<=r)
{
num[a[loc]]--;
if(num[a[loc]]==0)
ans_now--;
a[loc]=ago;
if(num[ago]==0)
ans_now++;
num[ago]++;
}
a[loc]=ago;
time_now--;
}
inline void t_r()
{
time_now++;
int loc=t[time_now].first,co=t[time_now].second.first;
if(l<=loc&&loc<=r)
{
num[a[loc]]--;
if(num[a[loc]]==0)
ans_now--;
a[loc]=co;
if(num[co]==0)
ans_now++;
num[co]++;
}
a[loc]=co;
}
int main()
{
// freopen("/Users/mashuo/Downloads/P1903_1.in", "r", stdin);
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>a[i+1];
int flag=0;
for(int i=0;i<m;i++)
{
char c;
int l,r;
cin>>c>>l>>r;
if(c==‘R‘)
{
time_++;
t[time_]=make_pair(l, make_pair(r, a[l]));
a[l]=r;
}else
{
nod t1;
t1.L=l;
t1.R=r;
t1.ti=time_;
t1.coun=flag++;
D.push_back(t1);
}
}
for(int i=time_;i>0;i--)
a[t[i].first]=t[i].second.second;
kuai=exp((log(n)+log(time_))/3);
sort(D.begin(), D.end(), cmp);
l=1;r=1;time_now=0;ans_now=1;num[a[1]]=1;
for(int i=0;i<D.size();i++)
{
while(l<D[i].L)l_r();
while(l>D[i].L)l_l();
while(r<D[i].R)r_r();
while(r>D[i].R)r_l();
while(time_now>D[i].ti) t_l();
while(time_now<D[i].ti) t_r();
ans.push_back(make_pair(D[i].coun, ans_now));
}
sort(ans.begin(), ans.end());
for(int i=0;i<ans.size();i++){
cout<<ans[i].second<<endl;
}
}
标签:name pair freopen string algorithm -- include BMI i+1
原文地址:https://www.cnblogs.com/King-of-Dark/p/12359567.html