zoukankan      html  css  js  c++  java
  • BZOJ 1251: 序列终结者 Splay

    1251: 序列终结者

    Description

    网上有许多题,就是给定一个序列,要你支持几种操作:A、B、C、D。一看另一道题,又是一个序列 要支持几种操作:D、C、B、A。尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思。这道题目 就叫序列终结者吧。 【问题描述】 给定一个长度为N的序列,每个序列的元素是一个整数(废话)。要支持以下三种操作: 1. 将[L,R]这个区间内的所有数加上V。 2. 将[L,R]这个区间翻转,比如1 2 3 4变成4 3 2 1。 3. 求[L,R]这个区间中的最大值。 最开始所有元素都是0。

    Input

    第一行两个整数N,M。M为操作个数。 以下M行,每行最多四个整数,依次为K,L,R,V。K表示是第几种操作,如果不是第1种操作则K后面只有两个数。

    Output

    对于每个第3种操作,给出正确的回答。

    Sample Input

    4 4
    1 1 3 2
    1 2 4 -1
    2 1 3
    3 2 4

    Sample Output

    2
    【数据范围】
    N<=50000,M<=100000。

    思路:

      模板题, 留代码。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define get(x) (ch[f[x]][1]==x)
    #define ls ch[p][0]
    #define rs ch[p][1]
    using namespace std;
    const int N = 50050, inf = 0x3f3f3f3f;
    int ch[N][2], f[N], siz[N], root;
    int tag[N], del[N], val[N], mx[N];
    inline char nc() {
    	static char buf[100000], *p1, *p2;
    	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    }
    inline int read() {
    	/*int x = 0, f = 1; char ch = nc();
    	while(!isdigit(ch)){if(ch=='-')f=-1;ch=nc();}
    	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^'0');ch=nc();}
    	return x*f;*/
    	int x;scanf("%d", &x);return x;
    }
    inline void up(int p) {
    	if(p) siz[p] = siz[ls] + siz[rs] + 1;
    	mx[p] = max(mx[ls], mx[rs]);
    	mx[p] = max(mx[p], val[p]);
    }
    void pushdown(int p) {
    	int t = del[p];
    	if(t) {
    		del[p] = 0;
    		if(ls) del[ls] += t, mx[ls] += t, val[ls] += t;
    		if(rs) del[rs] += t, mx[rs] += t, val[rs] += t;
    	}
    	if(tag[p]) {
    		tag[p] = 0;
    		swap(ls, rs);
    		tag[ls]^=1, tag[rs] ^=1;
    	}
    }
    void rotate(int x) {
    	int y = f[x], z = f[y], k = get(x);
    	ch[y][k]=ch[x][k^1];f[ch[y][k]]=y;ch[x][k^1]=y;
    	f[y] = x, f[x] = z;
    	if(z) ch[z][ch[z][1]==y]=x;
    	up(y), up(x);
    	if(root==y) root = x;
    }
    int find(int x) {
    	int p = root;
    	while(1) {
    		pushdown(p);
    		if(x<=siz[ls])p=ls;
    		else {
    			x-=siz[ls]+1;
    			if(!x) return p;
    			p=rs;
    		}
    	}
    }
    void splay(int x, int y) {
    	for(int fa;(fa=f[x])!=y;rotate(x))
    	if(f[fa]!=y)rotate(get(x)==get(fa)?fa:x);
    }
    void reverse(int x,int y) {
    	splay(x, f[root]);
    	splay(y, root);
    	tag[ch[y][0]]^=1;
    }
    void add(int x, int y, int c) {
    	splay(x, f[root]);
    	splay(y, root);
    	del[ch[y][0]]+=c;
    	val[ch[y][0]]+=c;
    	mx[ch[y][0]] +=c;
    }
    void query(int  x, int y) {
    	splay(x, f[root]);
    	splay(y, root);
    	printf("%d
    ", mx[ch[y][0]]);
    }
    void build(int fa, int l, int r) {
    	if(l>r) return;
    	int mid = (l+r) >> 1;
    	ch[fa][mid>fa]=mid;
    	siz[mid] = 1;
    	val[mid] = 0;
    	f[mid]   = fa;
    	build(mid, l, mid-1);
    	build(mid, mid+1, r);
    	up(mid);
    }
    int main() {
    	int n=read(), m=read();
    	for(int i=0;i<=n+2;i++) mx[i] = -inf;
    	build(0, 1, n+2);
    	root = (n+3) >> 1;
    	int opt, x, y;
    	for(int i=1;i<=m;i++) {
    		opt=read(), x=read(), y=read();
    		if(x>y) swap(x, y);
    		switch (opt) {
    			case 1 :add(find(x), find(y+2), read());break;
    			case 2 :reverse(find(x), find(y+2));break;
    			case 3 :query(find(x), find(y+2));break;
    		} 
    	}
    
    }
    
  • 相关阅读:
    THINKPHP导入全部post参数
    thinkphp 表单一些
    随机唯一不重复
    TP关联模型
    PHP函数之类
    MSSQLid清零
    httpwebrequest异步参考
    反射
    UrlOper
    工作周记
  • 原文地址:https://www.cnblogs.com/Tobichi/p/9204391.html
Copyright © 2011-2022 走看看