【POJ】3468 A Simple Problem with Integers
这题用线段树轻松解了,重新用树状数组解,关键点是区间更新。
公式推导如下:
sum[x] = org_sum[x] + delta[1]*x + delta[2]*(x-1) + delta[x]*1
= org_sum[x] + Sigma(delta[1..x]) * (x+1) - Sigma(delta[i]*i)
树状数组增加两个结点信息分别存delta[i] 和 delta[i] * i就好了。
/* 3468 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
__int64 s, ss;
} node_t; const int maxn = 1e5+;
int a[maxn];
__int64 tot[maxn];
node_t nd[maxn];
int n, q; int lowest(int x) {
return -x & x;
} __int64 sum(int x) {
__int64 s = , ss = ;
int xx = x; while (x) {
s += nd[x].s;
ss += nd[x].ss;
x -= lowest(x);
} return (xx+) * s - ss;
} void update(int x, int delta) {
__int64 tmp = 1LL * x * delta; while (x <= n) {
nd[x].s += delta;
nd[x].ss += tmp;
x += lowest(x);
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d %d", &n, &q);
rep(i, , n+) {
scanf("%d", &a[i]);
tot[i] = tot[i-] + a[i];
} __int64 ans;
char op[];
int l, r, d; while (q--) {
scanf("%s %d %d", op, &l, &r);
if (op[] == 'Q') {
ans = tot[r] - tot[l-] + sum(r) - sum(l-);
printf("%I64d\n", ans);
} else {
scanf("%d", &d);
update(l, d);
update(r+, -d);
}
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
数据发生器。
from copy import deepcopy
from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t = 1
bound = 10**3
# fout.write("%d\n" % (t))
for tt in xrange(t):
n = randint(100, 200)
q = randint(100, 200)
fout.write("%d %d\n" % (n, q))
L = []
for i in xrange(n):
x = randint(1, bound)
L.append(x)
fout.write(" ".join(map(str, L)) + "\n")
for i in xrange(q):
op = randint(0, 1)
if op:
l = randint(1, n)
r = randint(l, n)
k = randint(1, bound)
fout.write("C %d %d %d\n" % (l, r, k))
else:
l = randint(1, n)
r = randint(l, n)
fout.write("Q %d %d\n" % (l, r)) def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()
【POJ】3468 A Simple Problem with Integers的更多相关文章
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- 【HDOJ】4267 A Simple Problem with Integers
树状数组.Easy. /* 4267 */ #include <iostream> #include <string> #include <map> #includ ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 3468——A Simple Problem with Integers——————【线段树区间更新, 区间查询】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 86780 ...
随机推荐
- RestFul && HATEOAS && Spring-Data-Rest介绍
1.什么是RestFul 经常上网的同学会发现,现代软件的一个重要趋势就是互联网化,几乎没有一款软件是纯粹的单机版了.通常的情况下,软件管理着服务器的资源以及这些资源的状态变化,用户通过在浏览器输入h ...
- 【转】oracle中触发器中:new和:old 的使用方法
create or replace trigger TRI_PrintTest before delete or insert or update on TEST_EXAM --触发事件 for ea ...
- Jquery实现图片左右自动滚动
图片左右滚动的效果想必大家都有见到过吧,其实很简单.在本文将为大家介绍下使用Jquery是如何实现图片左右自动滚动的. 代码如下:<!DOCTYPE HTML> <html> ...
- nginx禁止访问某个后缀名的文件
猛然发现通过http://ip:端口号/路径/config.ini竟然能下载项目代码的配置文件,检查nginx配置,原来是没有加限制,立即加上,并记录如下: location ~* \.(ini|do ...
- Linux学习系列之Linux入门(一)linux安装与入门
第一篇:安装并配置Linux开发环境 一.安装linux: 主要安装Linux的发行版,到目前为之,主要的发行版有: 比较常用的是Ubuntu.redhat和centOS,主要的安装方法详细: Ubu ...
- 【转】FTP自动上传文件的perl脚本以及配置文件
这个perl文件将执行的任务是, 按照指定的文件夹目录,自动将该文件夹下的所有文件上传到指定ftp站点的指定目录下 本应用程序设计的几个基本理念是:工具箱再利用:尽可能利用已有的工具:简化运行步骤:不 ...
- Indri中的动态文档索引技术
Indri中的动态文档索引技术 戴维 译 摘要: Indri 动态文档索引的实现技术,支持在更新索引的同时处理用户在线查询请求. 文本搜索引擎曾被设计为针对固定的文档集合进行查询,对不少应用来说,这种 ...
- Mac下安装Redis图解教程
去redis官网(http://redis.io/download)自行下载安装包解压缩到本地文件夹,比如放在Mac应用程序文件夹(/Applications/),在终端进入redis文件夹. 需要进 ...
- 开源YYKit-b
转自 ibireme的博客,大家可以去他博客看看,有很多开发过程的一些调研和评测. YYModel 类似 Mantle/JSONModel 的工具,性能比 Mantle 高一个数量级,有更好的容错性, ...
- 基于Ogre的DeferredShading(延迟渲染)的实现以及应用
http://blog.sina.com.cn/s/blog_458f871201017i06.html 这篇文章写的不错,从比较宏观的角度说了下作者在OGRE中实现的延迟渲染