【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 ...
随机推荐
- 一句话解释jquery中offset、pageX, pageY、position、scrollTop, scrollLeft的区别
offset 元素相对文档的偏移 pageX, pageY 事件(鼠标)相对文档的偏移 注意:文档是指document, 而不是当前窗口,是包含了滚动位置的,即滚动条的位置对这些值是不产生影响的 ...
- NodeJS + Socket.io聊天服务器连接数达到1024后就连不上了
如果是亚马逊的Engine Yard服务器,解决办法为: 1.查看端口占用情况,找到nodejs进程号,例如我这里是8000端口 lsof -i:8000 找到pid 例如为 8213 2.设置no ...
- [Interview][CodingExam]
這次去Interview, 其中有一個公司 把我列為 2/25的考慮對象, 在Final 的 1/2, 我被刷掉了. 因為第一輪的程式,我稍微google了一下,參考了既有的寫法. 即使第二輪我用完全 ...
- A3992学习记录
ATmega64+A3992驱动步进电机 //ATmega 64a 电机驱动板程序//编译环境 AVR Studio 4.17/AVR GCC//系统外部时钟16M//作者:虞恺 //日期:2012. ...
- thinkphp操作数据库
1.实现or操作: $where=array( 'city'=>array('like',array('%'.$_GET['city'].'%')); 'hangye'=>array('l ...
- SPI协议及其工作原理详解
一.概述. SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在外围设备微控 ...
- 拓展,Fibonacci螺旋
#该程序由023递归这课中的fibonacci数列递归写法修改而成 #在写的过程中发现,如果要正确引导用户的每一次输入,写的代码比主程序还要多 #当然,为了使程序在用户交互过程中显得更加友好,提供错误 ...
- vim使用总结
tar -xf vim.tar -C ~ vim /etc/vimrc vim /root/.vimrc set ts=4 设置tab有多少空格 set ai 自动对齐 set nu set mous ...
- buffer busy wait在RAC环境下出现
昨天运维组的同时反映有套系统用户反映很慢,需要协助帮忙检查什么原因引起的性能问题.导出了从8点到11点的AWR报告进行分析,发现等待事件里大部分的指标都正常,就是buffer busy wait的平均 ...
- mysql mysqldump只导出表结构或只导出数据的实现方法
mysql mysqldump只导出表结构或只导出数据的实现方法,需要的朋友可以参考下. mysql mysqldump 只导出表结构 不导出数据 复制代码代码如下: mysqldump --opt ...