• [JSOI2008]最大数 题解


    前言

    巨佬说:要有线段树,于是蒟蒻就开始打线段树。

    蒟蒻只能拿之前0分的板子题开刀了QAQ。

    题解

    一开始我以为插入操作不带取模,于是打了下面这个弱智玩意
    下面的代码是会WA的

    #include <cstdio>
    #include <algorithm>
    #define ll long long
    
    using namespace std;
    
    ll read(){
    	ll x = 0; int zf = 1; char ch = ' ';
    	while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    	if (ch == '-') zf = -1, ch = getchar();
    	while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
    }
    
    ll s[800005];
    ll _max[800005], cnt[800005];
    int m, d, pos = 1;
    const int MAXM = 200001;
    
    pair<ll, ll> query(int pos, int l, int r, int x, int y){
    	if (x <= l && r <= y)
    		return make_pair(cnt[pos], _max[pos]);
    	pair<ll, ll> ans = make_pair(-(1ll << 62), -(1ll << 62));
    	int mid = (l + r) >> 1;
    	if (x <= mid)
    		ans = max(ans, query(pos << 1, l, mid, x, y));
    	if (mid < y)
    		ans = max(ans, query(pos << 1 | 1, mid + 1, r, x, y));
    	return ans;
    }
    
    void add(int pos, int l, int r, int x, ll val){
    	if (l == r){
    		s[pos] += val, cnt[pos] += s[pos] / d; s[pos] %= d;
    		_max[pos] = s[pos];
    		return ;
    	}
    	int mid = (l + r) >> 1;
    	if (x <= mid)
    		add(pos << 1, l, mid, x, val);
    	else if (mid < x)
    		add(pos << 1 | 1, mid + 1, r, x, val);
    	s[pos] = (s[pos << 1] + s[pos << 1 | 1]) % d;
    	if (cnt[pos << 1] > cnt[pos << 1 | 1])
    		_max[pos] = _max[pos << 1], cnt[pos] = cnt[pos << 1];
    	else if (cnt[pos << 1] < cnt[pos << 1 | 1])
    		_max[pos] = _max[pos << 1 | 1], cnt[pos] = cnt[pos << 1 | 1];
    	else{
    		cnt[pos] = cnt[pos << 1];
    		_max[pos] = max(_max[pos << 1], _max[pos << 1 | 1]);
    	}
    }
    
    int main(){
    	m = read(), d = read(); char op[1]; ll t = 0;
    	while (m--){
    		scanf("%s", op); int n = read();
    		if (op[0] == 'Q')
    			printf("%lld
    ", t = query(1, 1, MAXM, pos - n + 1, pos).second);
    		else if (op[0] == 'A')
    			add(1, 1, MAXM, ++pos, n + t);
    	}
    	return 0;
    }
    

    一波上交WA 0。
    然后一看不对啊,样例都过不了啊(我自信的没测样例)。
    仔细看了一下题目,发现插入操作带取模。QAQ
    简直有毒。
    然后一遍过...

    #include <cstdio>
    #include <algorithm>
    #define ll long long
    
    using namespace std;
    
    ll read(){
    	ll x = 0; int zf = 1; char ch = ' ';
    	while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
    	if (ch == '-') zf = -1, ch = getchar();
    	while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
    }
    
    ll s[800005];
    ll _max[800005];
    int m, d, pos = 1;
    const int MAXM = 200001;
    
    ll query(int pos, int l, int r, int x, int y){
    	if (x <= l && r <= y)
    		return _max[pos];
    	ll ans = -(1ll << 62);
    	int mid = (l + r) >> 1;
    	if (x <= mid)
    		ans = max(ans, query(pos << 1, l, mid, x, y));
    	if (mid < y)
    		ans = max(ans, query(pos << 1 | 1, mid + 1, r, x, y));
    	return ans;
    }
    
    void add(int pos, int l, int r, int x, ll val){
    	if (l == r){
    		(s[pos] += val) %= d, _max[pos] = s[pos];
    		return ;
    	}
    	int mid = (l + r) >> 1;
    	if (x <= mid)
    		add(pos << 1, l, mid, x, val);
    	else if (mid < x)
    		add(pos << 1 | 1, mid + 1, r, x, val);
    	s[pos] = (s[pos << 1] + s[pos << 1 | 1]) % d;
    	_max[pos] = max(_max[pos << 1], _max[pos << 1 | 1]);
    }
    
    int main(){
    	m = read(), d = read(); char op[1]; ll t = 0;
    	while (m--){
    		scanf("%s", op); int n = read();
    		if (op[0] == 'Q')
    			printf("%lld
    ", t = query(1, 1, MAXM, pos - n + 1, pos));
    		else if (op[0] == 'A')
    			add(1, 1, MAXM, ++pos, (n + t) % d);
    	}
    	return 0;
    }
    
  • 相关阅读:
    Samba文件服务器详细配置步骤
    chkconfig命令详解
    rdesktop的使用方法
    CentOs中yum安装LAMP+PHPMYADMIN
    YUM源
    Centos下设置VNC为3389端口
    vnc服务的安装与配置
    rdesktop的使用
    nginx 日志分割
    程序员私活网汇总
  • 原文地址:https://www.cnblogs.com/linzhengmin/p/11128242.html
Copyright © 2020-2023  润新知