1500: [NOI2005]维修数列
2016-03-13 13:45
337 查看
查错ing.....扔个随机数
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 5E5 + 50;
const int p = 1001;
int n,m;
class data {
private:
struct Node {
Node *ch[2];
int siz,rev,modi,l,r,MAX,num,sum,po;
}*root,pool[maxn];
int bo[maxn],now;
void maintain(Node *&x) {
int s1,s2,sl,sr,suml,sumr;
x->MAX = -2E9; x->sum = x->num;
x->l = x->r = 0;
s1 = s2 = sl = sr = suml = sumr = 0;
if (x->ch[0] != NULL) {
s1 = x->ch[0]->siz;
sl = x->ch[0]->r;
suml = x->ch[0]->sum;
x->MAX = max(x->ch[0]->MAX,x->MAX);
x->sum += x->ch[0]->sum;
x->l = max(x->l,x->ch[0]->l);
}
if (x->ch[1] != NULL) {
s2 = x->ch[1]->siz;
sr = x->ch[1]->l;
sumr = x->ch[1]->sum;
x->MAX = max(x->ch[1]->MAX,x->MAX);
x->sum += x->ch[1]->sum;
x->r = max(x->r,x->ch[1]->r);
}
x->MAX = max(x->MAX,sl + sr + x->num);
x->siz = s1 + s2 + 1;
x->l = max(x->l,suml + x->num + sr);
x->r = max(x->r,sumr + x->num + sl);
}
void pushdown(Node *&x) {
if (x->rev) {
swap(x->ch[0],x->ch[1]);
for (int i = 0; i < 2; i++) if (x->ch[i] != NULL)
x->ch[i]->rev ^= 1,swap(x->ch[i]->l,x->ch[i]->r);
x->rev ^= 1;
}
if (x->modi < p) {
for (int i = 0; i < 2; i++)
if (x->ch[i] != NULL) {
int t = x->modi;
x->ch[i]->num = x->ch[i]->modi = t;
x->ch[i]->sum = t*x->ch[i]->siz;
if (t > 0) x->ch[i]->l = x->ch[i]->r = x->ch[i]->MAX = x->ch[i]->sum;
else x->ch[i]->l = x->ch[i]->r = 0,x->ch[i]->MAX = t;
}
x->modi = p;
}
maintain(x);
}
void rotate(Node *&x,int d) {
Node *y = x->ch[d];
x->ch[d] = y->ch[d^1];
y->ch[d^1] = x;
maintain(x);
x = y;
maintain(x);
}
void splay(Node *&x,int rank) {
pushdown(x);
int s1 = x->ch[0] == NULL?0:x->ch[0]->siz;
if (s1 + 1 == rank) return;
int d;
if (s1 + 1 < rank) {
rank = rank - s1 - 1;
d = 1;
}
else d = 0;
pushdown(x->ch[d]);
int s2 = x->ch[d]->ch[0] == NULL?0:x->ch[d]->ch[0]->siz;
if (s2 + 1 != rank) {
int d1;
if (s2 + 1 < rank) d1 = 1,rank = rank - s2 - 1;
else d1 = 0;
pushdown(x->ch[d]->ch[d1]);
splay(x->ch[d]->ch[d1],rank);
if (d == d1) rotate(x,d);
else rotate(x->ch[d],d1);
}
rotate(x,d);
}
Node *merge(Node *left,Node *right) {
splay(left,left->siz);
left->ch[1] = right;
maintain(left);
return left;
}
void split(Node *x,Node *&left,Node *&right,int rank) {
splay(x,rank);
right = x->ch[1]; x->ch[1] = NULL;
left = x; maintain(left);
}
void DEL(Node *&x) {
if (x->ch[0] != NULL) DEL(x->ch[0]);
if (x->ch[1] != NULL) DEL(x->ch[1]);
x->ch[0] = x->ch[1] = NULL;
bo[++now] = x->po;
}
public:
data() {
root = &pool[0];
root->num = 0;
root->rev = 0; root->modi = p;
maintain(root);
for (int i = 1; i <= 500000; i++) bo[i] = i;
now = 500000;
}
void Insert (int rank,int num) {
Node *left,*right;
split(root,left,right,rank);
left->ch[1] = &pool[bo[now]];
Node *&x = left->ch[1]; x->po = bo[now--];
x->num = num; x->rev = 0; x->modi = p;
maintain(x);
maintain(left);
root = merge(left,right);
splay(root,rank);
}
void Remove(int pos,int tot) {
splay(root,pos);
if (pos + tot == n) {
DEL(root->ch[1]);
root->ch[1] = NULL;
maintain(root);
}
else {
splay(root->ch[1],tot+1);
DEL(root->ch[1]->ch[0]);
root->ch[1]->ch[0] = NULL;
maintain(root->ch[1]);
maintain(root);
}
}
void Modify(int pos,int tot,int num) {
splay(root,pos);
Node *x;
if (pos + tot == n) x = root->ch[1];
else {
splay(root->ch[1],tot+2);
x = root->ch[1]->ch[0];
}
x->modi = x->num = num;
int t = x->modi;
x->sum = t*x->siz;
if (t > 0) x->l = x->r = x->MAX = x->sum;
else x->l = x->r = 0,x->MAX = t;
if (pos + tot == n) root->ch[1] = x;
else root->ch[1]->ch[0] = x;
if (pos + tot != n) maintain(root->ch[1]);
maintain(root);
}
void Rever(int pos,int tot) {
splay(root,pos);
Node *x;
if (pos + tot == n) x = root->ch[1];
else {
splay(root->ch[1],tot+2);
x = root->ch[1]->ch[0];
}
x->rev ^= 1;
swap(x->l,x->r);
if (pos + tot == n) root->ch[1] = x;
else root->ch[1]->ch[0] = x;
if (pos + tot != n) maintain(root->ch[1]);
maintain(root);
}
int SUM(int pos,int tot) {
splay(root,pos);
if (pos + tot == n) return root->ch[1]->sum;
else {
splay(root->ch[1],tot+2);
return root->ch[1]->ch[0]->sum;
}
}
int MAXSUM() {
splay(root,1);
return root->ch[1]->MAX;
}
};
char co[10];
int getcom()
{
scanf("%s",co);
if (co[0] == 'I') return 1;
if (co[0] == 'D') return 2;
if (co[0] == 'R') return 4;
if (co[0] == 'G') return 5;
int len = strlen(co);
if (len > 7) return 3;
else return 6;
}
int typ,tot,pos;
int main()
{
#ifdef YZY
freopen("yzy.txt","r",stdin);
#endif
scanf("%d%d",&n,&m);
static data tree;
int N = n; n = 1;
for (int i = 1; i <= N; i++) {
int x; scanf("%d",&x);
++n;
tree.Insert(i,x);
}
while (m--) {
typ = getcom();
if (typ == 1) {
int c;
scanf("%d%d",&pos,&tot);
for (int i = pos + 1; i <= pos + tot; i++) {
scanf("%d",&c);
++n;
tree.Insert(i,c);
}
}
else if (typ == 2) {
scanf("%d%d",&pos,&tot);
tree.Remove(pos,tot);
n -= tot;
}
else if (typ == 3) {
int c;
scanf("%d%d%d",&pos,&tot,&c);
tree.Modify(pos,tot,c);
}
else if (typ == 4) {
scanf("%d%d",&pos,&tot);
tree.Rever(pos,tot);
}
else if (typ == 5) {
scanf("%d%d",&pos,&tot);
if (!n) printf("0\n");
else printf("%d\n",tree.SUM(pos,tot));
}
else {
if (!n) printf("0\n");
else printf("%d\n",tree.MAXSUM());
}
}
return 0;
}
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #include<vector> #include<queue> using namespace std; const int maxn = 4E6 + 2E4 + 10; const int p = 1001; class data { private: struct Node { Node *ch[2]; int siz,num,rev,modi,l,r,MAX,num; }pool[maxn],*root,*tot; void maintain(Node *&x) { int s1,s2,sl,sr,suml,sumr; x->MAX = -2E9; x->sum = x->num; x->l = max(0,x->num); x->r = max(0,x->num); s1 = s2 = sl = sr = suml = sumr = 0; if (x->ch[0] != NULL) { s1 = x->ch[0]->siz; sl = x->ch[0]->r; suml = x->ch[0]->sum; x->MAX = max(x->ch[0]->MAX,x->MAX); x->sum += x->ch[0]->sum; x->l = max(x->l,x->ch[0]->l); } if (x->ch[1] != NULL) { s2 = x->ch[0]->siz; sr = x->ch[1]->l; sumr = x->ch[1]->sum; x->MAX = max(x->ch[1]->MAX,x->MAX); x->sum += x->ch[1]->sum; x->r = max(x->r,x->ch[1]->r); } x->MAX = max(x->MAX,sl + sr + x->num); x->siz = s1 + s2 + 1; x->l = max(x->l,suml + x->num + sr); x->r = max(x->r,sumr + x->num + sl); } void pushdown(Node *&x) { if (x->rev) { swap(x->ch[0],x->ch[1]); for (int i = 0; i < 2; i++) if (x->ch[i] != NULL) x->ch[i]->rev ^= 1,swap(x->ch[i]->l,x->ch[i]->r); x->rev ^= 1; } if (x->modi < p) { for (int i = 0; i < 2; i++) if (x->ch[i] != NULL) { int t = x->modi; x->ch[i]->num = x->ch[i]->modi = t; x->ch[i]->sum = t*x->ch[i]->siz; if (t > 0) x->ch[i]->l = x->ch[i]->r = x->MAX = x->ch[i]->sum; else x->ch[i]->l = x->ch[i]->r = 0,x->ch[i]->MAX = t; } } maintain(x); } void rotate(Node *&x,int d) { Node *y = x->ch[d]; x->ch[d] = y->ch[d^1]; y->ch[d^1] = x; maintain(x); x = y; maintain(x); } void splay(Node *&x,int rank) { pushdown(x); int s1 = x->ch[0] == NULL?0:x->ch[0]->siz; if (s1 + 1 == rank) return; int d; if (s1 + 1 < rank) { rank = rank - s1 - 1; d = 1; } else d = 0; pushdown(x->ch[d]); int s2 = x->ch[d]->ch[0] == NULL?0:x->ch[d]->ch[0]->siz; if (s2 + 1 != rank) { int d1; if (s2 + 1 < rank) d1 = 1,rank = rank - s2 - 1; else d1 = 0; pushdown(x->ch[d]->ch[d1]); splay(x->ch[d]->ch[d1],rank) if (d == d1) rotate(x,d); else rotate(x->ch[d],d1); } rotate(x,d); } Node *merge(Node *left,Node *right) { splay(left,left->siz); left->ch[1] = right; maintain(left); return left; } void split(Node *x,Node *&left,Node *&right,int rank) { splay(x,rank); right = x->ch[1]; x->ch[1] = NULL; left = x; maintain(left); } public: data() { tot = pool; root = ++tot; root->num = -2E9; root->rev = 0; root->modi = p; maintain(root); } void Insert (int rank,int num) { Node *left,*right; split(root,left,right,rank); left->ch[1] = ++tot; Node *&x = left->ch[1]; x->num = num; x->rev = 0; x->modi = p; maintain(x); maintain(left); root = merge(left,right); } void Remove(int pos,int tot) { splay(root,pos); splay(root->ch[1],pos+tot+2); root->ch[1]->ch[0] = NULL; maintain(root->ch[1]); maintain(root); } void Modify(int pos,int tot,int num) { splay(root,pos); splay(root->ch[1],pos+tot+2); Node *&x = root->ch[1]->ch[0]; x->modi = x->num = num; int t = x->modi; x->sum = t*x->siz; if (t > 0) x->l = x->r = x->MAX = x->sum; else x->l = x->r = 0,x->MAX = t; } void Rever(int pos,int tot) { splay(root,pos); splay(root->ch[1],pos+tot+2); Node *&x = root->ch[1]->ch[0]; x->rev ^= 1; swap(x->l,x->r); } int SUM(int pos,int tot) { splay(root,pos); splay(root->ch[1],pos+tot+2); return root->ch[1]->ch[0]->sum; } int MAXSUM() { splay(root,1); return root->ch[1]->MAX; } }; int main() { #ifdef YZY freopen("yzy.txt","r",stdin); #endif return 0; }
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #include<vector> #include<ctime> #include<queue> using namespace std; int main() { #ifdef YZY freopen("yzy.txt","r",stdin); #endif srand(time(0)); int n,m; n = m = 10; printf("%d %d\n",n,m); for (int i = 1; i <= n; i++) printf("%d ",rand()%2001-1000); cout << endl; while (m--) { int typ = rand() % 6; if (typ == 0) { int pos = rand()%(n+1); int tot = rand()%5 + 1; printf("INSERT %d %d ",pos,tot); while (tot--) printf("%d ",rand()%2001-1000); cout << endl; n += tot; } else if (typ == 1) { int pos = rand()%n+1; int tot; if (pos == n) tot = 1; else tot = rand()%(n-pos)+1; printf("DELETE %d %d\n",pos,tot); n -= tot; } else if (typ == 2) { int pos = rand()%n+1; int tot; if (pos == n) tot = 1; else tot = rand()%(n-pos)+1; int c = rand()%2001-1000; printf("MAKE-SAME %d %d %d\n",pos,tot,c); } else if (typ == 3) { int pos = rand()%n+1; int tot; if (pos == n) tot = 1; else tot = rand()%(n-pos)+1; printf("REVERSE %d %d\n",pos,tot); } else if (typ == 4) { int pos = rand()%n+1; int tot; if (pos == n) tot = 1; else tot = rand()%(n-pos)+1; printf("GET-SUM %d %d\n",pos,tot); } else printf("MAX-SUM\n"); } return 0; }
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 5E5 + 50;
const int p = 1001;
int n,m;
class data {
private:
struct Node {
Node *ch[2];
int siz,rev,modi,l,r,MAX,num,sum,po;
}*root,pool[maxn];
int bo[maxn],now;
void maintain(Node *&x) {
int s1,s2,sl,sr,suml,sumr;
x->MAX = -2E9; x->sum = x->num;
x->l = x->r = 0;
s1 = s2 = sl = sr = suml = sumr = 0;
if (x->ch[0] != NULL) {
s1 = x->ch[0]->siz;
sl = x->ch[0]->r;
suml = x->ch[0]->sum;
x->MAX = max(x->ch[0]->MAX,x->MAX);
x->sum += x->ch[0]->sum;
x->l = max(x->l,x->ch[0]->l);
}
if (x->ch[1] != NULL) {
s2 = x->ch[1]->siz;
sr = x->ch[1]->l;
sumr = x->ch[1]->sum;
x->MAX = max(x->ch[1]->MAX,x->MAX);
x->sum += x->ch[1]->sum;
x->r = max(x->r,x->ch[1]->r);
}
x->MAX = max(x->MAX,sl + sr + x->num);
x->siz = s1 + s2 + 1;
x->l = max(x->l,suml + x->num + sr);
x->r = max(x->r,sumr + x->num + sl);
}
void pushdown(Node *&x) {
if (x->rev) {
swap(x->ch[0],x->ch[1]);
for (int i = 0; i < 2; i++) if (x->ch[i] != NULL)
x->ch[i]->rev ^= 1,swap(x->ch[i]->l,x->ch[i]->r);
x->rev ^= 1;
}
if (x->modi < p) {
for (int i = 0; i < 2; i++)
if (x->ch[i] != NULL) {
int t = x->modi;
x->ch[i]->num = x->ch[i]->modi = t;
x->ch[i]->sum = t*x->ch[i]->siz;
if (t > 0) x->ch[i]->l = x->ch[i]->r = x->ch[i]->MAX = x->ch[i]->sum;
else x->ch[i]->l = x->ch[i]->r = 0,x->ch[i]->MAX = t;
}
x->modi = p;
}
maintain(x);
}
void rotate(Node *&x,int d) {
Node *y = x->ch[d];
x->ch[d] = y->ch[d^1];
y->ch[d^1] = x;
maintain(x);
x = y;
maintain(x);
}
void splay(Node *&x,int rank) {
pushdown(x);
int s1 = x->ch[0] == NULL?0:x->ch[0]->siz;
if (s1 + 1 == rank) return;
int d;
if (s1 + 1 < rank) {
rank = rank - s1 - 1;
d = 1;
}
else d = 0;
pushdown(x->ch[d]);
int s2 = x->ch[d]->ch[0] == NULL?0:x->ch[d]->ch[0]->siz;
if (s2 + 1 != rank) {
int d1;
if (s2 + 1 < rank) d1 = 1,rank = rank - s2 - 1;
else d1 = 0;
pushdown(x->ch[d]->ch[d1]);
splay(x->ch[d]->ch[d1],rank);
if (d == d1) rotate(x,d);
else rotate(x->ch[d],d1);
}
rotate(x,d);
}
Node *merge(Node *left,Node *right) {
splay(left,left->siz);
left->ch[1] = right;
maintain(left);
return left;
}
void split(Node *x,Node *&left,Node *&right,int rank) {
splay(x,rank);
right = x->ch[1]; x->ch[1] = NULL;
left = x; maintain(left);
}
void DEL(Node *&x) {
if (x->ch[0] != NULL) DEL(x->ch[0]);
if (x->ch[1] != NULL) DEL(x->ch[1]);
x->ch[0] = x->ch[1] = NULL;
bo[++now] = x->po;
}
public:
data() {
root = &pool[0];
root->num = 0;
root->rev = 0; root->modi = p;
maintain(root);
for (int i = 1; i <= 500000; i++) bo[i] = i;
now = 500000;
}
void Insert (int rank,int num) {
Node *left,*right;
split(root,left,right,rank);
left->ch[1] = &pool[bo[now]];
Node *&x = left->ch[1]; x->po = bo[now--];
x->num = num; x->rev = 0; x->modi = p;
maintain(x);
maintain(left);
root = merge(left,right);
splay(root,rank);
}
void Remove(int pos,int tot) {
splay(root,pos);
if (pos + tot == n) {
DEL(root->ch[1]);
root->ch[1] = NULL;
maintain(root);
}
else {
splay(root->ch[1],tot+1);
DEL(root->ch[1]->ch[0]);
root->ch[1]->ch[0] = NULL;
maintain(root->ch[1]);
maintain(root);
}
}
void Modify(int pos,int tot,int num) {
splay(root,pos);
Node *x;
if (pos + tot == n) x = root->ch[1];
else {
splay(root->ch[1],tot+2);
x = root->ch[1]->ch[0];
}
x->modi = x->num = num;
int t = x->modi;
x->sum = t*x->siz;
if (t > 0) x->l = x->r = x->MAX = x->sum;
else x->l = x->r = 0,x->MAX = t;
if (pos + tot == n) root->ch[1] = x;
else root->ch[1]->ch[0] = x;
if (pos + tot != n) maintain(root->ch[1]);
maintain(root);
}
void Rever(int pos,int tot) {
splay(root,pos);
Node *x;
if (pos + tot == n) x = root->ch[1];
else {
splay(root->ch[1],tot+2);
x = root->ch[1]->ch[0];
}
x->rev ^= 1;
swap(x->l,x->r);
if (pos + tot == n) root->ch[1] = x;
else root->ch[1]->ch[0] = x;
if (pos + tot != n) maintain(root->ch[1]);
maintain(root);
}
int SUM(int pos,int tot) {
splay(root,pos);
if (pos + tot == n) return root->ch[1]->sum;
else {
splay(root->ch[1],tot+2);
return root->ch[1]->ch[0]->sum;
}
}
int MAXSUM() {
splay(root,1);
return root->ch[1]->MAX;
}
};
char co[10];
int getcom()
{
scanf("%s",co);
if (co[0] == 'I') return 1;
if (co[0] == 'D') return 2;
if (co[0] == 'R') return 4;
if (co[0] == 'G') return 5;
int len = strlen(co);
if (len > 7) return 3;
else return 6;
}
int typ,tot,pos;
int main()
{
#ifdef YZY
freopen("yzy.txt","r",stdin);
#endif
scanf("%d%d",&n,&m);
static data tree;
int N = n; n = 1;
for (int i = 1; i <= N; i++) {
int x; scanf("%d",&x);
++n;
tree.Insert(i,x);
}
while (m--) {
typ = getcom();
if (typ == 1) {
int c;
scanf("%d%d",&pos,&tot);
for (int i = pos + 1; i <= pos + tot; i++) {
scanf("%d",&c);
++n;
tree.Insert(i,c);
}
}
else if (typ == 2) {
scanf("%d%d",&pos,&tot);
tree.Remove(pos,tot);
n -= tot;
}
else if (typ == 3) {
int c;
scanf("%d%d%d",&pos,&tot,&c);
tree.Modify(pos,tot,c);
}
else if (typ == 4) {
scanf("%d%d",&pos,&tot);
tree.Rever(pos,tot);
}
else if (typ == 5) {
scanf("%d%d",&pos,&tot);
if (!n) printf("0\n");
else printf("%d\n",tree.SUM(pos,tot));
}
else {
if (!n) printf("0\n");
else printf("%d\n",tree.MAXSUM());
}
}
return 0;
}
相关文章推荐
- 文章标题
- 自定义Log
- FastSocket.Net
- 第三周项目4:穷举法解决组合问题
- Android MainActivity如何加载BaseActivity的布局
- java静态代码块/静态属性、构造块、构造方法执行、main方法、普通代码块的顺序
- Object-C学习笔记——内存管理
- C语言指针函数链表复习
- Java Socket编程详解
- 实验0 了解和熟悉操作系统
- MySQL数据库总结(8)字符集与校对集
- 剑指offer-反转链表
- CodeForces #4B. Before an Exam
- 基于python利用爬虫爬取网页教程
- 第二周学习进度
- 现有分布式技术(socket、.net remoting、asp.net webservice、WSE、ES)和wcf的比较及优势
- 通过sleep_for来使线程睡眠一定的时间
- vs2010 代码自动排版快捷键
- 在ios中运用core animation暂停和继续动画
- php 打印、显示、输出函数 收集