http://codeforces.com/contest/915/problem/E
题意
一 个 长 为 n 的 序 列 , 一 开 始 全 为 2. 每 次 操 作 将 一 个 区 间 赋 值 为 1 或 2 , 并 询 问 整 体 有 多 少 个 2. 一个长为n的序列,一开始全为2.每次操作将一个区间赋值为1或2,并询问整体有多少个2. 一个长为n的序列,一开始全为2.每次操作将一个区间赋值为1或2,并询问整体有多少个2.
题解
可以用权值线段树瞎搞.
当然用某暴力数据结构珂朵莉树也可以过啦.
每次动态维护答案,用
s
e
t
set
set合并区间,暴力拆开.
然后就过了.
珂朵莉树的话全部都是基本操作,具体可以看洛谷上CF896C的题解.
#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rel register ll
#define rec register char
#define gc getchar
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
char buf[1<<23],*p1=buf,*p2=buf;
inline int read(){
int x=0,f=1;char c=gc();
for (;!isdigit(c);c=gc()) f^=c=='-';
for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
return f?x:-x;
}
template <typename mitsuha>
inline bool read(mitsuha &x){
x=0;int f=1;char c=gc();
for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';
if (!~c) return 0;
for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
return x=f?x:-x,1;
}
template <typename mitsuha>
inline int write(mitsuha x){
if (!x) return 0&pc(48);
if (x<0) pc('-'),x=-x;
int bit[20],i,p=0;
for (;x;x/=10) bit[++p]=x%10;
for (i=p;i;--i) pc(bit[i]+48);
return 0;
}
inline char fuhao(){
char c=gc();
for (;isspace(c);c=gc());
return c;
}
}using namespace chtholly;
using namespace std;
struct chtholly_tree{
#define it set<node>::iterator
int sum;
struct node{
int l,r,val;
node(int nl,int nr=-1,int v=0):l(nl),r(nr),val(v){}
bool operator <(const node &b) const{
return l<b.l;
}
};
set<node> s;
it split(int pos) {
it p=s.lower_bound(node(pos));
if (p!=s.end()&&p->l==pos) return p; --p;
int nl=p->l,nr=p->r,v=p->val;
s.erase(p);
s.insert(node(nl,pos-1,v));
return s.insert(node(pos,nr,v)).first;
}
void assign(int l,int r,int v) {
it p,itr=split(r+1),itl=split(l);
for (p=itl;p!=itr;++p) sum-=(p->r-p->l+1)*p->val;
s.erase(itl,itr);
s.insert(node(l,r,v)),sum+=(r-l+1)*v;
}
void init(int n) {
sum=n;
s.insert(node(1,n,1));
}
}my_;
int main(){
int n=read(),q=read();
my_.init(n);
for (;q--;) {
int l=read(),r=read(),k=read()-1;
my_.assign(l,r,k),write(my_.sum),pl;
}
}
谢谢大家.